How to constrain a JSON field by a few possible values? - json

I have a field "type1" that can only have values of "1", "7" or "9". What should I add to my schema so that the requirement of only these 3 values is fullfilled?
The "type1" has this structure right now:
"type1": {
"type": String,
"pattern": "^[0-9]+$",
"maxLength": 1
}

Define it as enum to make it more simple and easier to read:
"type1": { "enum": [ "1", "7", "9" ] }
See also: Understanding JSON Schema

Related

Conditional statements do not apply in Json Schema

I would like to change the values acceptable to the Value B field according to the values of the Value A field in the following Json.
{"maximumList": [
{
"ValueA": 232,
"ValueB": ["aa"]
}]}
To do so, I obtained Schema using the Json Schema Generator and modified it to make it as follows.
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"maximumList": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"ValueA": {
"type": "integer"
},
"ValueB": {
"type": "array",
"items": [
{
"type": "string"
}
]
}
},
"required": [
"ValueA",
"ValueB"
],
"if":{
"properties":{
"ValueA" :{"const":232}
}
},
"then":{
"ValueB":{
"enum":["bbb"]
}
}
}
]
}
},
"required": [
"maximumList"
]
}
definitely set the value of the ValueB field to allow only "bbb", but even if "aaa" is entered in the value of the ValueB field, it passes the validation.
Is there anything I'm missing?
I think your problem is that you're using draft 4 of JSON Schema (identified by the $schema keyword value of http://json-schema.org/draft-04/schema#.
The if/then/else keywords are only supported in draft 7 and later. If you change the $schema value to http://json-schema.org/draft-07/schema#, it should work for you.

How to validate existence and values of some object properties based on the value of a Type object property?

I'm trying to validate JSON which is more complex than what's below, but hopefully this simpler example illustrates the issue cleanly.
Within the more complex schema, I need to have an array of objects, where:
All objects have a set of the same properties, like Name and Type
The Type property is an enum of allowed object types, such as "Full-Time", "Contractor"
Based on the value of the Type property, an additional per-type set of properties is defined. For example a "Full-Time" Employee has a Salary property which is required and an Email property which is optional, while a "Contractor" Employee has a Rate property which is required, and no optional additional properties.
Only properties which are defined should be allowed within each object
Required properties for each object may include those which are common across all objects, such as Name and Type, as well as those specific to one type of object, such as Salary
This seems like it should be a common problem, but so far I have not been able to find something which seems to match exactly what I'm trying to do. These came close, but I could not figure out exactly how to apply them to and/or which method is best for my use-case, so hoping someone who understands this area better can help:
Is it possible to have common properties before oneOf?
How to validate a JSON object against a JSON schema based on object's type described by a field?
how to set the type of a schema object based on the value of another property?
JsonSchema: Validate type based on value of another property
In particular, it's not clear how the required and additionalProperties keywords should work across the common vs per-object properties.
Schema I have so far, with unwanted duplication:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"$ref": "#/definitions/Employee"
},
"definitions": {
"Employee": {
"type": "object",
"oneOf": [{
"properties": {
"Name": { "type": "string" },
"Type": { "type": "string", "enum": [ "Full-Time" ] },
"Salary": { "type": "integer" },
"Email": { "type": "string" }
},
"required": [ "Name", "Type", "Salary" ],
"additionalProperties": false
},
{
"properties": {
"Name": { "type": "string" },
"Type": { "type": "string", "enum": [ "Contractor" ] },
"Rate": { "type": "number" }
},
"required": [ "Name", "Type", "Rate" ],
"additionalProperties": false
}]
}
}
}
To validate this data:
[
{ "Name": "Good First Employee", "Type": "Full-Time", "Salary": 100000, "Email": "first.employee#example.com" },
{ "Name": "Good Second Employee", "Type": "Full-Time", "Salary": 90000 },
{ "Name": "Good First Contractor", "Type": "Contractor", "Rate": 20.00 },
{ "Name": "Good Second Contractor", "Type": "Contractor", "Rate": 25 },
{ "Name": "Bad First Person", "Type": "Unknown", "Salary": 90000 },
{ "Name": "Bad Third Employee", "Type": "Full-Time" },
{ "Name": "Bad Fourth Employee", "Type": "Full-Time", "Rate": 49.00 },
{ "Name": "Bad Fifth Employee", "Type": "Full-Time", "Salary": 100000, "Phone": "123-123-1234" },
{ "Name": "Bad Second Person" }
]
For the data above, objects with a Name starting with "Good" are valid. The rest starting with "Bad" are invalid:
Bad First Person - invalid Type
Bad Third Employee - missing Salary
Bad Fourth Employee - missing Salary, Rate invalid for Employee
Bad Fifth Employee - Phone invalid for Employee
Bad Second Person - missing Type (a common field)
Some of this works, but with excessive error messages that don't clearly indicate where the problem is.
It IS a common problem. Sadly your unwanted duplication is the only way to make what you want happen for a draft-7 JSON Schema.
We (JSON Schema core team) did a lot of work on adding a new keyword for draft-8, but that's not published / done yet. https://github.com/json-schema-org/json-schema-spec/issues/556 - unevaluatedProperties.
The linked issue includes a lot of detail, including an example of where the OpenAPI specification has the same issue, and uses lots of deuplication as a result. The new key word cuts out lots of duplication.

How to use JSON Schema to require one of two fields

I want to validate JSON to make one of two fields manadatory.
Let's assume we have two fields (Email Address and Phone Number). I want to make sure that one of the two fields is required for the record to be valid.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "ExampleID-0212",
"title": "objectExamples",
"description": "Demo",
"type": "object",
"properties": {
"RecordObject": {
"type": "object",
"properties": {
"emailAddress": {
"type": "string"
},
"PhoneNumber": {
"type": "number"
}
}
}
},
"required": [
"RecordObject"
]
}
You need to add:
"anyOf": [
{ "required":
[ "emailAddress" ] },
{ "required":
[ "PhoneNumber" ] }
]
to the schema of RecordObject property.
It requires that at least one of fields is present. If you need exactly one field (i.e., not both) present, you need to use "oneOf" keyword (the rest should be the same).
This reference of JSON Schema keywords can be useful.
Sooperb Answer from ESP.. that is from jsonSchema ...
you can also do that validation thorugh condition..see below
if(EmailAddress == null && PhoneNumber == null){
//statements or error message response
}

Json schema "not in" enum type?

I'd like to use oneOf schemas which differ only by value of xyType property. I'd like to have two of them: one where the xyType is set to "1" and the second one where xyType is any other value. Can this be done using json schemas?
"oneOf": [
{
"properties": {
"xyType": "enum": ["1"],
"whatever" : "string"
},
"type": "object"
},
{
"properties": {
"xyType": "enum": [], /// NOT "1"?
"whatever" : "string"
},
"type": "object"
}
]
There's a not operator, and the enum keyword, and you can use them together, like
{
"not": {
"enum": ["1"]
}
}

JSON schema validation for null when "type"="string"

I wanted to prevent a json filed from allowing null as a valid value for it.
Tried using the keyword not, but no luck.
Want the below json to be validated as false, as the field stats as value as null.
{
"stats": "null"
}
please find my schema below:-
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net#",
"type": "object",
"additionalProperties": false,
"maxProperties": 1,
"properties": {
"stats": {
"id": "http://jsonschema.net/stats#",
"type": "string",
"maxLength": 5,
"minLength": 2,
"additionalProperties": false,
"maxProperties": 1,
"not": {"type": "null"}
}
},
"required": [
"stats"
]
}
Though i gave "not": {"type": "null"}, it still validated successfully.
Wow. So much confusion here.
The problem is simple:
{
"stats": "null"
}
"null" is a string, thus it’s valid (because you allow strings). This would not be allowed by your schema, which works as you expect:
{
stats: null
}
The answer from Ashish Patil is wrong: in your schema (not your data), when you are specifying the type, the type name is a string. Specifying "not": {"type": null} is not valid. You could specify "not": {"type": "null"}, but that would be redundant as the earlier "type": "string" already implies that.
The accepted answer from jruizaranguren works because it doesn’t allow the string "null". It doesn’t address the core confusion that null is not the same as "null".
First of all, null is not a String. So try using below in your schema--
"stats": {
"id": "http://jsonschema.net/stats#",
"type": "string",
"maxLength": 5,
"minLength": 2,
"additionalProperties": false,
"maxProperties": 1,
"not": {"type": null}
}
But, in the example snippet you have mentioned something like below--
{
"stats": "null"
}
So, if you really wanted null to be not allowed in your file, then your example file should look like {
"stats": null
}
Along schema i have provided.
You can use "enum" keyword instead of "type". "null" is not a valid json and json-schema type.
Also additionalProperties and maxProperties are useless within stats description.
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"id" : "http://jsonschema.net#",
"type" : "object",
"additionalProperties" : false,
"maxProperties" : 1,
"properties" : {
"stats" : {
"id" : "http://jsonschema.net/stats#",
"type" : "string",
"maxLength" : 5,
"minLength" : 2
"not" : {
"enum" : ["null"]
}
}
},
"required" : [
"stats"
]
}