Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a JSON schema that requires at least one of many properties

I would like to know if I can define a JSON schema (draft 4) that requires at least one of many properties possible for an object. I already know of allOf, anyOf and oneOf but just can't figure out how to use them in the way I want.

Here are some example JSON to illustrate :

// Test Data 1 - Should pass {      "email": "[email protected]",     "name": "John Doe" } // Test Data 2 - Should pass {     "id": 1,     "name": "Jane Doe" } // Test Data 3 - Should pass {     "id": 1,     "email": "[email protected]",     "name": "John Smith" } // Test Data 4 - Should fail, invalid email {     "id": 1,     "email": "thisIsNotAnEmail",     "name": "John Smith" }  // Test Data 5 - Should fail, missing one of required properties {     "name": "John Doe" } 

I would like to require at least id or email (also accepting both of them) and still pass validation according to format. Using oneOf fails validation if I provide both (test 3), anyOf passes validation even if one of them is not valid (test 4)

Here is my schema :

{     "$schema": "http://json-schema.org/draft-04/schema#",     "id": "https://example.com",     "properties": {         "name": {             "type": "string"         }     },     "anyOf": [         {             "properties": {                 "email": {                     "type": "string",                     "format": "email"                 }             }         },         {             "properties": {                 "id": {                     "type": "integer"                 }             }         }     ] } 

Can you help me how to achieve correct validation for my use case ?

like image 708
MaxiWheat Avatar asked Aug 05 '15 17:08

MaxiWheat


People also ask

What is used in a JSON Schema to specify the minimum number of items that must be in the array?

To set the minimum # of item in an array, use the "minItems" .

What does exclusive minimum property in JSON Schema mean?

Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum).

What is JSON Schema properties?

Properties. The properties (key-value pairs) on an object are defined using the properties keyword. The value of properties is an object, where each key is the name of a property and each value is a schema used to validate that property.

How do I specify a schema in a JSON file?

If you want to use a custom schema of your own that's part of your project, just click on your schema in Solution Explorer and then drag it to that dropdown list. Alternatively, you can use the $schema keyword in a JSON file to associate it with a schema.


2 Answers

To require at least one of a set of properties, use required inside a series of anyOf options:

{     "type": "object",     "anyOf": [         {"required": ["id"]},         {"required": ["email"]}         // any other properties, in a similar way     ],     "properties": {         // Your actual property definitions here     } { 

If any of the properties you want is present ("id", "email"), then it will pass the corresponding option in allOf.

like image 66
cloudfeet Avatar answered Sep 21 '22 03:09

cloudfeet


You may use minProperties: number (and maxProperties: number if needed). That would shorten the schema definition:

{      type: "object",      minProperties: 1,      properties: [/* your actual properties definitions */],      additionalProperties: false } 

Link to documentation: https://json-schema.org/understanding-json-schema/reference/object.html#size

like image 33
NBR Avatar answered Sep 21 '22 03:09

NBR