Reputation: 200
I'm working on a json schema like this:
{
"$schema": "http://json-schema.org/schema#",
"title": "Layout",
"description": "The layout created by the user",
"type": "object",
"definitions": {
"stdAttribute": {
"type": "object",
"required": ["attributeName","attributeValue"],
"properties": {
"attributeValue": {
"type": "string"
},
"attributeName": {
"type": "string"
}
}
},
"stdItem": {
"type": "object",
"required" : ["stdType","stdAttributes"],
"properties": {
"stdType": {
"enum": [
"CONTAINER",
"TEXT",
"TEXTAREA",
"BUTTON",
"LABEL",
"IMAGE",
"MARCIMAGE",
"DATA",
"SELECT",
"TABLE"
]
},
"stdAttributes": {
"type": "array",
"items": {
"$ref": "#/definitions/stdAttribute"
},
"minItems": 1
},
"children": {
"type": "array",
"items": {
"$ref": "#/definitions/stdItem"
}
}
}
}
},
"properties":{
"stdItem":{ "$ref": "#/definitions/stdItem" }
}
}
I'm trying to validate the following json with the above scheme:
{
"stdItem": {
"stdType": "CONTAINER",
"stdAttributes": [
{
"attributeName": "ola",
"attributeValue": "teste"
}
],
"children": [
{
"stdItem": {
"stdType": "TEXT",
"stdAttributes": [
{
"attributeName": "ola",
"attributeValue": "teste"
}
],
"children": []
}
}
]
}
}
I'm getting an error telling me that the require attributes stdType and stdAttributes are missing for the path stdItem/children/0. As you can see the attributes are there , they are not missing.
I tried to change the order fo the attributes but still doesnt work. I keep getting the following error:
--- BEGIN MESSAGES --- error: object has missing required properties (["stdAttributes","stdType"]) level: "error" schema: {"loadingURI":"#","pointer":"/definitions/stdItem"} instance: {"pointer":"/stdItem/children/0"} domain: "validation" keyword: "required" required: ["stdAttributes","stdType"] missing: ["stdAttributes","stdType"] --- END MESSAGES ---
Could anyone point me for what i'm doing wrong?
Upvotes: 1
Views: 857
Reputation: 1965
When you declare the "children" property you are saying it's a "stdItem" , so it expects there to have the stdAttributes and stdType properties. Instead what you have in your json is a "stdItem" property that is of stdItem type. So, you are missing the declaration of that property (stdItem) in your schema.
This schema will validate your json:
{
"$schema": "http://json-schema.org/schema#",
"title": "Layout",
"description": "The layout created by the user",
"type": "object",
"definitions": {
"stdAttribute": {
"type": "object",
"required": ["attributeName","attributeValue"],
"properties": {
"attributeValue": {
"type": "string"
},
"attributeName": {
"type": "string"
}
}
},
"stdItem": {
"type": "object",
"required" : ["stdType","stdAttributes"],
"properties": {
"stdType": {
"enum": [
"CONTAINER",
"TEXT",
"TEXTAREA",
"BUTTON",
"LABEL",
"IMAGE",
"MARCIMAGE",
"DATA",
"SELECT",
"TABLE"
]
},
"stdAttributes": {
"type": "array",
"items": {
"$ref": "#/definitions/stdAttribute"
},
"minItems": 1
},
"children": {
"type": "array",
"items": {
"type": "object",
"properties": {
"stdItem": { "$ref": "#/definitions/stdItem" }
}
}
}
}
}
},
"properties":{
"stdItem": { "$ref": "#/definitions/stdItem" }
}
}
Notice that I'm adding an object to the item specification of "children" that has a "stdItem" property. (I didn't declare it as required but you might want to add that)
Upvotes: 2