Hi All,
I am new to validating JSON file via JSON schema. Below is the JSON schema:
Schema
{
"type": "object",
"properties": {
"Product": {
"type": "object",
"if": {
"properties": {
"Retention": {
"type": "object"
}
}
},
"then": {
"properties": {
"Retention": {
"required": [
"AllowedRetention"
]
}
}
},
"else": {
"properties": {
"Product": {
"required": [
"AllowedRetention"
]
}
}
}
}
}
}
I am using below JSON’s for validation against above JSON schema.
Sample JSON1
{
"Product": {
"Product": "Machine",
"Retention": {
"IsHousekeepingEnabled": false,
"AllowedRetention": 25
}
}
}
Sample JSON2
{
"Product": {
"Process": "Machine",
"AllowedRetention": 255
}
}
Sample JSON 3
{
"Product": {
"Process": "Machine"
}
}
Requirement:
AllowedRetention element is REQUIRED element. But this element position is different in sample JSON1 and JSON2. In absence of this element validation should fail.
JSON validation should only pass for Sample JSON1 and Sample JSON2.
But it should fail for sample JSON3.
If JSON contains “Product->Retention” then REQUIRED element is AllowedRetention which is located at “Product-> Retention → AllowedRetention” in sample JSON1.
If “Retention” element is missing as per JSON2, then REQUIRED element is AllowedRetention which sitting at Product-> AllowedRetention”.
If Retention and AllowedRetention is missing as per JSON3, then schema validation should fail.
I am using https://www.jsonschemavalidator.net/ url to validate JSON against above schema.
I am not sure Schema validation don’t go to else part, not sure IF-ELSE is the right way to handle this kind of validation.
Any inputs would be much appreciated!
Thanks