How to use JSON schema validation against JSON document having arrays? - json

I have json document which contains three main elements along with sub elements. These elements also have arrays. I validated this document with json schema but I am not sure if there is any simple way to get rid of repeating schema for arrays as the current schema is too long.
JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "/",
"type": "object",
"properties": {
"book": {
"id": "book",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"isbn": {
"id": "isbn",
"type": "string"
},
"title": {
"id": "title",
"type": "string"
},
"price": {
"id": "price",
"type": "string"
},
"categories": {
"id": "categories",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "1",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
}
]
},
"warehouse": {
"id": "warehouse",
"type": "object",
"properties": {
"location": {
"id": "location",
"type": "string"
},
"aisle": {
"id": "aisle",
"type": "string"
},
"shelf": {
"id": "shelf",
"type": "string"
}
}
}
},
"required": [
"isbn",
"title",
"price",
"categories",
"category",
"warehouse"
]
},
{
"id": "1",
"type": "object",
"properties": {
"isbn": {
"id": "isbn",
"type": "string"
},
"title": {
"id": "title",
"type": "string"
},
"price": {
"id": "price",
"type": "string"
},
"categories": {
"id": "categories",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "1",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
}
]
},
"warehouse": {
"id": "warehouse",
"type": "object",
"properties": {
"location": {
"id": "location",
"type": "string"
},
"aisle": {
"id": "aisle",
"type": "string"
},
"shelf": {
"id": "shelf",
"type": "string"
}
}
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"isbn": {
"id": "isbn",
"type": "string"
},
"title": {
"id": "title",
"type": "string"
},
"price": {
"id": "price",
"type": "string"
},
"categories": {
"id": "categories",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "1",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
}
]
},
"warehouse": {
"id": "warehouse",
"type": "object",
"properties": {
"location": {
"id": "location",
"type": "string"
},
"aisle": {
"id": "aisle",
"type": "string"
},
"shelf": {
"id": "shelf",
"type": "string"
}
}
}
}
}
],
"required": [
"0",
"1",
"2"
]
}
},
"required": [
"book"
]
}
And JSON document:
{
"book":[
{
"isbn":"0-672-33751-7",
"title":"Unity Game Development",
"price":"$55.99",
"categories":[
{
"category":"Game Development"
},
{
"category":"Unit Game Engine"
},
{
"category":"Beginner to Intermediate"
}
],
"warehouse":{
"location":"North Warehouse",
"aisle":"A16",
"shelf":"3"
}
},
{
"isbn":"978-0-9871530-7-4",
"title":"Jquery: Novice to Ninja",
"price":"$39.95",
"categories":[
{
"category":"JavaScript"
},
{
"category":"jQuery"
},
{
"category":"Web Development"
}
],
"warehouse":{
"location":"North Warehouse",
"aisle":"W03",
"shelf":"6"
}
},
{
"isbn":"0-538-74477-4",
"title":"Programming Logic and Design",
"price":"$89.99",
"categories":[
{
"category":"Programming"
},
{
"category":"JavaScript"
},
{
"category":"Computer Logic"
}
],
"warehouse":{
"location":"South Warehouse",
"aisle":"B44",
"shelf":"1"
}
}
]
}

You've got a lot of issues with this schema. I suggest you read http://spacetelescope.github.io/understanding-json-schema/ to get a better understanding of JSON Schema. Below is the corrected schema. I'll give some brief advice, but not a lot of explanation. Go read that link for more info.
id is required to be a absolute URL. "/" is not valid
Don't use id anywhere other than the root of the document. It has some unexpected properties. It's best to just avoid it.
The items keyword should be a schema not an array of schemas. The form you are using is not intended for arrays with a single type of item.
The required keyword applies only to properties in the schema it appears in. Sub-schemas need to define their own required properties.
The required keyword does not work on arrays. Use minItems and maxItems instead.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"book": {
"type": "array",
"items": {
"type": "object",
"properties": {
"isbn": { "type": "string" },
"title": { "type": "string" },
"price": { "type": "string" },
"categories": {
"type": "array",
"items": {
"type": "object",
"properties": {
"category": { "type": "string" }
},
"required": ["category"]
}
},
"warehouse": {
"type": "object",
"properties": {
"location": { "type": "string" },
"aisle": { "type": "string" },
"shelf": { "type": "string" }
}
}
},
"required": [
"isbn", "title", "price", "categories", "warehouse"
]
}
}
},
"required": ["book"]
}

Related

Error while creating models for request validation in aws api gateway

I am trying to build model for the request validation in the aws api gateway that i created.
In the model schema, when i use simple json schema, it works. But when i use my schema as below.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"uuid": {
"type": "string",
"pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
"minLength": 36,
"maxLength": 36
},
"macAddress": {
"type": "string",
"pattern": "([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})"
},
"locale": {
"type": "string",
"pattern": "^[a-z]{2}(-[A-Z]{2})?$",
"minLength": 2
},
"status": {
"type": "string",
"enum": ["good", "bad", "canceled"]
},
"timezone": {
"type": "string",
"pattern": "^[A-Z][a-z]+/[A-Z][a-z]+$"
},
"device": {
"type": "object",
"additionalProperties": false,
"properties": {
"mac_address": {
"type": "string",
"$ref": "#/definitions/macAddress"
},
"hardware": {
"type": "string"
}
},
"required": ["mac_address"]
},
"started_at": {
"type": "number",
"minimum": 0,
"maximum": 2147483647999
},
"finished_at": {
"type": "number",
"minimum": 0,
"maximum": 2147483647999
},
"questionType": {
"enum": [
"barcode",
"name",
"text"
]
},
"question": {
"type": "object",
"additionalProperties": false,
"properties": {
"key": {
"string"
},
"type": {
"$ref": "#/definitions/questionType"
}
},
"required": ["key", "type"]
},
"answers": {
"type": "array",
"items": {
"type": "object",
"properties": {
"question": {
"$ref": "#/definitions/question"
},
"status": {
"type": "string",
"$ref": "#/definitions/status"
},
"started_at": {
"type": "number",
"$ref": "#/definitions/started_at"
},
"finished_at": {
"type": "number",
"$ref": "#/definitions/finished_at"
}
},
"allOf": [
{"if": {
"properties": { "question": { "properties": {
"type": { "const": "text" }
} } } },
"then": {
"properties": { "value": {"type": "string"} } }
},
{"if": {
"properties": { "question": { "properties": {
"type": { "const": "name" }
} } } },
"then": {
"properties": { "value": {"type": "string" } } }
},
{"if": {
"properties": { "question": { "properties": {
"type": { "const": "barcode" }
}} } },
"then": {
"properties": { "value": {"type": "string" } } }
}
]
}
}
},
"properties": {
"locale": {
"$ref": "#/definitions/locale"
},
"timezone": {
"$ref": "#/definitions/timezone"
},
"device": {
"$ref": "#/definitions/device"
},
"answers": {
"$ref": "#/definitions/answers"
}
},
"required": ["locale"]
}
I get the below error.
validation error detected: Value 'Invalid model specified: Validation Result: warnings : [], errors : [Invalid model schema specified. Unsupported keyword(s): ["if","then"], Invalid model schema specified.
When i read the aws documentation, i see that the json schema draft 4 is supported and i have also used the draft 4 rules to build my schema.
Is there a hack to use the if/then in aws model schema or how can i build the schema without if then?

array not required returns the message: "1 item required; only 0 were given" in the json schema

I configured the json schema and it looked like this:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"organisationId": {
"type": "string"
},
"clientId": {
"type": "string"
},
"issuer": {
"oneOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"createdDate": {
"type": "string",
"format": "date-time"
},
"lastModifiedDate": {
"type": "string",
"format": "date-time"
},
"consentId": {
"type": "string"
},
"internalStatus": {
"type": "string",
"enum": [
"AUTHORISED",
"AWAITING_AUTHORISATION",
"REJECTED",
"TIMEOUT_EXPIRED",
"OVERDUE",
"REVOKED"
]
},
"permissions": {
"type": "array",
"items": [
{
"type": "string"
}
]
},
"approverType": {
"type": "string",
"enum": [
"AND",
"OR"
]
},
"status": {
"type": "string",
"enum": [
"AUTHORISED",
"AWAITING_AUTHORISATION",
"REJECTED",
"REVOKED",
"CONSUMED"
]
},
"statusUpdateDateTime": {
"type": "string",
"format": "date-time"
},
"expirationDateTime": {
"type": "string",
"format": "date-time"
},
"resourceGroups": {
"uniqueItems": true,
"oneOf": [
{
"type": "array"
},
{
"type": "null"
}
],
"items": [
{
"type": "object",
"properties": {
"resourceGroupId": {
"type": "integer"
},
"permissions": {
"type": "array",
"items": [
{
"type": "string"
}
]
},
"resources": {
"type": "array",
"uniqueItems": true,
"items": [
{
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": [
"AVAILABLE",
"UNAVAILABLE",
"TEMPORARY_UNAVAILABLE",
"PENDING_AUTHORISATION"
]
},
"additionalInfos": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
]
},
"type": {
"type": "string",
"enum": [
"CUSTOMERS_PERSONAL_IDENTIFICATIONS",
"CUSTOMERS_PERSONAL_QUALIFICATION",
"CUSTOMERS_PERSONAL_ADITTIONALINFO",
"CUSTOMERS_BUSINESS_IDENTIFICATIONS",
"CUSTOMERS_BUSINESS_QUALIFICATION",
"CUSTOMERS_BUSINESS_ADITTIONALINFO",
"CAPITALIZATION_TITLES",
"PENSION",
"DAMAGES_AND_PEOPLE_PATRIMONIAL",
"DAMAGES_AND_PEOPLE_AERONAUTICAL",
"DAMAGES_AND_PEOPLE_NAUTICAL",
"DAMAGES_AND_PEOPLE_NUCLEAR",
"DAMAGES_AND_PEOPLE_OIL",
"DAMAGES_AND_PEOPLE_RESPONSABILITY",
"DAMAGES_AND_PEOPLE_TRANSPORT",
"DAMAGES_AND_PEOPLE_FINANCIAL_RISKS",
"DAMAGES_AND_PEOPLE_RURAL",
"DAMAGES_AND_PEOPLE_AUTO",
"DAMAGES_AND_PEOPLE_HOUSING",
"DAMAGES_AND_PEOPLE_PEOPLE",
"DAMAGES_AND_PEOPLE_ACCEPTANCE_AND_BRANCHES_ABROAD"
]
},
"hidden": {
"type": "boolean"
},
"resourceId": {
"type": "string"
}
}
}
]
},
"additionalInfos": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
]
},
"type": {
"type": "string",
"enum": [
"ACCOUNT",
"CREDIT_CARD_ACCOUNT",
"LOAN",
"INVOICE_FINANCING",
"UNARRANGED_ACCOUNT_OVERDRAFT",
"FINANCING",
"RESOURCE",
"CUSTOMER"
]
}
},
"required": [
"permissions",
"type"
]
}
]
},
"approvers": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": [
"AUTHORISED",
"AWAITING_AUTHORISATION",
"REJECTED",
"REVOKED",
"CONSUMED"
]
},
"approverId": {
"type": "string"
}
},
"required": [
"approverId"
]
}
]
},
"loggedUser": {
"type": "object",
"properties": {
"document": {
"type": "object",
"properties": {
"identification": {
"type": "string"
},
"rel": {
"type": "string"
}
},
"required": [
"identification",
"rel"
]
}
},
"required": [
"document"
]
},
"businessEntity": {
"type": "object",
"properties": {
"document": {
"type": "object",
"properties": {
"identification": {
"type": "string"
},
"rel": {
"type": "string"
}
},
"required": [
"identification",
"rel"
]
}
},
"required": [
"document"
]
}
},
"required": [
"organisationId",
"clientId",
"consentId",
"permissions",
"approverType",
"status",
"statusUpdateDateTime",
"expirationDateTime",
"loggedUser"
]
},
"links": {
"type": "object",
"properties": {
"self": {
"type": "string"
},
"first": {
"type": "string"
},
"prev": {
"type": "string"
},
"next": {
"type": "string"
},
"last": {
"type": "string"
}
},
"required": [
"self"
]
},
"meta": {
"type": "object",
"properties": {
"totalRecords": {
"type": "integer"
},
"totalPages": {
"type": "integer"
}
},
"required": [
"totalRecords",
"totalPages"
]
}
}
}
Note that the "resources" array is not required, it is not mandatory.
However... when I run my test and it returns an empty array in "resources":
"resourceGroupId":1,
"permissions":[
"CUSTOMERS_PERSONAL_QUALIFICATION_READ",
"CUSTOMERS_PERSONAL_IDENTIFICATIONS_READ"
],
"resources":[],
"type":"CUSTOMER"
}
I get the following message:
"#/data/resourceGroups/0/resources: failed schema #/properties/data/properties/resourceGroups/items/0/properties/resources: 1 item required; only 0 were supplied."
I don't understand how 1 item is required if the array is not required.
and still have( "uniqueItems": true
)
which in theory would accept a [] in the return, according to the Json schema documentation.
I've tried passing minItems=0 and many other things and nothing has worked.
This looks like a combination of a bug in the validator you are using and an incorrect usage of items. The good news is that when you use items correctly, the bug will probably not apply.
The items keyword has two forms: one that takes a single schema and the other that takes an array of schemas. The form must people need most of the time is the single schema form.
{
"type": "array",
"items": { "type": "string" }
}
This schemas asserts that every item in the array is a string.
{
"type": "array",
"items": [
{ "type": "string" },
{ "type": "number" }
]
}
This schema asserts that the first item in the array is a string and the second is a number. However, it should not require that those items are present or assert anything on the rest of the items in the array. So, the bug is that your validator seems to require those values when it shouldn't.
But, that bug shouldn't affect you because you I'm sure you really meant to use the single schema version of items that validates all the items in the array against the schema. Just remove the [ and ] and your schema should work as you expected.

Error in Parse JSON step in Microsoft Flow

Sorry for the long question, but I wanted to include all the JSON to get the best help.
we are getting an error parsing JSON from an HTTP Get request in our flow.
Here is the schema we are using in our Parse JSON step:
{
"type": "object",
"properties": {
"d": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"uri": {
"type": "string"
},
"type": {
"type": "string"
}
}
},
"Alerts": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Groups": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Id": {
"type": "integer"
},
"IsHiddenInUI": {
"type": "boolean"
},
"LoginName": {
"type": "string"
},
"Title": {
"type": "string"
},
"PrincipalType": {
"type": "integer"
},
"Email": {
"type": "string"
},
"IsEmailAuthenticationGuestUser": {
"type": "boolean"
},
"IsShareByEmailGuestUser": {
"type": "boolean"
},
"IsSiteAdmin": {
"type": "boolean"
},
"UserId": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"NameId": {
"type": "string"
},
"NameIdIssuer": {
"type": "string"
}
}
}
},
"required": [
"__metadata",
"Alerts",
"Groups",
"Id",
"IsHiddenInUI",
"LoginName",
"Title",
"PrincipalType",
"Email",
"IsEmailAuthenticationGuestUser",
"IsShareByEmailGuestUser",
"IsSiteAdmin",
"UserId"
]
}
}
}
}
}
}
we are only interested in the Email property that we want to use to send approvals...
here are the INPUTS results of the Parse JSON step that fails:
{
"d": {
"results": [
{
"__metadata": {
"id": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)",
"uri": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)",
"type": "SP.User"
},
"Alerts": {
"__deferred": {
"uri": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)/Alerts"
}
},
"Groups": {
"__deferred": {
"uri": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)/Groups"
}
},
"Id": 1691,
"IsHiddenInUI": false,
"LoginName": "i:0#.f|membership|Surnamei#Company.org.uk",
"Title": "Firstname Surname",
"PrincipalType": 1,
"Email": "Surnamef#Company.org.uk",
"Expiration": "",
"IsEmailAuthenticationGuestUser": false,
"IsShareByEmailGuestUser": false,
"IsSiteAdmin": false,
"UserId": {
"__metadata": {
"type": "SP.UserIdInfo"
},
"NameId": "10033fff9fe67a30",
"NameIdIssuer": "urn:federation:microsoftonline"
},
"UserPrincipalName": "Surnamef#Company.org.uk"
},
}
]
}
here is the schema from the run results of the Parse JSON that fails:
{
"type": "object",
"properties": {
"d": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"uri": {
"type": "string"
},
"type": {
"type": "string"
}
}
},
"Alerts": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Groups": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Id": {
"type": "integer"
},
"IsHiddenInUI": {
"type": "boolean"
},
"LoginName": {
"type": "string"
},
"Title": {
"type": "string"
},
"PrincipalType": {
"type": "integer"
},
"Email": {
"type": "string"
},
"IsEmailAuthenticationGuestUser": {
"type": "boolean"
},
"IsShareByEmailGuestUser": {
"type": "boolean"
},
"IsSiteAdmin": {
"type": "boolean"
},
"UserId": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"NameId": {
"type": "string"
},
"NameIdIssuer": {
"type": "string"
}
}
}
},
"required": [
"__metadata",
"Alerts",
"Groups",
"Id",
"IsHiddenInUI",
"LoginName",
"Title",
"PrincipalType",
"Email",
"IsEmailAuthenticationGuestUser",
"IsShareByEmailGuestUser",
"IsSiteAdmin",
"UserId"
]
}
}
}
}
}
}
and here is the error in the step from the run history:
[
{
"message": "Invalid type. Expected Object but got Null.",
"lineNumber": 0,
"linePosition": 0,
"path": "d.results[3].UserId",
"schemaId": "#/properties/d/properties/results/items/properties/UserId",
"errorType": "type",
"childErrors": []
}
]
so how can I change the schema to successfully retrieve the email
The problem was caused by UserId being null in several elements of the data received from the HTTP Get, while that is set as required in the JSON Schema...
initially we solved it by using the below in the HTTP Get:
_api/web/sitegroups/getbyname('Legal Owners')/users?$filter=UserId+ne+null
Then we were given a better and more elegant solution on the Power Platform Community:
in the Apply to each step use:
body('Send_an_HTTP_request_to_SharePoint')?['d']?['results']
and within the loop use:
item()?['Email']

Create a recursive json schema which is generic

I am trying to create a json schema for a json file. The structure of the json is generic and I am trying to create a generic schema for json files which contain a similar structure to the one shown below.
**Json file:**
{
"Soccer League": {
"lvl": "Championships",
"state": "1",
"Clubs": {
"Rosely": {
"Boys": {"id": "A1", "state": "Ready"},
"Girls": {
"id": "A2",
"state": "Ready",
"Substitutes": {
"id": "A3",
"state": "Sitting",
"Goalkeepers": {
"Players": {"id": "A4", "state": "Idle"},
"id": "A5",
"state": "Idle",
},
},
},
},
"Division League": {
"TeamA": {
"PlayersA": {
"id": "A6",
"status": "Ready",
"CoachA": {"id": "A7", "state": "Ready"},
},
"PlayersB": {
"id": "A8",
"state": "Playing",
"CoachB": {"id": "A9", "state": "Idle"},
},
}
},
},
}
}
**Json-schema:**
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Soccer League": {
"type": "object",
"properties": {
"lvl": {
"type": "string"
},
"state": {
"type": "string"
},
"Clubs": {
"type": "object",
"properties": {
"Rosely": {
"type": "object",
"properties": {
"Boys": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"id",
"state"
]
},
"Girls": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"state": {
"type": "string"
},
"Substitutes": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"state": {
"type": "string"
},
"Goalkeepers": {
"type": "object",
"properties": {
"Players": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"id",
"state"
]
},
"id": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"Players",
"id",
"state"
]
}
},
"required": [
"id",
"state",
"Goalkeepers"
]
}
},
"required": [
"id",
"state",
"Substitutes"
]
}
},
"required": [
"Boys",
"Girls"
]
},
"Division League": {
"type": "object",
"properties": {
"TeamA": {
"type": "object",
"properties": {
"PlayersA": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"status": {
"type": "string"
},
"CoachA": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"status": {
"type": "string"
}
},
"required": [
"id",
"status"
]
}
},
"required": [
"id",
"status",
"CoachA"
]
},
"PlayersB": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"status": {
"type": "string"
},
"CoachB": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"status": {
"type": "string"
}
},
"required": [
"id",
"status"
]
}
},
"required": [
"id",
"status",
"CoachB"
]
}
},
"required": [
"PlayersA",
"PlayersB"
]
}
},
"required": [
"TeamA"
]
}
},
"required": [
"Rosely",
"Division League"
]
}
},
"required": [
"lvl",
"state",
"Clubs"
]
}
},
"required": [
"Soccer League"
]
}
As we can see from above, we have many same layers but just with different names. I am unsure on how to create a recursive json schema for json files which will follow a similar format. This is sort of hard-coded, but how to make it generic.
I am new to json schema and would appreciate if someone could help me infer a schema from the json above. Any help would be greatly appreciated.

JSON Schema Template and JSON Editor

I have been struggling for a few days, trying to use the JSON Editor Javascript plugin. I am using the SWIG JS engine, but I am open to propositions that will solve my issue.
I created a JSON Template that works fine so far, but that does not achieve entirely what I need to do, and I tried understanding and using the syntax provided by the JSON Editor github page, but no luck so far.
How can I make the template behave as follow :
If the value of features.type == "Point", then I want features.display to have a property of the type "#/definitions/marker". Otherwise (features.type == "Polygon") I want features.display to have a property of the type "#/definitions/area". The same goes for filter.display.
So far, I use the "oneOf" property, because it's the closest to what I want, but definitely not quite it.
Thanks for your help !
Kind regards.
Here is my JSON Schema so far :
{
"type": "array",
"title": "Layers",
"items": {
"title": "Layer",
"type": "object",
"headerTemplate": "{{table.id.public_name}}",
"properties": {
"table":{"$ref": "#/definitions/table"},
"features":{"$ref": "#/definitions/features"}
}
},
"definitions": {
"table": {
"title": "Table Information",
"type": "object",
"properties": {
"id":{"$ref": "#/definitions/id"},
"primary_key":{"$ref": "#/definitions/primary_key"},
"read":{"$ref": "#/definitions/read"}
}
},
"features": {
"title": "Features Settings",
"type": "object",
"properties": {
"type":{"$ref": "#/definitions/type"},
"id":{"$ref": "#/definitions/id"},
"cols":{"$ref": "#/definitions/cols"},
"display": {
"type": "object",
"title": "Display",
"format": "grid",
"oneOf": [
{"$ref": "#/definitions/marker"},
{"$ref": "#/definitions/area"}
]
}
}
},
"cols": {
"title": "Table Columns",
"type": "array",
"items": {
"type": "object",
"title": "Column",
"properties": {
"id":{"$ref": "#/definitions/id"},
"read":{"$ref": "#/definitions/read"},
"write":{"$ref": "#/definitions/write"},
"filter":{"$ref": "#/definitions/filter"}
}
}
},
"id": {
"title": "Identifier",
"type": "object",
"format": "grid",
"properties": {
"name":{"$ref": "#/definitions/name"},
"public_name":{"$ref": "#/definitions/public_name"}
}
},
"name": {
"title": "Name",
"type": "string"
},
"public_name": {
"title": "Public Name",
"type": "string"
},
"primary_key": {
"title": "Primary key",
"type": "string",
"format": "grid"
},
"write": {
"title": "Editing",
"type": "object",
"format": "grid",
"properties": {
"read":{"$ref": "#/definitions/read"},
"method":{"$ref": "#/definitions/method"}
}
},
"read": {
"title": "Reading",
"type": "array",
"items": {
"type": "integer",
"title": "Access Level",
"format": "grid",
"properties":{"$ref": "#/definitions/access_level"}
}
},
"type": {
"title": "Type",
"type": "string",
"enum": [ "Point", "Polygon" ]
},
"access_level": {
"title": "Access Level",
"type": "integer",
"format": "number"
},
"method": {
"title": "Method",
"type": "object",
"format": "grid",
"properties": {
"type":{"$ref": "#/definitions/method_type"},
"data":{"$ref": "#/definitions/data"}
}
},
"data": {
"title": "Data",
"type": "array",
"items": {
"type": "string",
"title": "Data",
"format": "grid",
"properties":{"$ref": "#/definitions/value"}
}
},
"value": {
"title": "Value",
"type": "string"
},
"filter": {
"title": "Filter",
"type": "array",
"items": {
"type": "object",
"title": "Filter",
"properties": {
"value":{"$ref": "#/definitions/value"},
"display": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/marker"},
{"$ref": "#/definitions/area"}
]
}
}
}
},
"marker": {
"title": "Marker",
"type": "object",
"format": "grid",
"properties": {
"color":{"$ref": "#/definitions/color"},
"icon":{"$ref": "#/definitions/icon"}
}
},
"color": {
"title": "Color",
"type": "string",
"enum": ["red", "darkred", "orange", "green", "darkgreen", "blue", "purple", "darkpuple", "cadetblue"]
},
"css_color": {
"title": "CSS Color",
"type": "string",
"format": "color"
},
"icon": {
"title": "Icon",
"type": "object",
"format": "grid",
"properties": {
"name":{"$ref": "#/definitions/name"},
"color":{"$ref": "#/definitions/css_color"}
}
},
"area": {
"title": "Area",
"type": "object",
"properties": {
"color":{"$ref": "#/definitions/color"},
"border":{"$ref": "#/definitions/border"}
}
},
"border": {
"title": "Border",
"type": "object",
"properties": {
"border_color":{"$ref": "#/definitions/border_color"},
"width":{"$ref": "#/definitions/width"}
}
},
"border_color": {
"title": "Color",
"type": "object",
"format": "grid",
"properties": {
"normal":{"$ref": "#/definitions/normal"},
"hover":{"$ref": "#/definitions/hover"}
}
},
"width": {
"title": "Width",
"type": "string"
},
"normal": {
"title": "Normal",
"type": "string",
"format": "color"
},
"hover": {
"title": "Hover",
"type": "string",
"format": "color"
},
"method_type": {
"title": "Type",
"type": "string",
"enum": [ "text", "select" ]
}
}
}
The only way to enforce this (until appareance of jsonschema draft 5) is to use the enum as a discriminator. You can find an example in How to use dependencies in JSON schema (draft-04)