I am working on a Json schema for validation but it does not seem to be working. Apart from the if then condition, the rest works fine but even though i am following the example provided in jsonschema - Applying Subschemas Conditionally, it does not work. This is the schema
'''
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"identifierKey": {
"type": "string",
"pattern": "^[a-z][a-z0-9]*(_[a-z0-9] )*$",
"minLength": 2
},
"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
},
"mail_identifierKey": {
"pattern": "^(?:(?!.*?[.]{2})[a-zA-Z0-9](?:[a-zA-Z0-9. !%-]{1,64}|)|\"[a-zA-Z0-9. !% -]{1,64}\")@[a-zA-Z0-9][a-zA-Z0-9.-] (.[a-z]{2,}|.[0-9]{1,})$"
},
"question_type": {
"enum": [
"barcode",
"name",
"nps",
"single_choice",
"text",
"date",
"number",
"id",
"mail_address",
"multiple_choice",
"phone_number",
"zip_code",
"rating"
]
},
"question": {
"type": "object",
"additionalProperties": false,
"properties": {
"key": {
"$ref": "#/definitions/identifierKey"
},
"type": {
"$ref": "#/definitions/question_type"
}
},
"required": ["key", "type"]
},
"answers": {
"type":"array",
"items": {
"type": "object",
"properties": {
"question": {
"$ref": "#/definitions/question"
},
"status": {
"type": "string"
}
},
"allOf": [
{"if": {
"properties": { "question": { "const": "rating" } } },
"then": {
"properties": { "value": { "type": "number", "minimum": 1, "maximum": 5 } } }
},
{"if": {
"properties": { "question": { "const": "zip_code" } } },
"then": {
"properties": { "value": { "type": "string", "$ref": "#/definitions/identifierKey","minLength": 4, "maxLength": 10 } } }
},
{"if": {
"properties": { "question": { "const": "phone_number" } } },
"then": {
"properties": { "value": { "type": "string", "minLength": 8, "maxLength": 15 } } }
},
{"if": {
"properties": { "question": { "const": "multiple_choice" } } },
"then": {
"properties": { "value": { "type": "array", "minItems": 1, "uniqueItems": true,
"contains": { "type": "string" } }
}
}
},
{"if": {
"properties": { "question": { "const": "single_choice" } } },
"then": {
"properties": { "value": {"type": "string","$ref": "#/definitions/identifierKey"} } }
},
{"if": {
"properties": { "question": { "const": "mail_address" } } },
"then": {
"properties": { "value": {"type": "string","$ref": "#/definitions/mail_identifierKey"} } }
},
{"if": {
"properties": { "question": { "const": "id" } } },
"then": {
"properties": { "value": {"type": "string","$ref": "#/definitions/uuid"} } }
},
{"if": {
"properties": { "question": { "const": "number" } } },
"then": {
"properties": { "value": {"type": "number"} } }
},
{"if": {
"properties": { "question": { "const": "text" } } },
"then": {
"properties": { "value": {"type": "string"} } }
},
{"if": {
"properties": { "question": { "const": "date" } } },
"then": {
"properties": { "value": {"type": "string", "format": "date"} } }
},
{"if": {
"properties": { "question": { "const": "nps" } } },
"then": {
"properties": { "value": {"type": "number", "minimum": 1, "maximum":10 } } }
},
{"if": {
"properties": { "question": { "const": "name" } } },
"then": {
"properties": { "value": {"type": "string" } } }
},
{"if": {
"properties": { "question": { "const": "barcode" } } },
"then": {
"properties": { "value": {"type": "number" } } }
}
]
}
}
},
"properties": {
"answers": {
"$ref": "#/definitions/answers"
}
}
}
'''
and this is what i am trying to validate
''' {
"$schema": "./blank.json",
"answers":[ {
"status": "hello1",
"question": {
"key": "hello",
"type": "rating"
},
"value": 1234
}]
} '''
i am following the example below but not able to figure out what i am missing
CodePudding user response:
Okay... I see the problem.
Here's the if
subschema that you expect to trigger for your data
{
"properties": {
"question": { "const": "rating" }
}
}
However, in your data, question
has a type
property.
"question": {
"key": "hello",
"type": "rating"
}
This means that the if
subschema isn't passing because {"key": "hello", "type": "rating" }
!= "rating"
.
You'll need to update your if
subschema to account for this property hierarchy.
{
"properties": {
"question": {
"properties": {
"type": { "const": "rating" }
}
}
}
}
You'll need to do this for all of the if
subschemas.
Making just this change gives this error on my validator:
{
"valid": false,
"keywordLocation": "#/properties/answers/$ref/items/allOf/0/then/properties/value/maximum",
"absoluteKeywordLocation": "https://json-everything/base#/definitions/question_type/items/allOf/0/then/properties/value/maximum",
"instanceLocation": "#/answers/0/value",
"error": "1234 is greater than or equal to 5"
}