JSON Schema `required` allows empty string for value - json

I'm using a JSON schema template to validate the data that is received by an online form. One of the requirements of the validator is that it allows some questions to be required based on the answers given for other questions.
For example if the question is Do you want a loan? and the user answers yes, then the answer to the question What is the loan to be used for? needs to be set to required so that the user must provide an answer. If the answer is no then the second question is not required.
I'm using definitions to define my questions, and then referencing them below in the main question schema. I read that by using the if-then-else feature provided in draft-07 I could use it to set certain questions to be required based on answers to other questions.
In this particular instance what I would like to happen is that if the user enters the answer Home improvements (General) for question 9, then question 257 will be set to required and MUST be answered, otherwise an error should be thrown.
At the moment, when I enter this validator into https://www.jsonschemavalidator.net/ it does not work as expected. What actually happens is the answer for question 257 can be left blank even if the answer to question 9 is "Home improvements (General)
How can I change my schema to give the behaviour I am trying to get?
JSON Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"question3-9": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"enum": [
"Home improvements (General)",
"Other"
]
}
}
},
"question3-257": {
"type": "object",
"properties": {
"answer": {
"type": "string",
}
}
}
},
"type": "object",
"properties": {
"form_submission": {
"type": "object",
"properties": {
"sections": {
"type": "object",
"properties": {
"3": {
"type": "object",
"properties": {
"questions": {
"type": "object",
"properties": {
"9": {
"$ref": "#/definitions/question3-9"
},
"257": {
"$ref": "#/definitions/question3-257"
}
},
"if": {
"properties": {
"9": {
"properties": {
"answer": {
"enum": [
"Home improvements (General)"
]
}
}
}
}
},
"then": {
"required": [
"257"
]
}
}
}
}
},
"required": [
"3"
]
}
}
}
}
}
JSON to be validated:
{
"form_submission": {
"sections": {
"3": {
"questions": {
"9": {
"answer": "Home improvements (General)",
},
"257": {
"answer": "",
}
}
}
}
}
}
Updated If-Then
"if": {
"properties": {
"9": {
"properties": {
"answer": {
"enum": [
"Home improvements (General)"
]
}
},
"required": ["answer"]
}
},
"required": ["9"]
},
"then": {
"257": {
"properties":{
"answer":{
"minLength": 1
}
}
}
}

Your problem here is you are expecting required to check the value of the key, which it does not.
Required from the current draft-7 specification:
An object instance is valid against this keyword if every item in the
array is the name of a property in the instance.
This means required only checks that the key exists for the object.
It is not related to the value.
For string validation, see the validation key words which are applicable to strings. I suspect you want minLength or pattern (which is regex).
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.3

Related

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.

JSON Schema Nested If Then

I cannot seem to find a working way of applying multiple if/then logic on an enum.
anyOf doesnt apply the conditional logic, but instead it says if any of them match then thats good.
allOf again doesnt apply the conditional logic but tests a superset of the properties/required fields.
Here is a JSON Schema example:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"type"
],
"properties": {
"type": {
"$id": "#/properties/type",
"enum": [
"a",
"b",
"c"
],
"title": "The Type"
},
"options": {
"$id": "#/properties/options",
"type": "object",
"title": "The Options Schema",
"oneOf": [
{
"if": { "properties": { "type": { "const": "a" } }
},
"then": {
"required": [ "option1" ],
"properties": {
"option1": {
"$id": "#/properties/options/properties/option1",
"type": "boolean",
"title": "The option1 Schema"
}
}
}
},
{
"if": { "properties": { "type": { "const": "b" } }
},
"then": {
"required": [ "option2" ],
"properties": {
"option2": {
"$id": "#/properties/options/properties/option2",
"type": "boolean",
"title": "The option2 Schema"
}
}
}
},
{
"if": { "properties": { "type": { "const": "c" } }
},
"then": {
"required": [],
"properties": {}
}
}
]
}
}
}
If you validate against this JSON:
{
"type": "a",
"options": {
"option1": true
}
}
It fails because option2 is required.
If you change it to anyOf then it succeeds, but if you change the JSON to be invalid:
{
"type": "a",
"options": {
"option2": false
}
}
It still succeeds.
I havent managed to get nested if/then/else/if/then/else working either.
How can i perform a check where I have set of properties for each type and you cannot intermingle them? Is this actually possible, or should I just change my design.
First, you can test your schemas here. There are several of these sites across the internet.
Second, the if/then/else construct was introduced to replace a oneOf for these kind of enum scenarios, not be combined with it.
This subschema
"if": { "properties": { "type": { "const": "a" } } },
"then": {
"required": [ "option1" ],
"properties": {
"option1": {
"$id": "#/properties/options/properties/option1",
"type": "boolean",
"title": "The option1 Schema"
}
}
}
doesn't actually fail when type is not a. It merely says that if type=a, apply the then subschema. It doesn't say anything about what to validate if type is not a, so it passes. If you add an else:false to this, it'll be more in line with what you're thinking, but I encourage you to think about it differently.
Use oneOf or if/then/else, but not both. I suggest changing your subschemas to use this format:
{
"properties": {
"type": { "const": "a" },
"option1": {
"$id": "#/properties/options/properties/option1",
"type": "boolean",
"title": "The option1 Schema"
}
},
"required": [ "option1" ],
}
This asserts that option1 is required and must be a boolean, and that type=a. If type is not a, this schema fails, which is what you want.
This answer describes what you need to do in a bit more detail.

Use conditional statements on json schema based on another schema object

I have a json object like:
{
"session": {
"session_id": "A",
"start_timestamp": 1535619633301
},
"sdk": {
"name": "android",
"version": "21"
}
}
The sdk name can either be android or ios. And the session_id is based on name field in sdk json. I have written a json schema using conditional statement (Using draft 7) as follows:
But it works in an unexpected manner:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/Base",
"definitions": {
"Base": {
"type": "object",
"additionalProperties": false,
"properties": {
"session": {
"$ref": "#/definitions/Session"
},
"sdk": {
"$ref": "#/definitions/SDK"
}
},
"title": "Base"
},
"Session": {
"type": "object",
"additionalProperties": false,
"properties": {
"start_timestamp": {
"type": "integer",
"minimum": 0
},
"session_id": {
"type": "string",
"if": {
"SDK": {
"properties": {
"name": {
"enum": "ios"
}
}
}
},
"then": {
"pattern": "A"
},
"else": {
"pattern": "B"
}
}
},
"required": [
"session_id",
"start_timestamp"
],
"title": "Session"
},
"SDK": {
"type": "object",
"additionalProperties": false,
"properties": {
"version": {
"type": "string"
},
"name": {
"type": "string",
"enum": [
"ios",
"android"
]
}
},
"required": [
"name",
"version"
],
"title": "SDK"
}
}
}
So the following JSON Passes:
{
"session": {
"session_id": "A",
"start_timestamp": 1535619633301
},
"sdk": {
"name": "ios",
"version": "21"
}
}
But this fails:
{
"session": {
"session_id": "B",
"start_timestamp": 1535619633301
},
"sdk": {
"name": "android",
"version": "21"
}
}
can someone explain y?.. Even this passes:
{
"session": {
"session_id": "A",
"start_timestamp": 1535619633301
},
"sdk": {
"name": "android",
"version": "21"
}
}
I think you're having a similar problem as in this question.
#Relequestual is right in that you need the properties keyword around your SDK callout. But for what you want to do, you need to reorganize.
Subschemas only operate on their level in the instance, not at the root.
Consider this schema for a simple JSON object instance containing a one and a two property:
{
"properties": {
"one": {
"enum": ["yes", "no", "maybe"]
},
"two": {
"if": {
"properties": {
"one": {"const": "yes"}
}
},
"then": {
... // do some assertions on the two property here
},
"else": {
...
}
}
}
}
The if keyword under the two property can only consider the portion of the instance under the two property (i.e. two's value). It's not looking at the root of the instance, so it can't see the one property at all.
To make it so that the subschema under the two property subschema can see the one property in the instance, you have to move the if outside of the properties keyword.
{
"if": {
"properties": {
"one": {"const" : "yes"}
}
},
"then": {
... // do some assertions on the two property here
},
"else": {
... // assert two here, or have another if/then/else structure to test the one property some more
}
}
For two possible values of one, this is pretty good. Even three possible values isn't bad. However, as the possible values of one increases, so does the nesting of ifs, which can make your schema horrible to read (and possibly make validation slower).
Instead of using the if/then/else construct, I suggest using an anyOf or oneOf where each subschema represents a valid state for the instance, given the varying values of one.
{
"oneOf": [
{
"properties": {
"one": {"const": "yes"},
"two": ... // do some assertions on the two property here
}
},
{
"properties": {
"one": {"const": "no"},
"two": ... // do some assertions on the two property here
}
},
{
"properties": {
"one": {"const": "maybe"},
"two": ... // do some assertions on the two property here
}
}
]
}
This is much cleaner in my opinion.
Hopefully that explanation helps you reconstruct your schema to allow those other instances to pass.
You have to move your conditional to a high enough level to be able to reference all of the the properties it needs to reference. In this case, that's the /definitions/Base schema. Then you just need to write your schemas properly as Relequestual explained.
{
"$ref": "#/definitions/Base",
"definitions": {
"Base": {
"type": "object",
"properties": {
"session": { "$ref": "#/definitions/Session" },
"sdk": { "$ref": "#/definitions/SDK" }
},
"allOf": [
{
"if": {
"properties": {
"sdk": {
"properties": {
"name": { "const": "ios" }
}
}
},
"required": ["sdk"]
},
"then": {
"properties": {
"session": {
"properties": {
"session_id": { "pattern": "A" }
}
}
}
},
"else": {
"properties": {
"session": {
"properties": {
"session_id": { "pattern": "B" }
}
}
}
}
}
]
},
...
}
The value of if must be a JSON Schema. If you were to take lines https://gist.github.com/Relequestual/f225c34f6becba09a2bcaa66205f47f3#file-schema-json-L29-L35 (29-35) and use that as a JSON Schema by itself, you would impose no validation constraints, because there are no JSON Schema key words at the top level of the object.
{
"SDK": {
"properties": {
"name": {
"enum": "ios"
}
}
}
}
This is allowed in the specification, because people may want to extend the functionality of JSON Schema by adding their own key words. So it's "valid" JSON Schema, but doesn't actually DO anything.
You Need to add properties to the schema for it to make sense.
{
"properties": {
"SDK": {
"properties": {
"name": {
"const": "ios"
}
}
}
}
}
Additionally, enum must be an array. When you only have a single item, you may use const.

How do I make the property of a JSON object required based upon the value of a second JSON object?

I have a JSON submission that I'm trying to validate based upon a number of rules defined in a template. This template defines a number of questions that are being asked to the user. For the submission, whether one of the question answers is required or not is conditional based upon the value of a previous question.
So basically
Do you have a dog? Yes/No
What kind of dog do you have?
The first question's valid answers are guarded using an enum, so that the user can only provide either yes or no strings as an answer to the question.
If the user answers yes to the question, I want the second question to be required, so that if the second answer is left blank when the first answer is yes then an error is raised. If the first answer is no the user is free to leave the second question blank.
Below is the JSON schema I have at the moment for this.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"question3-9": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"enum": [
"Yes", "No"
]
}
},
"if": {
"properties":{
"answer": {"enum": ["Yes"]}
}
},
"then": {"requried": "#/definitions/question3-257"}
},
"question3-257": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"minLength": 1
}
}
}
},
"type": "object",
"properties": {
"form_submission": {
"type": "object",
"properties": {
"sections": {
"type": "object",
"properties": {
"3": {
"type": "object",
"properties": {
"questions": {
"type": "object",
"properties": {
"9": {
"$ref": "#/definitions/question3-9"
},
"257": {
"$ref": "#/definitions/question3-257"
}
},
"required": [
"257"
]
}
}
}
},
"required": [
"3"
]
}
}
}
}
}
I thought that by using the if-then-else available in JSON-Schema7 I'd be able to set the second question to required, but it doesn't seem to work like this.
Here is the submission that is being validated using the above schema.
{
"form_submission": {
"sections": {
"3": {
"questions": {
"9": {
"answer": "Yes",
},
"257": {
"answer": "",
}
}
}
}
}
}
Updated JSON Schema:
"3": {
"type": "object",
"properties": {
"questions": {
"type": "object",
"properties": {
"9": {
"$ref": "#/definitions/question3-9"
},
"257": {
"$ref": "#/definitions/question3-257"
}
},
"if": {
"properties":{
"answer": {"const": "Home improvements (General)"}
}
},
"then": {"required": ["257"]}
}
}
}
To be validated:
"3": {
"questions": {
"9": {
"answer": "Home improvements (General)",
},
"257": {
"answer": "", //<-- This is an empty string but should be required since the answer to the abvoe question is "Home improvements (general) as defined with "answer": {"const": "Home improvements (General)"}
}
}
You're on the right track with the if/then keywords, but the requirement in your then isn't quite right.
The required keyword needs to be an array of strings, each of which indicates a property name; in this case, the property name is "257".
Additionally, you need to place the if/then keywords under the questions property, not in the definition. By having it in the definition, you're required that the questions.9 property has a 257 property (questions.9.257), but you want the questions.257 property to be required.
Try this instead:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"question3-9": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"enum": ["Yes", "No"]
}
}
},
"question3-257": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"minLength": 1
}
}
}
},
"type": "object",
"properties": {
"form_submission": {
"type": "object",
"properties": {
"sections": {
"type": "object",
"properties": {
"3": {
"type": "object",
"properties": {
"questions": {
"type": "object",
"properties": {
"9": {
"$ref": "#/definitions/question3-9"
},
"257": {
"$ref": "#/definitions/question3-257"
}
},
"if": {
"properties":{
"answer": {"const": "Yes"}
}
},
"then": {"required": ["257"]}
}
}
}
},
"required": ["3"]
}
}
}
}
}
Additionally, I'd suggest using the const keyword in the if. I think it reads a bit more explicitly.

Metaschema specifying required attribute for all properties

I want to customize a metaschema such that all properties are required to have an additional attribute, for example, how could I require that all properties specify a "type"?
Then this schema should fail:
{
"$schema": "http://json-schema.org/schema#",
"title": "...",
"description": "...",
"type": "object",
"properties": {
"name": {
"description": "..."
}
}
}
But this one should succeed:
{
"$schema": "http://json-schema.org/schema#",
"title": "...",
"description": "...",
"type": "object",
"properties": {
"name": {
"description": "...",
"type": "string"
}
}
}
Unfortunately, writing meta-schemas is not easy. It's being worked on, but there's no good solution yet.
You would have to make a copy of the meta-schema you want to extend and then add "required": ["type"].
But, while we're here, maybe I can convince you not to do this. Making the type keyword required causes problems in some cases. Here are a few examples. https://github.com/json-schema/json-schema/issues/172#issuecomment-124214534
EDIT
After discussing this further, we found that this particular case doesn't have the problems we normally run into with extending meta-schemas because it doesn't need to be recursive. Here is an example of extending the draft-06 schema to include a new keyword called custom which is a boolean and is required only at the top level of a properties schema.
{
"allOf": [
{ "$ref": "http://json-schema.org/draft-06/schema#" },
{
"properties": {
"properties": {
"patternProperties": {
".*": {
"properties": {
"custom": { "type": "boolean" }
},
"required": ["custom"]
}
}
}
}
}
]
}
And here's an example schema that conforms to this meta-schema.
{
"properties": {
"foo": {
"custom": true,
"not": { "type": "string" }
}
}
}
The custom keyword is required for the "foo" schema, but not the not schema or the top level schema.