Iterate over the Json object to fetch level 3 data in Powershell - json

I m trying to loop over the below json object to fetch "x-visibility": "Public
The selected output needs to be written in a new json file.
I m trying to use ForEach-Object on "paths",how do I iterate over "/about" and etc and fetch "x-visibility" object ?
{
"swagger": "2.0",
"info": {
"title": "Ope sepc API",
"version": "20.8.0"
},
"basePath": "/qrs",
"schemes": [
"https"
],
"paths": {
"/about": {
"get": {
"operationId": "65d540ef-b3b9-4678-ae24-2d8abdc304cc",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/About"
}
}
},
"x-visibility": "Public",
"x-stability": "Locked"
}
},
"/about/api/default": {
"get": {
"operationId": "3d1a3502-a020-4a37-8da3-90b7e030792e",
"parameters": [
{
"name": "listentries",
"in": "query",
"required": false,
"default": true,
"type": "boolean",
"allowEmptyValue": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"additionalProperties": {
"type": "object"
}
}
}
}
]
}
}
}
"x-visibility": "Private",
"x-stability": "Locked"
}
},
"/about/api/default/analyticconnection": {
"get": {
"operationId": "2b781738-afd0-37a9-4248-84ec8d6be58c",
"parameters": [
{
"name": "listentries",
"in": "query",
"required": false,
"default": true,
"type": "boolean",
"allowEmptyValue": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/AnalyticConnection"
}
}
},
"x-visibility": "Public",
"x-stability": "Locked"
}
},
"/about/api/default/app": {
"get": {
"operationId": "f868fb6f-330f-60f8-0851-60eadf848788",
"parameters": [
{
"name": "listentries",
"in": "query",
"required": false,
"default": true,
"type": "boolean",
"allowEmptyValue": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/App"
}
}
},
"x-visibility": "Private",
"x-sability": "Locked"
}
},
"/about/api/default/app/content": {
"get": {
"operationId": "4d486f37-c652-85bb-c291-a7384cccdfad",
"parameters": [
{
"name": "listentries",
"in": "query",
"required": false,
"default": true,
"type": "boolean",
"allowEmptyValue": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/AppContent"
}
}
},
"x-visibility": "Public",
"x-stability": "Locked"
}
},
I have to do all this in powershell and I m not to the world of powershell scripting.
Please help if possbile

Please fix your JSON. The following should do what you want:
foreach ($p in $data.paths.psobject.properties.value) { $p.get }
PSObject.Properties will give you a list of all of the properties on an object.

Related

How to use jq to remove a path method (based on a x-* property) from OpenAPI 3.0 or Swagger 2.0 json?

I am new to jq and this one seems impossible to write. So need some help from jq gurus out there:
So I have an Open API spec file, let's take version 3.0 for example:
The paths element has endpoints listed, and each of them has methods(get, post, etc.)
I need a jq filter to select ONLY those methods marked with "x-visibility": "public" and leave everything else out.
So far, I've tried this:
.paths |= map(select(.[]."x-visibility" == "public" | not))
Obviously, it doesn't work.
Any help/pointers will be appreciated!
Here is the OpenAPI 3.0 spec file (extra stuff stripped off):
{
"openapi": "3.0.1",
"info": {
"title": "API service",
"description": "Exposing useful methods",
"x-descriptionForPublicDocs": "This is a test description for public docs specifically",
"contact": {
"name": "Message me",
"url": "https://message.me"
},
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:9090",
"description": "Generated server url"
}
],
"tags": [
{
"name": "AccountGroups"
}
],
"x-tags": [
{
"name": "TestResourceLists"
}
],
"paths": {
"/crane/v1/resource_list/{id}": {
"get": {
"tags": [
"ResourceLists"
],
"x-tags": "TestResourceLists",
"x-visibility": "public",
"summary": "Get Resource List by ID",
"description": "Returns the resource list that has the specified ID.",
"operationId": "getById",
"parameters": [
{
"name": "id",
"in": "path",
"description": "ResourceList Id",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Successful operation",
"content": {
"application/json; charset=UTF-8": {
"schema": {
"$ref": "#/components/schemas/ResourceListModel"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"4XX": {
"description": "Client error",
"content": {
"application/json; charset=UTF-8": {
"schema": {
"$ref": "#/components/schemas/SpringErrorResponse"
}
}
}
}
}
},
"put": {
"tags": [
"ResourceLists"
],
"x-visibility": "internal",
"summary": "Update Resource List",
"description": "Updates the resource list that has the specified ID.",
"operationId": "update",
"parameters": [
{
"name": "id",
"in": "path",
"description": "ResourceList Id",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"description": "Model for Resource List",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ResourceListModel"
},
"examples": {
"TAG": {
"description": "Tag ResourceList",
"value": {
"members": [
{
"string": "string"
}
],
"name": "string",
"resourceListType": "TAG",
"description": "string"
}
},
"RESOURCE_GROUP": {
"description": "RESOURCE_GROUP ResourceList",
"value": {
"description": "string",
"members": [
"string",
"string"
],
"name": "string",
"resourceListType": "RESOURCE_GROUP"
}
},
"COMPUTE_ACCESS_GROUP": {
"description": "COMPUTE_ACCESS_GROUP ResourceList",
"value": {
"description": "string",
"members": [
{
"appIDs": [
"*"
],
"clusters": [
"*"
],
"codeRepos": [
"*"
],
"containers": [
"*"
],
"functions": [
"*"
],
"hosts": [
"*"
],
"images": [
"*"
],
"labels": [
"*"
],
"namespaces": [
"*"
]
}
],
"name": "string",
"resourceListType": "COMPUTE_ACCESS_GROUP"
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful operation",
"content": {
"application/json; charset=UTF-8": {
"schema": {
"$ref": "#/components/schemas/ResourceListModel"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"4XX": {
"description": "Client error",
"content": {
"application/json; charset=UTF-8": {
"schema": {
"$ref": "#/components/schemas/SpringErrorResponse"
}
}
}
}
}
},
"delete": {
"tags": [
"ResourceLists"
],
"x-visibility": "internal",
"summary": "Delete Resource List",
"description": "Deletes the resource list that has the specified ID.",
"operationId": "delete",
"parameters": [
{
"name": "id",
"in": "path",
"description": "ResourceList Id",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"204": {
"description": "Successful operation"
},
"400": {
"description": "Bad Request",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"4XX": {
"description": "Client error",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/SpringErrorResponse"
}
}
}
}
}
}
},
"/crane/v1/account_group/{id}": {
"get": {
"tags": [
"AccountGroups"
],
"x-visibility": "public",
"summary": "Get resource",
"description": "Returns something",
"operationId": "getById_1",
"parameters": [
{
"name": "id",
"in": "path",
"description": "Account Group ID",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "includeAccountInfo",
"in": "query",
"description": "Include Cloud Account details",
"required": false,
"schema": {
"type": "boolean"
}
}
],
"responses": {
"200": {
"description": "Successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AccountGroupModel"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"4XX": {
"description": "Client error",
"content": {
"application/json; charset=UTF-8": {
"schema": {
"$ref": "#/components/schemas/SpringErrorResponse"
}
}
}
}
},
},
"put": {
"tags": [
"AccountGroups"
],
"x-visibility": "internal",
"summary": "Update resource",
"description": "Update information",
"operationId": "update_1",
"parameters": [
{
"name": "id",
"in": "path",
"description": "AccountGroup Id",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"application/json; charset=UTF-8": {
"schema": {
"$ref": "#/components/schemas/AccountGroupModel"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful operation"
},
"400": {
"description": "Bad Request",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"4XX": {
"description": "Client error",
"content": {
"application/json; charset=UTF-8": {
"schema": {
"$ref": "#/components/schemas/SpringErrorResponse"
}
}
}
}
}
},
"delete": {
"tags": [
"AccountGroups"
],
"x-visibility": "internal",
"summary": "Delete resource",
"description": "Deletes the resource",
"operationId": "delete_1",
"parameters": [
{
"name": "id",
"in": "path",
"description": "Delete resource",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Successful operation"
},
"400": {
"description": "Bad Request",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"*/*": {
"schema": {
"type": "object"
}
}
}
},
"4XX": {
"description": "Client error",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/SpringErrorResponse"
}
}
}
}
}
}
}
}
}
.paths |= map_values(
if type == "object"
then with_entries(select(
if .value|objects|has("x-visibility")
then .value|.["x-visibility"] == "public"
else true end) )
else . end)
Use map_values to apply a function to all values of an object. You have two nested objects, so you need to call map_values twice:
.paths |= map_values(map_values(select(."x-visibility" == "public")))
To strip empty paths, select only those values which are not an empty array:
.paths |= map_values(
map_values(select(."x-visibility" == "public"))
| select(. != {})
)
or perhaps even one of the following:
.paths |= map_values(
map_values(select(."x-visibility" == "public"))
| select(length > 0)
)
.paths |= map_values(
map_values(select(."x-visibility" == "public"))
| select(any)
)

Mapping request body fields in logic apps

We have configured common alert schema for alerts and we are using a ticketing software and whenever we receive alert it should create a ticket.
In the logic app I have to make Post API call for creation of ticket with following json object, some fields are hard coded values:
{
"subject": "",
"Id": "123456789", // Hard code value
"priority": "",
"email": "test#test.com", // Hard code value
"status": "Open" // Hard code value
}
Parse Json sample payload schema for Alert:
{
"data": {
"alertContext": {
},
},
"customProperties": null,
"essentials": {
"alertContextVersion": "123",
"alertId": "123",
"alertRule": "Test Alerts",
"description": "test",
"severity": "Sev4"
}
},
"schemaId": "test"
}
I have to map "subject and "priority" fields with alert json object "description" and "severity":
subject-->description
priority --> severity // sev0 =high ,sev1=medium, sev2 =low
How can I achieve this using logic app?
After Parse JSON You can directly map its objects in the compose connector with the required fields. Below is my logic app flow.
You can use the below Code view to reproduce the same in your Logic app
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": {
"customProperties": null,
"data": {
"alertContext": {}
},
"essentials": {
"alertContextVersion": "123",
"alertId": "123",
"alertRule": "Test Alerts",
"description": "test",
"severity": "Sev4"
},
"schemaId": "test"
},
"runAfter": {},
"type": "Compose"
},
"Compose_2": {
"inputs": {
"Id": "123456789",
"email": "test#test.com",
"priority": "#{body('Parse_JSON')?['essentials']?['severity']}",
"status": "Open",
"subject": "#{body('Parse_JSON')?['essentials']?['description']}"
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "Compose"
},
"Parse_JSON": {
"inputs": {
"content": "#outputs('Compose')",
"schema": {
"properties": {
"customProperties": {},
"data": {
"properties": {
"alertContext": {
"properties": {},
"type": "object"
}
},
"type": "object"
},
"essentials": {
"properties": {
"alertContextVersion": {
"type": "string"
},
"alertId": {
"type": "string"
},
"alertRule": {
"type": "string"
},
"description": {
"type": "string"
},
"severity": {
"type": "string"
}
},
"type": "object"
},
"schemaId": {
"type": "string"
}
},
"type": "object"
}
},
"runAfter": {
"Compose": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}

how to write a test for a json schema?

I have created my UI using JSON schema, I am very confused on how can I write a test for it? I checked various resources online but couldn't figure out on how to get started. I want to test that if the radio button is true then the dropdown displays and the dropdown further has input fields in them. Here is my code and I want to test it using javascript or enzyme:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"definitions": {
},
"type": "object",
"properties": {
"enabled": {
"title": "click me to enable this radiobutton",
"type": "boolean",
"default": false
}
},
"dependencies": {
"enabled": {
"oneOf": [
{
"properties": {
"enabled": {
"enum": [
false
]
}
}
},
{
"properties": {
"enabled": {
"enum": [
true
]
},
"style": {
"title": "Color Type",
"enum": [
"rainbow",
"primaryColors",
"mixedColors"
],
"enumNames": [
"Rainbow",
"Primary",
"Mixed"
],
"default": "primaryColors"
}
},
"dependencies": {
"style": {
"oneOf": [
{
"properties": {
"style": {
"enum": [
"rainbow"
]
},
"violet": {
"title": "Violet",
"type": "string"
}
}
},
{
"properties": {
"style": {
"enum": [
"primaryColors"
]
},
"red": {
"title": "this is red",
"type": "string"
}
}
},
{
"properties": {
"style": {
"enum": [
"mixedColors"
]
},
"pink": {
"title": "I am pink",
"type": "string"
}
}
}
]
}
}
}
]
}
}
}

Why is my use of JSON Schema `allOf` keyword not validating correctly?

I am running into more children objects that are not validating correctly (object.actor, object.verb, object.object). I tried looking for any empty schema after changing my if/then structures in the object schema to if/then/else adding the false value for each else. I did not find anything obvious.
JSON -Should fail but doesn't
{
"actor": {
"objectType": "Agent",
"name": "xAPI account",
"mbox": "mailto:xapi#adlnet.gov"
},
"verb": {
"id": "http://adlnet.gov/expapi/verbs/attended",
"display": {
"en-GB": "attended",
"en-US": "attended"
}
},
"object": {
"objectType": "SubStatement",
"actor": {
"objectType": "should fail",
"name": "xAPI mbox",
"mbox": "mailto:should fail"
},
"verb": {
"id": "http://adlnet.gov/expapi/verbs/reported",
"display": {
"should fail": "reported",
"en-US": "reported"
}
},
"object": {
"objectType": "Activity",
"id": "should fail"
}
}
}
JSON - Fails at the root level only; substatement values are not checked. assuming empty schema coming from somewhere.
{
"actor": {
"objectType": "Agent",
"name": "xAPI account",
"mbox": "this fails"
},
"verb": {
"id": "http://adlnet.gov/expapi/verbs/attended",
"display": {
"this fails": "attended",
"en-US": "attended"
}
},
"object": {
"objectType": "SubStatement",
"actor": {
"objectType": "should fail",
"name": "xAPI mbox",
"mbox": "mailto:should fail"
},
"verb": {
"id": "http://adlnet.gov/expapi/verbs/reported",
"display": {
"should fail": "reported",
"en-US": "reported"
}
},
"object": {
"objectType": "Activity",
"id": "should fail"
}
}
}
JSON SCHEMA (stripped to the bone)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "xAPIValidator",
"description": "Validation schema for xAPI tests",
"type": "object",
"allOf": [
{
"$ref": "#/definitions/Statement"
},
{
"statements": [
{
"$ref": "#/definitions/Statement"
}
]
}
],
"definitions": {
"Statement": {
"additionalProperties": false,
"properties": {
"objectType": {
"type:": "string",
"enum": [
"Agent",
"Activity",
"Group",
"SubStatement",
"StatementRef"
]
},
"id": {
"allOf": [
{
"$ref": "#/definitions/uuid"
}
]
},
"actor": {
"$id": "#actor",
"allOf": [
{
"$ref": "#/definitions/allOfAgentGroup"
}
]
},
"verb": {
"$id": "#verb",
"type": "object",
"properties": {
"id": {
"allOf": [
{
"$ref": "#/definitions/URI"
}
]
},
"display": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/lang5646"
}
]
}
}
},
"object": {
"$id": "#object",
"type": "object",
"properties": {
"objectType": {
"type:": "string",
"enum": [
"Activity",
"Agent",
"Group",
"SubStatement",
"StatementRef"
]
}
},
"oneOf": [
{
"if": {
"properties": {
"objectType": {
"const": "SubStatement"
}
}
},
"then": {
"$comment": "Substatement object type",
"allOf": [
{
"$ref": "#/definitions/Statement"
}
],
"allOf": [
{
"not": {
"required": [
"id"
]
}
},
{
"not": {
"required": [
"authority"
]
}
},
{
"not": {
"required": [
"stored"
]
}
},
{"required":["actor","verb","object"]}
]
},
"else": false
}
]
}
},
"required": [
"actor",
"verb",
"object"
]
},
"Agent": {
"$id": "#Agent",
"maxProperties": 3,
"allOf": [
{
"$ref": "#/definitions/IFI"
}
]
},
"AnonGroup": {
"$id": "#AnonGroup",
"maxProperties": 3,
"properties": {
"member": {
"type": "array",
"items": [
{
"allOf": [
{
"$ref": "#/definitions/allOfAgentGroup"
}
]
}
]
}
},
"dependencies": {
"objectType": [
"member"
]
},
"required": [
"member"
],
"not": {
"required": [
"mbox"
]
},
"not": {
"required": [
"mbox_sha1sum"
]
},
"not": {
"required": [
"openid"
]
},
"not": {
"required": [
"account"
]
}
},
"IdGroup": {
"$id": "#IdGroup",
"maxProperties": 4,
"properties": {
"member": {
"type": "array",
"items": [
{
"oneOf": [
{
"$ref": "#/definitions/allOfAgentGroup"
}
]
}
]
}
},
"oneOf": [
{
"$ref": "#/definitions/IFI"
}
]
},
"allOfAgentGroup": {
"properties": {
"objectType": {
"type": "string",
"enum": [
"Agent",
"Group"
]
},
"name": {
"type": "string"
}
},
"oneOf": [
{
"if": {
"properties": {
"objectType": {
"const": "Agent"
}
}
},
"then": {
"allOf": [
{
"$ref": "#/definitions/Agent"
}
]
},
"else": false
},
{
"if": {
"properties": {
"objectType": {
"const": "Group"
}
}
},
"then": {
"oneOf": [
{
"allOf": [
{
"$ref": "#/definitions/IdGroup"
}
]
},
{
"allOf": [
{
"$ref": "#/definitions/AnonGroup"
}
]
}
]
},
"else": false
}
]
},
"IFI": {
"oneOf": [
{
"properties": {
"mbox": {
"allOf": [
{
"$ref": "#/definitions/mailto"
}
]
}
},
"required": [
"mbox"
]
},
{
"properties": {
"mbox_sha1sum": {
"type": "string",
"pattern": "\\b[0-9a-f]{5,40}\\b"
}
},
"required": [
"mbox_sha1sum"
]
},
{
"properties": {
"account": {
"properties": {
"homePage": {
"allOf": [
{
"$ref": "#/definitions/URI"
}
]
},
"name": {
"type": "string"
}
},
"required": [
"homePage",
"name"
]
}
},
"required": [
"account"
]
},
{
"properties": {
"openid": {
"allOf": [
{
"$ref": "#/definitions/URI"
}
]
}
},
"required": [
"openid"
]
}
]
},
"mailto": {
"type": "string",
"pattern": "(mailto:)(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"
},
"URI": {
"type": "string",
"pattern": "^(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]"
},
"uuid": {
"type": "string",
"pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
},
"lang5646": {
"type": "object",
"patternProperties": {
"^((?:(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))|((?:([A-Za-z]{2,3}(-(?:[A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4}|[A-Za-z]{5,8})(-(?:[A-Za-z]{4}))?(-(?:[A-Za-z]{2}|[0-9]{3}))?(-(?:[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*(-(?:[0-9A-WY-Za-wy-z](-[A-Za-z0-9]{2,8})+))*(-(?:x(-[A-Za-z0-9]{1,8})+))?)|(?:x(-[A-Za-z0-9]{1,8})+))$": {
"type": "string"
}
},
"additionalProperties": false
},
"lang5646string": {
"type": "string",
"pattern": "^((?:(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))|((?:([A-Za-z]{2,3}(-(?:[A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4}|[A-Za-z]{5,8})(-(?:[A-Za-z]{4}))?(-(?:[A-Za-z]{2}|[0-9]{3}))?(-(?:[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*(-(?:[0-9A-WY-Za-wy-z](-[A-Za-z0-9]{2,8})+))*(-(?:x(-[A-Za-z0-9]{1,8})+))?)|(?:x(-[A-Za-z0-9]{1,8})+))$"
}
}
}
In "$id": "#object" > oneOf > then, you define allOf twice in that JSON object.
The behaviour of duplicate keys in JSON is undefined.
Often the way it's handled is just to take the last occurrence of each key for an object.
You can see this working by having the following schema and an empty object instance: {}
Schema :
{
"allOf": [
false
],
"allOf": [
true
]
}
The JSON library will likely ignore the first allOf, taking the last occurrence, resulting in allways pass validation. Swap the true and false to double confirm.
Any time you've used a specific *of keyword more than once in the same object in the schema (or any key more than once in an object), you'll need to fix it.

Swagger - Invalid JSON errors

I'm just starting with Swagger UI and I'm trying to understand how it works.
So far I've entered some JSON (manually) and this is the result:
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "PhakeApps API",
"contact": {
"name": "PhakeApps API team",
"url": "http://phakeapps.com/"
},
"license": {
"name": "Creative Commons 4.0 International",
"url": "http://creativecommons.org/licenses/by/4.0/"
}
},
"host": "api.phakeapps.com",
"basePath": "/v1",
"schemes": [
"http"
],
"produces": [
"application/json"
],
"consumes": [
"application/json"
],
"paths": {
"/places/search": {
"post": {
"tags": [
"Places"
],
"description": "Search for (a) place(s) <br /><br /> <b>id</b> - The ID of the request. <br /> <b>api_key</b> - API Key for the platform the request is sent. <i>Currently, not required.</i> <br /> <b>Params</b> - Required. <i>See model & model schema.</i>",
"operationId": "PlacesSearch",
"produces": [
"application/json"
],
"consumes": [
"application/json"
],
"parameters": [
{
"name": "request",
"in": "body",
"paramType": "body",
"description": "Object containing the <u>id</u>, <u>api_key</u> and certain <u>params</u>.",
"required": true,
"schema": {
"$ref": "#/definitions/Search"
}
}
],
"responses": {
"200": {
"description": "Success",
"schema": {
"$ref": "#/definitions/PlacesResult"
}
},
"403": {
"description": "Validation error or Server Failure",
"schema": {
"$ref": "#/definitions/Error"
}
}
}
}
}
},
"definitions": {
"PlacesResult": {
"required": [
"data",
"id",
"code"
],
"properties": {
"data": {
"$ref": "#/definitions/Places"
},
"id": {
"type": "integer",
"format": "int32"
},
"code": {
"type": "integer",
"format": "int32"
}
}
},
"Places": {
"required": [
"places"
],
"properties": {
"places": {
"$ref": "#/definitions/Place"
}
}
},
"City": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"Neighbourhood": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"Cuisine": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"Place": {
"required": [
"id",
"name",
"city",
"neighbourhood",
"address",
"cuisine",
"price",
"photos_cnt",
"lat",
"lng",
"is_fav"
],
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"city": {
"type": "array",
"items": {
"$ref": "#/definitions/City"
}
},
"neighbourhood": {
"type": "array",
"items": {
"$ref": "#/definitions/Neighbourhood"
}
},
"address": {
"type": "string"
},
"cuisine": {
"type": "array",
"items": {
"$ref": "#/definitions/Cuisine"
}
},
"price": {
"type": "integer",
"format": "int32"
},
"photos_cnt": {
"type": "integer",
"format": "int32"
},
"lat": {
"type": "double"
},
"lng": {
"type": "double"
},
"is_fav": {
"type": "boolean"
}
}
},
"Search": {
"required": [
"id",
"api_key",
"params"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"api_key": {
"type": "string"
},
"params": {
"$ref": "#/definitions/SearchParams"
}
}
},
"SearchParams": {
"required": [
"user_id",
"city_id",
"people",
"dt",
"locale"
],
"properties": {
"user_id": {
"type": "string",
"default": "956dda4c21c72e48f5f17a7cd783a0f7"
},
"city_id": {
"type": "string",
"default": "4ec4b3e6098c9d23c925b0c2451eb06a"
},
"people": {
"type": "integer",
"format": "int32",
"minimum": 1,
"default": 2
},
"dt": {
"type": "integer",
"format": "int32",
"default": "1427742000"
},
"locale": {
"type": "string",
"default": "bg"
},
"place_id": {
"type": "string",
"default": "0"
},
"neighborhood_id": {
"type": "string",
"default": "0"
},
"cuisine_id": {
"type": "string",
"default": "0"
},
"kids_place": {
"type": "boolean",
"default": false
},
"price": {
"type": "integer",
"format": "int64",
"default": 1
},
"outdoors": {
"type": "boolean",
"default": false
}
}
},
"Error": {
"required": [
"code",
"data"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"data": {
"type": "array",
"items": {
"type": "array"
}
}
}
}
}
}
However, swagger's validator says it's not valid. The error I get is this
[
{
"level": "error",
"domain": "validation",
"keyword": "anyOf",
"message": "instance failed to match at least one required schema among 2",
"schema": {
"loadingURI": "http://json-schema.org/draft-04/schema#",
"pointer": "/properties/type"
},
"instance": {
"pointer": "/definitions/Place/properties/lat/type"
}
}
]
Note that it works as expected (so far). It displays the data (models and models' structure) properly. Make requests and retrieves responses. Yet the validator says it's not valid. (The yellow badge saying 'Invalid', not the red one that says 'Error').
What am I missing?
Your spec is indeed not valid. In your Place definition, you use "type": "double" to describe the type of the lat (and also lng) property, but such a type does not exist.
If you want to describe a numeric value of 'double' size, you should change the definition as follows:
"lng": {
"type": "number",
"format": "double"
}
Do that everywhere you use the double type, and it should resolve that issue.