Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "required" vs "optional" in JSON Schema

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?

like image 554
Cory Avatar asked Apr 25 '13 01:04

Cory


People also ask

What does required mean in JSON Schema?

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.

What does $Ref mean in JSON Schema?

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.

What is $id in JSON Schema?

$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.

What does Exclusiveminimum property in JSON Schema mean?

Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum).


1 Answers

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.

like image 117
Peace Makes Plenty Avatar answered Sep 23 '22 01:09

Peace Makes Plenty