I would like to use json schema to combine relative JSON pointer references, with a $ref schema, when I am introducing a conditional if / then statement.
In this case I would like to require that:
- If system = Phone then require usePhone element
- If system = Email then require useEmail element
The schema is generating an error when I use it to validate - I suspect the if -> $ref / enum code is the cause of the issue. The json-schema documentation suggests nesting required constant / enum values inside the defined element but I am unsure how to do this when my element is a $ref location, e.g.:
https://json-schema.org/understanding-json-schema/reference/conditionals.html
"if": {
"properties": { "country": { "const": "United States of America" } }
}
The need for a relative schema is because the instance of ContactPoint is used in multiple locations in the combined schema.
References:
- https://json-schema.org/understanding-json-schema/reference/conditionals.html
- https://docs.opis.io/json-schema/1.x/pointers.html
- https://docs.opis.io/json-schema/1.x/conditional-subschemas.html
- https://docs.opis.io/json-schema/1.x/ref-keyword.html
- https://docs.opis.io/json-schema/1.x/multiple-subschemas.html
Example:
Thanks!
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "characteristic.entity.json",
"title": "characteristic.entity.schema.1.0",
"description": "Characteristic Objects Json Schema",
"definitions": {
"ContactPoint": {
"title": "ContactPoint",
"additionalProperties": true,
"properties": {
"id": {
"description": "",
"$ref": "primitive.entity.json#/definitions/string"
},
"type": {
"description": "The type of Contact.",
"enum": [
"Alternative",
"Primary"
]
},
"system": {
"description": "Telecommunications form for contact point - what communications system is required to make use of the contact.",
"enum": [
"Phone",
"Email",
"other"
]
},
"value": {
"description": "",
"$ref": "primitive.entity.json#/definitions/string"
},
"usePhone": {
"description": "Identifies the purpose of a Phone contact point.",
"enum": [
"Alternate",
"Business - Direct",
"Business - Main",
"Home",
"Mobile",
"Work"
]
},
"useEmail": {
"description": "Identifies the purpose of an Email contact point.",
"enum": [
"Person",
"Work",
"Business"
]
}
},
"allOf": [
{
"if": {
"$ref": "1/system",
"enum": [
"Phone"
]
},
"then": {
"required": [
"usePhone"
]
}
},
{
"if": {
"$ref": "1/system",
"enum": [
"Email"
]
},
"then": {
"required": [
"useEmail"
]
}
}
]
}
}
}