Properties based on enum value in JSON Schema - json

I'm building a json schema definition which has a fixed set of controls that I've currently limited with an enum. However, not all properties are relevant for all controls.
I only want to require an options property if the controlType = dropdown
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"controlType": {
"type": "string",
"enum": ["title", "dropdown", "button"]
},
"options:": {
"type": "array",
"items": {"type": "string"}
}
}
}
}
How can I conditionally include / require a field in a json schema?

Use IF..Then..Else new in Draft-07
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"controlType": {
"type": "string",
"enum": ["title", "dropdown", "button"]
},
"options:": {
"type": "array",
"items": {"type": "string"}
}
},
"if": {
"properties": {
"controlType": {"const": "dropdown"}
}
},
"then": {
"required": ["options"]
}
}
}
Use oneOf or anyOf
This can be useful if you have a property that has a limited number of acceptable values (such as an enum), but each possible value needs to be individually mapped.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"controlType": {
"type": "string",
"enum": ["title", "dropdown", "button"]
},
"options:": {
"type": "array",
"items": {"type": "string"}
}
},
"anyOf": [
{
"properties": {
"controlType": {"const": "dropdown"}
},
"required": ["controlType", "options"]
},
{
"properties": {
"controlType": {"const": "title"}
},
"required": ["controlType"]
},
{
"properties": {
"controlType": {"const": "button"}
},
"required": ["controlType"]
}
]
}
}
Further Reading
Conditionally require attribute
Applying subschemas conditionally
Combining schemas

Related

Json schema permits override of fields in objects in array

I have an json that looks like this
"List": {
{"Color": "red"},
{}
},
"Color": "grey"
}
whereas it means that the default color is grey, and the object in the list could override this Color.
The schema should allow the json to pass as long as default color(the property in the same level of List) is present. If not, it shall only allow the json to pass the schema check if all items in the list have specified a "Color".
May I know how can I write a json schema that does this check? I am aware of anyOf but I don't think it can check for all items in the array.
I tried
{
"type": "object",
"properties": {
"List": {"type": "array", "items": {"$ref:" "#/definitions/Item"}},
"Color": {"type": "string"}
},
"definitions": {
"Item": {"type: "object", "properties": " {"Color": {"type": "string"}}}
},
"anyOf": {
{
"type": "object",
"required": ["Color"]
},
{
"type": "object",
"List": {
"type": "array",
"items": {"$ref": "#/definitions/Item", "required": ["Color"]}
}
}
}
But it does not seem that the required color for the anyOf[1] is picked up by the validator.
Please help.! Thank you.
The schema in the other answer is correct, but is unnecessarily complicated. Here's an example that removes duplication and make the schema easier to read.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"List": {
"type": "array",
"items": {
"type": "object",
"properties": {
"Color": { "type": "string" },
"Shape": { "type": "string" }
}
}
},
"Color": { "type": "string" },
"Shape": { "type": "string" }
},
"required": ["List"],
"allOf": [
{ "$ref": "#/definitions/color-required-if-no-default-color" },
{ "$ref": "#/definitions/shape-required-if-no-default-shape" }
],
"definitions": {
"color-required-if-no-default-color": {
"anyOf": [
{ "required": ["Color"] },
{
"properties": {
"List": {
"items": { "required": ["Color"] }
}
}
}
]
},
"shape-required-if-no-default-shape": {
"anyOf": [
{ "required": ["Shape"] },
{
"properties": {
"List": {
"items": { "required": ["Shape"] }
}
}
}
]
}
}
}
I think I found out a way:
Schema is like below
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {"Color": {"type":"string"}, "Shape": {"type": "string"}},
"allOf":[
{
"anyOf": [
{
"required": ["Color"],
"properties": {"List": {"type": "array", "items": {"$ref": "#/definitions/Item"}}}
},
{
"properties": {"List": {"type": "array", "items": {"$ref": "#/definitions/ColorItem"}}}
}
]
},
{
"anyOf": [
{
"required": ["Shape"],
"properties": {"List": {"type": "array", "items": {"$ref": "#/definitions/Item"}}}
},
{
"properties": {"List": {"type": "array", "items": {"$ref": "#/definitions/ShapeItem"}}}
}
]
}
],
"required": ["List"],
"definitions": {
"Item": {
"type": "object",
"properties": {}
},
"ColorItem": {
"allOf": [{"$ref": "#/definitions/Item"},
{"properties": {"Color": {"type": "string"}}, "required": ["Color"]}]
},
"ShapeItem": {
"allOf": [{"$ref": "#/definitions/Item"},
{"properties": {"Shape": {"type": "string"}}, "required": ["Shape"]}]
}
}
}
Basically this does what I want. So it will pass the json only if either there is a Color/Shape read from top-level json, or we can find it in the array.

why doesn't json schema validate definitions defined in required attribute

I'm trying to create a json schema that validates an object depending on its type. It picks the right definition, however, it doesn't validate the required attributes in the selected definition. Here is the json schema i am trying:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"literal": {
"type": "object",
"properties": {
"raw": { "type": "string" }
},
"required": ["raw"],
"additionalProperties": false
},
"identifier": {
"type": "object",
"properties": {
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
},
"type": "object",
"oneOf": [
{
"type": "object",
"properties": {
"type": {
"enum": ["Literal"]
},
"content": { "$ref": "#/definitions/literal" }
}
},
{
"type": "object",
"properties": {
"type": {
"enum": ["Identifier"]
},
"content": { "$ref": "#/definitions/identifier" }
}
}
],
"required": ["type"]
};
the following schema is valid, even tho its missing the "raw" property:
{ "type" : "Literal" }
thanks
There is no keyword content in JSON Schema spec.
Once you have asserted "type":"object" in root schema, there is no need to do it again in subschema.
In order to combine object type enumerated value with associated extended definition, you need allOf keyword.
Also in definitions if you use "additionalProperties": false you have to list all properties of the object (see "type": {}). For previously defined/validated properties you can just use permissive schema: {} or true.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"literal": {
"properties": {
"type": {},
"raw": { "type": "string" }
},
"required": ["raw"],
"additionalProperties": false
},
"identifier": {
"properties": {
"type": {},
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
},
"type": "object",
"oneOf": [
{
"allOf": [
{
"properties": {
"type": {
"enum": ["Literal"]
}
}
},
{"$ref": "#/definitions/literal"}
]
},
{
"allOf": [
{
"properties": {
"type": {
"enum": ["Identifier"]
}
}
},
{"$ref": "#/definitions/identifier" }
]
}
],
"required": ["type"]
}

JsonSchema: Validate type based on value of another property

I am using the following schema to validate my json:
{
"$schema": "http://json-schema.org/schema#",
"title": " Rules",
"description": "Describes a set of rules",
"type": "object",
"properties": {
"rules": {
"type": "array",
"items": {
"type": "object",
"properties": {
"precedence": {
"type": "number",
"minimum": 0
},
"conditions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field": {
"type": "string",
"enum": [ "Name", "Size" ]
},
"relation": {
"type": "string",
"enum": [ "is", "is not", "is not one of", "is one of" ]
},
"value": {
"type": ["array", "string", "number"]
}
},
"required": ["field", "relation", "value"],
"additionalProperties": false
}
}
},
"required": ["precedence", "conditions"],
"additionalProperties": false
}
}
},
"required": ["rules"],
"additionalProperties": false
}
I want to set up a dependency to validate that when the value of the relation property has the value is one of or the value is not one of, then the type of the value property can only be array
For example, the following json should not validate because it uses the relation value is not one of and the value property is not an array:
{
"rules": [{
"precedence": 0,
"conditions": [{
"field": "Name",
"relation": "is not one of",
"value": "Mary"
}
]
}
]
}
Is it possible to set up dependencies to validate this way?
The best way to solve these kinds of problems is to separate the complex validation from the rest of the schema using definitions and include it with an allOf. In this solution, I use implication to enforce the validation.
{
"type": "object",
"properties": {
"rules": {
"type": "array",
"items": { "$ref": "#/definitions/rule" }
}
},
"required": ["rules"],
"definitions": {
"rule": {
"type": "object",
"properties": {
"precedence": { "type": "number", "minimum": 0 },
"conditions": {
"type": "array",
"items": { "$ref": "#/definitions/condition" }
}
},
"required": ["precedence", "conditions"]
},
"condition": {
"type": "object",
"properties": {
"field": { "enum": ["Name", "Size"] },
"relation": { "enum": ["is", "is not", "is not one of", "is one of"] },
"value": { "type": ["array", "string", "number"] }
},
"required": ["field", "relation", "value"],
"allOf": [{ "$ref": "#/definitions/array-condition-implies-value-is-array" }]
},
"array-condition-implies-value-is-array": {
"anyOf": [
{ "not": { "$ref": "#/definitions/is-array-condition" } },
{ "$ref": "#/definitions/value-is-array" }
]
}
"is-array-condition": {
"properties": {
"relation": { "enum": ["is not one of", "is one of"] }
},
"required": ["relation"]
},
"value-is-array": {
"properties": {
"value": { "type": "array" }
}
}
}
}
If you are able to use the latest draft-7 version of JSON Schema, you can use if then else, as per https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-00#section-6.6
Although, using oneOf is also a valid approach, it might not be as clear to someone else inspecting your schema at a later date.
I've copied an example from an answer to another question:
If the "foo" property equals "bar", Then the "bar" property is
required
{
"type": "object",
"properties": {
"foo": { "type": "string" },
"bar": { "type": "string" }
},
"if": {
"properties": {
"foo": { "enum": ["bar"] }
}
},
"then": { "required": ["bar"] }
}
(You may want to check the draft support of the library you are using.)
There may be a more concise way to do this, but this will work:
{
"$schema": "http://json-schema.org/schema#",
"title": "Rules",
"description": "Describes a set of rules",
"definitions": {
"field": {
"type": "string",
"enum": ["Name", "Size"]
}
},
"type": "object",
"properties": {
"rules": {
"type": "array",
"items": {
"type": "object",
"properties": {
"precedence": {
"type": "number",
"minimum": 0
},
"conditions": {
"type": "array",
"items": {
"type": "object",
"oneOf": [
{
"properties": {
"field": {
"$ref": "#/definitions/field"
},
"relation": {
"type": "string",
"enum": ["is", "is not"]
},
"value": {
"type": ["string", "number"]
}
},
"required": ["field", "relation", "value"],
"additionalProperties": false
},
{
"properties": {
"field": {
"$ref": "#/definitions/field"
},
"relation": {
"type": "string",
"enum": ["is not one of", "is one of"]
},
"value": {
"type": ["array"]
}
},
"required": ["field", "relation", "value"],
"additionalProperties": false
}
]
}
}
},
"required": ["precedence", "conditions"],
"additionalProperties": false
}
}
},
"required": ["rules"],
"additionalProperties": false
}

AJV schema validation error

I have the input json like below,
{"contents":[{"type":"field"},{"type":"field","itemId":"594b9980e52b5b0768afc4e8"}]}
the condition is,
if the type is 'field', then 'itemId' should be the required field
and if the type is 'fieldGroup' or 'subSection', then 'itemId' is optional
This is the Json Schema I tried and its not working as expected,
"type": "object",
"additionalProperties": false,
"properties" : {
"contents" : {
"type" : "array",
"items": {"$ref": "#displayItem" }
}
},
"definitions": {
"displayItem" : {
"id": "#displayItem",
"type": "object",
"items": {
"anyOf": [
{"$ref": "#fieldType"},
{"$ref": "#fieldGroupSubSectionType"}
]
}
},
"fieldType" : {
"id": "#fieldType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": ["field"]
}
}
},
"fieldGroupSubSectionType" : {
"id": "#fieldGroupSubSectionType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": [ "string", "null" ]
},
"type": {
"type": "string",
"enum": [
"fieldGroup",
"subSection"
]
}
}
}
}
Any help / workaround with Sample Json Schema to achieve the above use case is appreciated.
If I understand the description of what you want correctly, then the json example you provide is not valid since it has a type: "field" but does not have an "itemId" property.
Assuming that is true. Instead of using
type: ["string", null]
use the required property.
I changed your schema a bit, instead of having separate definitions I inlined them, but other than that (and the use of required) is the same:
{
"type": "object",
"additionalProperties": false,
"properties": {
"contents": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"field"
]
}
},
"required": [
"itemId"
]
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"fieldGroup",
"subSection"
]
}
}
}
]
}
}
}
}
Here is your answer with a little cleanup for best practices and style. The trick is that you need to use implication "a implies b <=> (not a) or b". In this case you have "type = field implies itemId is required <=> type is not field or itemId is required".
{
"type": "object",
"properties": {
"contents": {
"type": "array",
"items": { "$ref": "#/definitions/displayItem" }
}
},
"definitions": {
"displayItem": {
"type": "object",
"properties": {
"itemId": { "type": "string" },
"type": { "enum": ["field", "fieldGroup", "subSection"] }
},
"anyOf": [
{ "not": { "$ref": "#/definitions/fieldType" } },
{ "required": ["itemId"] }
]
},
"fieldType": {
"properties": {
"type": { "enum": ["field"] }
}
}
}
}

Conditional validation in JSON schema on nested field

I tried searching, but I'm not quite sure how to put this in words! The point of confusion is how "required" works in JSON schema v4.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"prop1": {
"type": "array",
"items": {
"type": "object",
"properties": {
"A": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
enum:["abc","bcd"]
}
},
"required": [
"name"
]
}
}
},
"required": [
"A"
]
}
},
"prop2": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field": {
"type": "string"
}
},
"required": [
"field"
]
}
}
},
"required": [
"prop1"
]
}
Here I want to set a rule that if prop1.name=="abc" then only prop2 is required otherwise prop2 is optional, how to do that ?