how can I override the validation rules which are defined in a json schema which is inherited by the "allOf" keyword?
Example:
{
"$schema": "http://json-schema.org/draft-06/schema",
"title": "My JSON Schema",
"description": "",
"definitions": {
"a": {
"type": "object",
"properties": {
"b": {
"type": "object",
"properties": {
"c": {
"type": "string",
"minLength": 1,
"maxLength": 100
}
},
"required": [
"c"
]
}
},
"required": [
"b"
]
}
},
"properties": {
"main": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/a"
}
]
},
"sub": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/a"
}
]
}
}
}
The json schema defines two objects:
Both objects inherit their properties from the defined object "a" But the object "sub" should have other validation rules for property b.c (currently it is minLength 1 and maxLength 100).
So of course following json is invalid:
{
"main" :{
"b": {
"c": "This property has a min length"
}
},"sub" : {
"b": {
"c": ""
}
}
}
How can I override the validation rules for property b.c?
Because there is no such thing as schema inheritance currently defined. When using allOf , you require that all schemas in allOf match; and if you are strict about what can exist in this or that JSON, you'll have added additionalProperties to false . As such, you cannot inherit.
In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.
A schema can reference another schema using the $ref keyword. The value of $ref is a URI-reference that is resolved against the schema's Base URI. When evaluating a $ref , an implementation uses the resolved identifier to retrieve the referenced schema and applies that schema to the instance.
$id is a reserved keyword. It serves for: Declaring an identifier for the schema or subschema. Declaring a base URL against which $ref URLs are resolved.
There is currently no way to do this perscribed by the JSON Schema specification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With