Sometimes, I noticed the following JSON Schemas:
{ "type": "object", "properties": { "address": { "type": "string", "required": true } } }
vs
{ "type": "object", "properties": { "address": { "type": "string", "optional": false } } }
So what is the difference between required
vs optional
in the above example?
Required Properties By default, the properties defined by the properties keyword are not required. However, one can provide a list of required properties using the required keyword. The required keyword takes an array of zero or more strings. Each of these strings must be unique.
In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.
$id is a reserved keyword. It serves for: Declaring an identifier for the schema or subschema. Declaring a base URL against which $ref URLs are resolved.
Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum).
The IETF draft v4 of the JSON schema only defines required
and does not include optional
.
To quote the section on required
from draft v4:
Valid values: The value of this keyword MUST be an array. This array MUST have at least one element. Elements of this array MUST be strings, and MUST be unique.
Conditions for successful validation: An object instance is valid against this keyword if its property set contains all elements in this keyword's array value.
In effect, using required
makes optional all properties for which the name is not included in the given array of strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With