JSON Schema Validation for JSON Object Values

Q

What validation keywords I can use in JSON Schema to specifically validate JSON Object values?

✍: FYIcenter.com

A

The current JSON Schema specification supports the following validation keywords to specifically validate JSON Object values. They are not applied if the JSON instance is not a JSON Object.

"maxProperties" - If the JSON instance is an object, the number of its properties must be less than or equal to the given value. For example:

JSON Schema: 
{"maxProperties": 3}

Valid JSON instance:
{"name": "Joe", "age": 25, "role": "user"}

Valid JSON instance:
[1,2,3,4,5]

Invalid JSON instance:
{"name": "Joe", "age": 25, "role": "user", "status": "new"}

"minProperties" - If the JSON instance is an object, the number of its properties must be greater than or equal to the given value. For example:

JSON Schema: 
{"minProperties": 3}

Valid JSON instance:
{"name": "Joe", "age": 25, "role": "user"}

Valid JSON instance:
[1,2,3,4,5]

Invalid JSON instance:
{"name": "Joe", "age": 25}

"required" - If the JSON instance is an object, it must have properties listed in the given array. For example:

JSON Schema: 
{"required": 
    ["name", "status"]
}

Valid JSON instance:
{"name": "Joe", "age": 25, "status": "new"}

Valid JSON instance:
[1,2,3,4,5]

Invalid JSON instance:
{"age": 25, "role": "user", "status": "new"}

"properties" - If the JSON instance is an object, its properties must be valid against the schema in the given object of the same property name. For example:

JSON Schema: 
{"properties": 
    {"name": {"type": "string"},
	 "age": {"type": "number"}
	}
}

Valid JSON instance:
{"name": "Joe", "age": 25, "status": "new"}

Valid JSON instance:
[1,2,3,4,5]

Invalid JSON instance:
{"age": "25", "role": "user", "status": "new"}

"patternProperties" - If the JSON instance is an object, its properties must be valid against the schema in the given object of the property name with matching regular expression.

JSON Schema: 
{"patternProperties": 
    {".*Name": {"type": "string"}}
}

Valid JSON instance:
{"firstName": "Joe", "lastName": "Smint", "age": 25}

Valid JSON instance:
[1,2,3,4,5]

Invalid JSON instance:
{"firstName": "Joe", "lastName": "Smint", "midName": null}

"additionalProperties" - If the JSON instance is an object, its properties, that could not find its schema in "properties" and "patternProperties" keywords, must be valid against the given schema. For example:

JSON Schema: 
{"properties": 
    {"name": {"type": "string"},
	 "age": {"type": "number"}
	},
 "additionalProperties": {"type": "boolean"}
}

Valid JSON instance:
{"name": "Joe", "age": 25, "isSingle": true, "hasJob": true}

Valid JSON instance:
[1,2,3,4,5]

Invalid JSON instance:
{"name": "Joe", "age": 25, "status": "new"}

"additionalProperties" specifies a schema for all properties of the JSON Object instance, if "properties" and "patternProperties" are not specified. For example:

JSON Schema: 
{"additionalProperties": {"type": "number"}
}

Valid JSON instance:
{"age": 25, "height": 1.80, "weight": 80}

Valid JSON instance:
[1, 2, 3]

Invalid JSON instance:
{"name": "Joe", "age": 25, "status": "new"}

"dependencies" - If the JSON instance is an object and it has a property that matches a given property, that property must satisfy the given dependency condition.

If the a schema is given in a "dependencies" property, and the same property exists in the JSON Object instance, the entire JSON object instance must be valid against the given schema. For example:

JSON Schema: 
{"dependencies": 
    {"isFullAddress": {"minProperties": 5}
    }
}

Valid JSON instance:
{"isFullAddress": true,
 "street": "1 Road",
 "city": "London",
 "country": "UK",
 "postCode": "LKJHGF"
}

Valid JSON instance:
[1, 2, 3]

Invalid JSON instance:
{"isFullAddress": false,
 "city": "London",
 "country": "UK"
}

If the an array is given in a "dependencies" property, and the same property exists in the JSON Object instance, all elements in the given schema must be exists as properties in the JSON Object instance. For example:

JSON Schema: 
{"dependencies": 
    {"street": ["city", "country"]
    }
}

Valid JSON instance:
{"isFullAddress": true,
 "street": "1 Road",
 "city": "London",
 "country": "UK",
 "postCode": "LKJHGF"
}

Valid JSON instance:
[1, 2, 3]

Invalid JSON instance:
{"isFullAddress": false,
 "street": "1 Road",
 "city": "London",
 "postCode": "LKJHGF"
}

"propertyNames" - If the JSON instance is an object, all property names of the object must be valid against the given schema. Note all property names are considered as strings. So the most commonly used schema for "propertyNames" is the "pattern" validation. For example:

JSON Schema: 
{"propertyNames": {"pattern": "^[A-Z]"}
}

Valid JSON instance:
{"Street": "1 Road",
 "City": "London",
 "Country": "UK",
 "PostCode": "LKJHGF"
}

Valid JSON instance:
[1, 2, 3]

Invalid JSON instance:
{"city": "London",
 "country": "UK"
 "1stName": "Joe"
}

 

Introduction of JSON Schema

⇒⇒JSON Tutorials

2017-09-01, 159👍, 0💬