Reputation: 369
I am trying to validate a sample JSON file with a JSON-Schema. In this, I want to specify the password as required if the category is "Private" and type in confidential is "Customer". Otherwise, password is not required. How to compare the values in the definition at if-else-then which is not inside of that definition.
schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"user": {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {
"pattern": "(^[a-zA-Z0-9\\s]+\\b(s\\/o|d\\/o|a\\/l|a\\/p)?\\b[a-zA-Z0-9\\s]*$)",
"maximum": 255,
"minimum": 1
},
"email": {
"maximum": 100,
"format": "email"
},
"password": {
"pattern": "(^[a-zA-Z0-9\\s]+\\b(s\\/o|d\\/o|a\\/l|a\\/p)?\\b[a-zA-Z0-9\\s]*$)",
"maximum": 25,
"minimum": 8
}
}
},
"confidential": {
"type": "object",
"required": ["id", "version", "type"],
"properties": {
"id": {
"type": "integer",
"maximum": 55,
"minimum": 4
},
"version": {
"maximum": 100,
"type": "number"
},
"type": {
"enum": ["Admin", "Customer"]
}
}
}
},
"required": ["doc_number", "category"],
"properties": {
"user": {
"$ref": "#/definitions/user"
},
"confidential": {
"$ref": "#/definitions/confidential"
},
"doc_number": {
"type": "integer"
},
"category": {
"enum": ["Private", "Public"]
}
}
}
Sample of JSON code is given below
{
"doc_number":12345,
"category": "Private",
"user":{
"name":"Nikola Tesla",
"email":"[email protected]",
"password":"Rvpwv84nExp#E@c"
},
"confidential":{
"type":"Customer",
"version":2.3,
"id":784562
}
}
}
Upvotes: 1
Views: 1656
Reputation: 369
Added if/then clause in at the top level of my schema, at the same level as the "properties" and "required" keywords.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"user": {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {
"pattern": "(^[a-zA-Z0-9\\s]+\\b(s\\/o|d\\/o|a\\/l|a\\/p)?\\b[a-zA-Z0-9\\s]*$)",
"maximum": 255,
"minimum": 1
},
"email": {
"maximum": 100,
"format": "email"
},
"password": {
"pattern": "(^[a-zA-Z0-9\\s]+\\b(s\\/o|d\\/o|a\\/l|a\\/p)?\\b[a-zA-Z0-9\\s]*$)",
"maximum": 25,
"minimum": 8
}
}
},
"confidential": {
"type": "object",
"required": ["id", "version", "type"],
"properties": {
"id": {
"type": "integer",
"maximum": 55,
"minimum": 4
},
"version": {
"maximum": 100,
"type": "number"
},
"type": {
"enum": ["Admin", "Customer"]
}
}
}
},
"required": ["doc_number", "category"],
"properties": {
"user": {
"$ref": "#/definitions/user"
},
"confidential": {
"$ref": "#/definitions/confidential"
},
"doc_number": {
"type": "integer"
},
"category": {
"enum": ["Private", "Public"]
}
},
"if": {
"properties": {
"category": {
"const": "Private"
},
"confidential": {
"properties": {
"type": {
"const": "Customer"
}
}
}
}
},
"then": {
"properties": {
"user": {
"required": ["password"]
}
}
}
}
Upvotes: 0
Reputation: 53986
At the top level of your schema, at the same level as the "properties" and "required" keywords, add an if/then clause that spells out all the criteria:
required
), and its value must be a certain value (const
)required
), and its value must be a certain value (const
)Upvotes: 1