ajv conditional schema validation based on data - json

I would like to specify a regexp pattern for one field based on the data in another. Is this possible? I've tried switch and $data but not sure how to use them.
for example, if data looks like:
{
"contacts":[
{
"mode":"Email",
"contact":"john.doe#abc.com"
},
{
"mode":"Phone",
"contact":"111-555-1234"
}
]
}
and schema looks something like:
"$schema":"http://json-schema.org/draft-04/schema#",
"type":"object",
"properties":{
"Contacts":{
"type":"array",
"minItems":1,
"items":{
"type":"object",
"properties":{
"mode":{
"type":"string",
"enum":[
"Email",
"Phone"
]
},
"contact":{
"type":"string",
"pattern":"?????"
}
},
"required":[
"mode",
"contact"
]
}
}
}
}
How can I set the pattern of contact based on data in mode, so that if mode is Email, it validates contact against a regexp for an email format, and if mode is Phone, it validates contact against a regexp for a phone format? I have the regexp for each. I need the logic to choose one or the other.

There are several ways to do it
anyOf (pros: draft-04 compatible, cons: error reporting is a bit verbose - you will get errors from both subschemas if none matches):
{
"type": "object",
"properties": {
"Contacts": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"anyOf": [
{
"properties": {
"mode": {"enum": ["Email"]},
"contact": {
"type": "string",
"format": "email"
}
}
},
{
"properties": {
"mode": {"enum": ["Phone"]},
"contact": {
"type": "string",
"pattern": "phone_pattern"
}
}
}
],
"required": ["mode", "contact"]
}
}
}
}
if/then/else (available in ajv-keywords package, pros: error reporting makes more sense, accepted to be included in draft-07, cons: not standard at the moment):
{
"type": "object",
"properties": {
"Contacts": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"mode": {"type": "string", "enum": ["Email", "Phone"]},
"contact": {"type": "string"}
},
"if": {
"properties": {
"mode": {"enum": ["Email"]}
}
},
"then": {
"properties": {
"contact": {"format": "email"}
}
},
"else": {
"properties": {
"contact": {"pattern": "phone_pattern"}
}
}
"required": ["mode", "contact"]
}
}
}
}
select (available in ajv-keywords package, pros: more concise than if/then/else, particularly if there are more than two possible values, cons: not on the standard track yet, but you can support it :), requires enabling $data reference and Ajv v5.x.x):
{
"type": "object",
"properties": {
"Contacts": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"mode": {"type": "string"},
"contact": {"type": "string"}
},
"select": { "$data": "0/mode" },
"selectCases": {
"Email": {
"properties": {
"contact": {"format": "email"}
}
},
"Phone": {
"properties": {
"contact": {"pattern": "phone_pattern"}
}
}
},
"selectDefault": false,
"required": ["mode", "contact"]
}
}
}
}
I prefer the last option.

Related

Unable to constrain properties to specific object with oneOf

I have 3 kinds of product definitions. I want to express choose each product based on its properties. I define the products and then use a oneOf to conditionally pick the product. However, the attributes for the other products are not constrained and can be placed in the json they shouldn't be allowed in. Here is my attempt at displaying the selected product.
{
"type": "object",
"required": [
"type",
"productId"
],
"additionalProperties": false,
"properties": {
"productId": {
"type": "string",
"minLength": 1
},
"type": {
"enum": ["product"]
},
"productInfo": {
"type": "object",
"additionalProperties": false,
"required": [ "type", "gameMode"],
"properties": {
"type": {
"enum": ["digital"]
},
"gameMode": {
"type": "object",
"oneOf": [
{ "$ref": "#/$defs/audioPlayer" },
{ "$ref": "#/$defs/multiPlayer" },
{ "$ref": "#/$defs/console" },
{ "$ref": "#/$defs/controller" }
]
}
}
}
},
"$defs": {
"audioPlayer": {
"type": "object",
"gameMode": {
"enum": ["mp3", "wav"]
}
},
"controller": {
"type": "object",
"properties": {
"mode": {
"enum": ["Retro","XboxElite" ]
},
"batteryLevel": { "type": "string" },
"warranty": { "type": "string" },
"tradeIn": { "type": "boolean" }
}
},
"multiPlayer": {
"type": "object",
"properties": {
"mode": {
"enum": ["LiveXBox","FortNite"]
},
"signUp": {
"type": "object",
"properties": {
"url": { "type": "string" }
}
}
}
},
"console": {
"type": "object",
"properties": {
"mode": {
"enum": ["single", "multi"]
},
"required": {
"type": "boolean"
}
}
}
}
}
This is the JSON that validates successfully against the schema, which is undesirable. I only want the properties to be based on the product.
//This product has properties it shouldn't have
// signUp should be only allowed in multiplayer etc.
{
"type": "product",
"productId": "wwwwww",
"productInfo": {
"type": "digital",
"gameMode": {
"mode": "mp3",
"required": true,
"signUp": "https://mmmmmmm.com",
"warranty": "yes"
}
}
}
The correct examples should be:
// Audio Player properties only
{
"type": "product",
"productId": "audio",
"productInfo": {
"type": "digital",
"gameMode": {
"mode": "mp3", // or "wav"
}
}
}
// Controller properties only
{
"type": "product",
"productId": "controller",
"productInfo": {
"type":"digital",
"gameMode": {
"mode": "Retro", // or XboxElite
"batteryLevel": "high",
"warranty": "yes",
"tradeIn": true
}
}
}
First of all, your $defs/audioPlayer schema is missing the properties keyword. That's not the problem, but I wanted to point it out so it doesn't confuse you later.
The actual problem is that you're missing the "additionalProperties": false constraint on your gameMode types. Your first example doesn't fail because additional properties are ignored by default just like any other schema.

Validate JSON Schema based on "type" property

I have the following JSON which validates against my current schema:
{
"information": [
{
"value": "closed",
"type": "power"
},
{
"value": "on",
"type": "door"
}
]
}
However, I want this validation to fail (since open/closed should relate to door, and on/off should only relate to power.
How can I tie the two together?
My current working schema:
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"Type": {
"type": "string",
"enum": ["power", "door"]
},
"Power": {
"type": "string",
"enum": ["on", "off"]
},
"Door": {
"type": "string",
"enum": ["open", "closed"]
},
"InformationField": {
"type": "object",
"required": [ "type", "value" ],
"properties": {
"label": { "type": "string" },
"value": {
"anyOf": [
{ "$ref": "#/definitions/Power"},
{ "$ref": "#/definitions/Door"}
]
},
"type": { "$ref": "#/definitions/Type" }
}
},
"Information": {
"type": "array",
"items": { "$ref": "#/definitions/InformationField" }
},
},
"properties": {
"information": { "$ref": "#/definitions/Information" }
},
}
I have a lot of types, so I want to do this in the cleanest way possible.
Here is one of my attempts, which fails to validate correctly:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"Type": {
"type": "string",
"enum": ["power", "door"]
},
"Power": {
"type": "string",
"enum": ["on", "off"]
},
"Door": {
"type": "string",
"enum": ["open", "closed"]
},
"InformationField": {
"type": "object",
"required": [ "label", "value" ],
"properties": {
"label": { "type": "string" },
"type": { "$ref": "#/definitions/Type" }
},
"anyOf": [
{
"if": {
"properties": { "type": { "const": "power" } }
},
"then": {
"properties": { "value": { "$ref": "#/definitions/Power" } }
}
},
{
"if": {
"properties": { "type": { "const": "door" } }
},
"then": {
"properties": { "value": { "$ref": "#/definitions/Door" } }
}
}
]
},
"Information": {
"type": "array",
"items": { "$ref": "#/definitions/InformationField" }
},
},
"properties": {
"information": { "$ref": "#/definitions/Information" }
},
}
(Validates, even though it shouldn't ...)
Changed anyOf to allOf in my attempt, which fixes validation.
My reasoning as to why this works:
From the anyOf JSON schema spec:
5.5.4.2. Conditions for successful validation
An instance validates successfully against this keyword if it validates successfully against at least one schema defined by this keyword's value.
If my first if condition is false, then it doesn't evaluate the then, and is therefore valid.
Using allOf evaluates every condition's validation (not the condition itself).

JSON Schema if-else condition complex scenario

{
"policyHolder": {
"fullName": "A"
},
"traveller": [
{
"fullName": "B",
"relationship": "Spouse"
},
{
"fullName": "A",
"relationship": "My Self"
}
]
}
In above json, I want to validate that
if "relationship" = "My Self" then fullName must match the fullName in policyHolder
A field relationship must exist in traveller array, else json is invalid
I have tried to create a json schema with if-else, allOf, etc. but nothing works which can do these validations but not able to.
Please help!!
Schema:
{
"type": "object",
"required": [
"policyHolder",
"traveller",
],
"properties": {
"policyHolder": {
"$id": "#/properties/policyHolder",
"type": "object",
"required": [
"fullName"
],
"properties": {
"fullName": {
"$id": "#/properties/policyHolder/properties/fullName",
"type": "string",
}
}
},
"traveller": {
"$id": "#/properties/traveller",
"type": "array",
"minItems": 1,
"items": {
"$id": "#/properties/traveller/items",
"type": "object",
"properties": {
"fullName": {
"$ref": "#/properties/policyHolder/properties/fullName"
},
"relationship": {
"$id": "#/properties/traveller/items/properties/relationship",
"type": "string",
}
},
"required": [
"fullName",
"relationship"
],
}
}
}
}```
It's your first requirement that you're going to have the most trouble with. JSON Schema doesn't support validation of data against data elsewhere in the instance. It's a highly discussed topic, but nothing has been adopted yet. I suggest you verify this with a little code.
For the second, I would suggest you extract some of your subschemas into definitions rather than trying to muck about with IDs. IDs are typically more beneficial if you're referencing them from other documents or if you use short (like single-word) IDs. Defining the ID as its location in the document is redundant; most processors will handle this automatically.
{
"type": "object",
"required": [
"policyHolder",
"traveller",
],
"definitions": {
"person": {
"type": "object"
"properties": {
"fullName": {"type": "string"}
},
"required": ["fullName"]
},
"relationship": { "enum": [ ... ] } // list possible relationships
},
"properties": {
"policyHolder": { "$ref": "#/definitions/person" },
"traveller": {
"type": "array",
"minItems": 1,
"items": {
"allOf": [
{ "$ref": "#/definitions/person" },
{
"properties": {
"relationship": { "$ref": "#/definitions/relationship" }
},
"required": ["relationship"]
}
]
}
}
}
}
(I extracted the relationship into its own enum definition, but this is really optional. You can leave it inline, or even an unrestricted string if you don't have a defined set of relationships.)
This can't currently be done with JSON Schema. All JSON Schema keywords can only operate on one value at a time. There's a proposal for adding a $data keyword that would enable doing this kind of validation, but I don't think it's likely to be adopted. $data would work like $ref except it references the JSON being validated rather than referencing the schema.
Here's what how you would solve your problem with $data.
{
"type": "object",
"properties": {
"policyHolder": {
"type": "object",
"properties": {
"fullName": { "type": "string" }
}
},
"traveler": {
"type": "array",
"items": {
"type": "object",
"properties": {
"fullName": { "type": "string" },
"relationship": { "type": "string" }
},
"if": {
"properties": {
"relationship": { "const": "My Self" }
}
},
"then": {
"properties": {
"fullName": { "const": { "$data": "#/policyHolder/fullName" } }
}
}
}
}
}
}
Without $data, you will have to do this validation in code or change your data structure so that it isn't necessary.

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
}

JSON-Schema: Conditional dependency (by value)

This is the simplified JSON-Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "user",
"type": "object",
"properties": {
"account": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["COMPANY", "PERSON"]
}
},
"required": ["type"]
},
"person": {
"type": "object",
"properties": {
"firstName": { "type": "string" },
"lastName": { "type": "string" }
},
"required": ["firstName", "lastName"]
},
"company": {
"type": "object",
"properties": {
"name": { "type": "string" },
"taxNumber": { "type": "string" }
}
}
},
"required": ["account", "person"]
}
What I want to achieve is:
If account.type is set to "COMPANY":
company object and its properties should be required.
If account.type isset to "PERSON":
company object should be optional.
But if company object is present, company.name and company.taxNumber should be required.
This can be achieved by defining two long sub-schemas under a oneOf but that would mean too many duplicates and a complex schema, since account and company has many more properties than this simplified version.
AFAIK, the only way to define a specific value in a schema is by using the enum keyword with a single item. I tried this with the dependencies keyword but didn't help.
Can you think of a way without altering the structure of the data object?
You can express this requirement using switch keyword from JSON-schema v5/6 proposals that is supported in Ajv (I am the author).
Under Draft-07, you can do it by conditionally applying schema requirements:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "user",
"type": "object",
"properties": {
"account": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["COMPANY", "PERSON"]
}
},
"required": ["type"]
},
"person": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
},
"required": ["firstName", "lastName"]
},
"company": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"taxNumber": {
"type": "string"
}
}
}
},
"if": {
"properties": {
"account": {
"const": {
"type": "COMPANY"
}
}
}
},
"then": {
"required": ["account", "person", "company"]
},
"else": {
"required": ["account", "person"]
}
}
Here's a working example that validates against it:
{
"account": {
"type": "COMPANY"
},
"person": {
"firstName": "John",
"lastName": "Doe"
},
"company": {
"name": "XYZ Corp"
}
}