Create a recursive json schema which is generic - json

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.

Related

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.

How to use Parser Transformation for JSON data in IICS?

I am new to IICS and I have JSON data as below, which I would to parse in csv file. I am using this link as a reference to achieve this transformation. I created valid mapping in IICS.The mapping runs fine. However, when I see my jobs I am receiving below error.I went to the path mentioned and opened the Events.cme file in Notepad but cannot make of what file is talking about (Note: in belwo output I deleted few of the numbers)
Not sure what is wrong ? Do I need to save my JSON data file as txt file ?
Any help will be appreciated! Thanks in advance!
ERROR after running the mapping
[ERROR] Failed to process data: File C:/IICSLabFiles/test.json doesn't exist or isn't readable- for more information see file://C:/PROGRA~1/Informatica Cloud Secure Agent/apps/Data_Integration_Server/data/CMReports/Tmp/2022-06-01/HierarchyParser_h2r_udt_8gns3_ONLY_H2R_XMAP_/Events.cme
Opening Events.cme file in notepad produces following
<B#80010%#>
!~109146~165266~~10.2.2.65()
<B#80032%#>
</B#8032%#>
<m -- XMap%m>
!~103149~1654220266~~Pages\/page_m_1.cmv%Pages\/page_m_1.json
<B#80037%XML#>
!~1031~1654220266~~Pages\/Input_of_m_1.cmv%Pages\/Input_of_m_1.json
<LocalFile>
!~309025~16542266~~C:\/IICSLabFiles\/test.json
</LocalFile>
!~103205~16540266~~C:\/IICSLabFiles\/test.json
!~3033~1654220266~~
</B#8007%XML#>
</m -- XMap>
</B#80010%#>
JSON Data that is saved in test.json (with File type as JSON File):
{
"current_page": 1,
"first_page_url": "https://covid-api.com/api/regions?per_page=20&page=1",
"last_page_url": "https://covid-api.com/api/regions?per_page=20&page=50",
"next_page_url": "https://covid-api.com/api/regions?per_page=20&page=2",
"prev_page_url": null,
"per_page": "20",
"last_page": 50,
"from": 1,
"path": "https://covid-api.com/api/regions",
"to": 20,
"total": 997,
"data": [
{
"iso": "CHN",
"name": "China"
},
{
"iso": "TWN",
"name": "Taipei and environs"
},
{
"iso": "USA",
"name": "US"
},
{
"iso": "JPN",
"name": "Japan"
},
{
"iso": "THA",
"name": "Thailand"
},
{
"iso": "KOR",
"name": "Korea, South"
},
{
"iso": "SGP",
"name": "Singapore"
},
{
"iso": "PHL",
"name": "Philippines"
},
{
"iso": "MYS",
"name": "Malaysia"
},
{
"iso": "VNM",
"name": "Vietnam"
},
{
"iso": "AUS",
"name": "Australia"
},
{
"iso": "MEX",
"name": "Mexico"
},
{
"iso": "BRA",
"name": "Brazil"
},
{
"iso": "COL",
"name": "Colombia"
},
{
"iso": "FRA",
"name": "France"
},
{
"iso": "NPL",
"name": "Nepal"
},
{
"iso": "CAN",
"name": "Canada"
},
{
"iso": "KHM",
"name": "Cambodia"
},
{
"iso": "LKA",
"name": "Sri Lanka"
},
{
"iso": "CIV",
"name": "Cote d'Ivoire"
}
]
}
**JSON SCHEMA that is saved in Hierarchy schema (with file type as JSON FILE) **
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"current_page": {
"type": "integer"
},
"first_page_url": {
"type": "string"
},
"last_page_url": {
"type": "string"
},
"next_page_url": {
"type": "string"
},
"prev_page_url": {
"type": "null"
},
"per_page": {
"type": "string"
},
"last_page": {
"type": "integer"
},
"from": {
"type": "integer"
},
"path": {
"type": "string"
},
"to": {
"type": "integer"
},
"total": {
"type": "integer"
},
"data": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
},
{
"type": "object",
"properties": {
"iso": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"iso",
"name"
]
}
]
}
},
"required": [
"current_page",
"first_page_url",
"last_page_url",
"next_page_url",
"prev_page_url",
"per_page",
"last_page",
"from",
"path",
"to",
"total",
"data"
]
}
Source connection Setup
Path_text file contains following information
Path
C:/IICSLabFiles/test.json
The error message "C:/IICSLabFiles/test.json doesn't exist or isn't readable" suggests you try reading local file. Is this the path to the file located on Secure Agent running the mapping or is it the path to a file stored on your laptop? What is your Source definition?
Keep in mind that you design the mapping on your laptop where you have access to files stored on your laptop - but once you execute, it gets processed by Secure Agent (that can be a different machine, cloud-hosted, etc.). In this case it seems the Secure Agent cannot access the file at the given location.
It's also possible to have Secure Agent installed on your machine and run the process on the laptop where you actually have been designing the mapping. In such case please make sure there are no typos in the path, no leading or trailing empty spaces. And if it's a Windows-based Secure Agent, verify the paths as the one you use has froward slashes while Windows uses backslashes usually:
C:/IICSLabFiles/test.json
vs
C:\IICSLabFiles\test.json

How to prevent additions properties when using oneOf in JSON Schema

I have a JSON configuration with an array of "specs", with many different kinds of "path" properties. The "kind" property is the selector of the variant.
I want to restrict the validation to not allow addition properties. In the JSON example, the properties "noAllowedInt" and "notAllowedObject" must report a validation error.
How can I add this rule to the JSON schema?
{
"specs": [
{
"id": 12,
"label": "Serial",
"path": {
"kind": "serial",
"speed": 9600,
"parity": "even"
}
},
{
"id": 13,
"label": "Memory",
"path": {
"kind": "memory",
"storage": "permanent",
"location": "external"
},
"noAllowedInt": 42,
"notAllowedObject": {
"value": 3.1415
}
}
]
}
There are two Json Schema to validate this, a simple one and one with "definitions"
Simple JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"specs": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"properties": {
"id": {
"type": "integer"
},
"label": {
"type": "string"
}
},
"required": [
"id",
"label"
],
"oneOf": [
{
"type": "object",
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"serial"
]
},
"speed": {
"type": "integer"
},
"parity": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
},
{
"type": "object",
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"memory"
]
},
"storage": {
"type": "string"
},
"location": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
}
]
}
}
},
"required": [
"specs"
]
}
JSON Schema with "definitions"
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"specs": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"properties": {
"id": {
"type": "integer"
},
"label": {
"type": "string"
}
},
"required": [
"id",
"label"
],
"oneOf": [
{
"$ref": "#/definitions/PathKindSerialType"
},
{
"$ref": "#/definitions/PathKindMemory"
}
]
}
}
},
"required": [
"specs"
],
"definitions": {
"PathKindSerialType": {
"type": "object",
"additionalProperties": true,
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"serial"
]
},
"speed": {
"type": "integer"
},
"parity": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
},
"PathKindMemory": {
"type": "object",
"additionalProperties": true,
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"memory"
]
},
"storage": {
"type": "string"
},
"location": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
}
}
}
I guess you need to keep the path definition as a property at the specs array level. This would be my proposal:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
"type": "object",
"additionalProperties": false,
"properties": {
"specs": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "integer"
},
"label": {
"type": "string"
},
"path": {
"oneOf": [
{
"$ref": "#/definitions/PathKindSerialType"
},
{
"$ref": "#/definitions/PathKindMemory"
}
]
}
},
"required": [ "id", "label" ]
}
}
},
"required": [ "specs" ],
"definitions": {
"PathKindSerialType": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [ "serial" ]
},
"speed": {
"type": "integer"
},
"parity": {
"type": "string"
}
},
"required": [ "kind" ]
},
"PathKindMemory": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [ "memory" ]
},
"storage": {
"type": "string"
},
"location": {
"type": "string"
}
},
"required": [ "kind" ]
}
}
}

JSONSchema (draft 7) not validating child definitions for arrays

I'm getting very confused as to why my JSONSchema will not validate my data as expected.
I've appended my JSONShema and Example Date below.
The schema validates the first layer of the nested data structure (the Organisation) without issue, but fails to validate the second layer (User) and below.
Can anybody point me in the right direction as to what I'm doing wrong?
Much appreciated!
JSONSchema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://myorg/json/schemas/report-schedule.json",
"type": "array",
"items": {
"$ref": "#/definitions/Organisation"
},
"definitions": {
"Organisation": {
"type": "object",
"required": [
"organisationId",
"organisationName",
"users"
],
"properties": {
"organisationId": {
"type": "string",
"minLength": 1
},
"organisationName": {
"type": "string",
"minLength": 1
},
"users": {
"type": "array",
"items": {
"ref": "#/definitions/User"
}
}
}
},
"User": {
"type": "object",
"required": [
"name",
"email",
"reports"
],
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"email": {
"type": "string",
"format": "email"
},
"reports": {
"type": "array",
"items": {
"ref": "#/definitions/Report"
}
}
}
},
"Report": {
"type": "object",
"required": [
"reportType",
"reportWeekEndDay",
"sendDay",
"sendHour",
"locations",
"widgets"
],
"properties": {
"reportType": {
"type": "string",
"enum": [
"org-weekly"
]
},
"reportWeekEndDay": {
"type": "integer",
"maximum": 6,
"minimum": 0
},
"sendDay": {
"type": "integer",
"maximum": 6,
"minimum": 0
},
"sendHour": {
"type": "integer",
"maximum": 23,
"minimum": 0
},
"locations": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
},
"widgets": {
"$ref": "#/definitions/Widgets"
}
}
},
"Widgets": {
"type": "array",
"title": "Widgets within a report",
"items": {
"$ref": "#/definitions/Widget"
}
},
"Widget": {
"type": "object",
"required": [
"widgetType",
"metrics",
"comparisonType"
],
"properties": {
"widgetType": {
"type": "string",
"enum": [
"table-individual-locations",
"table-all-locations"
]
},
"comparisonType": {
"type": "string",
"enum": [
"week-on-week",
"3-and-1-month-weekly-avg"
]
},
"metrics": {
"$ref": "#/definitions/Metrics"
}
}
},
"Metrics": {
"type": "array",
"title": "The Available Metrics",
"items": {
"oneOf": [
{
"$ref": "#/definitions/FootFallAndMovement"
},
{
"$ref": "#/definitions/Engagement"
},
{
"$ref": "#/definitions/Occupancy"
},
{
"$ref": "#/definitions/SalesDataTransactions"
},
{
"$ref": "#/definitions/SalesDataConversion"
}
]
}
},
"FootFallAndMovement": {
"type": "object",
"required": [
"title",
"type",
"endpoint",
"queryParams"
],
"properties": {
"title": {
"type": "string",
"minLength": 1
},
"type": {
"type": "string",
"const": "FootFallAndMovement"
},
"endpoint": {
"type": "string",
"minLength": 1
},
"queryParams": {
"type": "object",
"required": [
"excludeStaff"
],
"properties": {
"excludeStaff": {
"type": "boolean"
}
}
}
}
},
"Engagement": {
"type": "object",
"required": [
"title",
"type",
"endpoint",
"queryParams"
],
"properties": {
"title": {
"type": "string",
"minLength": 1
},
"type": {
"type": "string",
"const": "Engagement"
},
"endpoint": {
"type": "string",
"minLength": 1
},
"queryParams": {
"type": "object",
"required": [
"excludeStaff",
"contexts"
],
"properties": {
"additionalFilters": {
"type": "object",
"required": [
"ignoreLingerUnder"
],
"properties": {
"ignoreLingerUnder": {
"type": "integer",
"minimum": 1
}
}
},
"excludeStaff": {
"type": "boolean"
},
"contexts": {
"type": "object",
"required": [
"type",
"value"
],
"properties": {
"type": {
"type": "string",
"const": "taxonomy"
},
"value": {
"type": "string",
"minLength": 1
}
}
}
}
}
}
},
"Occupancy": {
"type": "object",
"required": [
"title",
"type",
"endpoint",
"queryParams"
],
"properties": {
"title": {
"type": "string",
"minLength": 1
},
"type": {
"type": "string",
"const": "Occupancy"
},
"endpoint": {
"type": "string",
"minLength": 1
},
"queryParams": {
"type": "object",
"required": [
"excludeStaff",
"contexts"
],
"properties": {
"excludeStaff": {
"type": "boolean"
},
"contexts": {
"type": "object",
"required": [
"type",
"value"
],
"properties": {
"type": {
"type": "string",
"const": "taxonomy"
},
"value": {
"type": "string",
"minLength": 1
}
}
}
}
}
}
},
"SalesDataTransactions": {
"type": "object",
"required": [
"title",
"type",
"endpoint"
],
"properties": {
"title": {
"type": "string",
"minLength": 1
},
"type": {
"type": "string",
"const": "SalesDataTransactions"
},
"endpoint": {
"type": "string",
"minLength": 1
}
}
},
"SalesDataConversion": {
"type": "object",
"required": [
"title",
"type",
"endpoint",
"queryParams"
],
"properties": {
"title": {
"type": "string",
"minLength": 1
},
"type": {
"type": "string",
"const": "SalesDataConversion"
},
"endpoint": {
"type": "string",
"minLength": 1
},
"queryParams": {
"type": "object",
"required": [
"excludeStaff"
],
"properties": {
"excludeStaff": {
"type": "boolean"
}
}
}
}
}
}
}
Example Data
[
{
"organisationName": "a",
"organisationId": "a",
"users": [
{
"nameWITHERROR": "a"/*e.g. name should be required*/,
"email": "rob#test.com",
"reports": [
{
"reportType": "org-weekly1"/*e.g. this should be an enum*/,
"reportWeekEndDay": 6,
"sendDay": 6,
"sendHour": 6,
"locations": [
"abc",
"bcd"
],
"widgets": [
{
"widgetType": "table-all-locations",
"comparisonType": "3-and-1-month-weekly-avg",
"metrics": [
{
"title": "test",
"type": "FootFallAndMovement1"/*e.g. this enum should fail*/,
"endpoint": "/test",
"queryParams": {
"excludeStaff": true
}
},
{
"title": "test",
"type": "Engagement",
"endpoint": "/test",
"queryParams": {
"excludeStaff": true,
"contexts": {
"type": "taxonomy",
"value": "tests"
}
}
},
{
"title": "test",
"type": "Occupancy",
"endpoint": "/test",
"queryParams": {
"excludeStaff": true,
"contexts": {
"type": "taxonomy",
"value": "tests"
}
}
},
{
"title": "test",
"type": "SalesDataTransactions",
"endpoint": "/test"
},
{
"title": "test",
"type": "SalesDataConversion",
"endpoint": "/test",
"queryParams": {
"excludeStaff": true
}
}
]
}
]
}
]
}
]
}
]
Opening your JSON schema in JSONBuddy shows me immediately that you have used "ref" at /definitions/Organisation/properties/users/items/ref. So there is no validation because the definition is not found.

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

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"]
}