Getting Started
Miscellaneous Examples
In this page, you will find miscellaneous examples illustrating different uses cases to help you get the most out of your JSON Schemas. Each example comes with accompanying JSON data and explanation.
- A typical minimum schema
- Arrays of things
- Enumerated values
- Regular expression pattern
- Complex object with nested properties
- Conditional validation with dependentRequired
- Conditional validation with dependentSchemas
- Conditional validation with if-else
Basic
This example provides a typical minimum you are likely to see in JSON Schema. It contains:
$id
keyword$schema
keywordtitle
annotation keywordtype
instance data modelproperties
validation keyword- Three keys:
firstName
,lastName
andage
each with their own:description
annotation keyword.type
instance data model (see above).
minimum
validation keyword on theage
key.
Data
In the data example, we provide values for the firstName
, lastName
, and age
properties. The values match the defined schema, where firstName
is a string, lastName
is a string, and age
is an integer greater than or equal to zero.
Arrays of things
Arrays are fundamental structures in JSON -- here we demonstrate a couple of ways they can be described:
- An array of string values.
- An array of objects.
We also introduce the following with this example:
For the fruits
property:
type
is set to "array" to indicate it's an array.items
describes the items within the array. In this case, they should be of type "string".
For the vegetables
property:
type
is also set to "array" to indicate it's an array.items
references the$defs/veggie
definition, indicating that the items in the array should conform to the "veggie" schema defined in the$defs
section.
Data
The data example shows the usage of arrays. The fruits
property contains an array of strings, while the vegetables
property contains an array of objects, each adhering to the "veggie" schema definition.
Enumerated values
This example introduces the enum
validation keyword which is used with an array of values that includes an integer (42
), a boolean (true
), a string ("hello"
), null
, and an array ([1, 2, 3]
). This demonstrates how enum
can be used to specify a set of allowed values of different types.
Data
The provided data adheres to the schema by using the exact values specified in the enum array: [1, 2, 3]
.
Regular expression pattern
This example introduces the pattern keyword and defines an object with a property called code
that must match a specific regular expression pattern: ^[A-Z]{3}-\d{3}$
. The pattern here requires three uppercase letters followed by a hyphen and three digits.
Data
The provided data, "ABC-123", satisfies this pattern defined in the schema.
Complex object with nested properties
The schema below represents a complex object with various properties including name
, age
, address
, and hobbies
. The address
property is an object with nested properties, and the hobbies
property is an array of strings. The name
and age
properties are required.
Data
The provided data conforms to the schema by including values for the required properties and ensuring the age
is an integer greater than or equal to zero. The address
object contains all the necessary properties, and the hobbies
property is an array of strings.
Conditional validation with dependentRequired
In this example, the dependentRequired
keyword is used to specify that the property bar
is required when the property foo
is present. The schema enforces the condition that if foo
exists, then bar
must also be present.
Data
As per the schema, when the foo
property is present (true
), the bar
property becomes required. The bar
property is provided with the value "Hello World", satisfying the requirement of being a string and ensuring compliance with the dependentRequired
condition.
Since both foo
and bar
are missing, the instance is still valid and in compliance with the dependentRequired
condition as well.
The above schema is invalid, since the foo
property is present, but bar
is not, which invalidates the condition of the dependentRequired
keyword.
Conditional validation with dependentSchemas
The given schema showcases the use of the dependentSchemas
keyword. It allows defining a subschema that must be satisfied if a certain property is present.
- In this example, the schema defines an object with two properties:
foo
andpropertiesCount
. Thefoo
property is of boolean type, while thepropertiesCount
property is of integer type with a minimum value of 0. - According to the subschema, when the
foo
property is present, thepropertiesCount
property becomes required, and must be an integer with a minimum value of 7.
Data
Here, the foo
property is set to true, indicating its presence. As per the schema, when foo
is present, the propertiesCount
property becomes required. In this case, the propertiesCount
property is provided with a value of 10, which satisfies the requirement of being an integer and having a minimum value of 7.
In the above data, propertiesCount
is 5 but since foo
is missing, propertiesCount
does not need to be 7 or more than 7, it only needs to be greater than or equal to 0. Hence, this instance is valid.
In this, we have foo
as true, but propertiesCount
is 5, and in the schema, propertiesCount
is set to have minimum 7 as the input according to the dependentSchemas
. Hence, this is an invalid instance.
Conditional validation with if-else
In this schema, we have two properties: isMember
and membershipNumber
. The conditional validation is based on the value of the isMember
property. The validation keywords if, then, and else.
Here's how the validation works in this example:
If the value of isMember
is true:
- The
then
block is applied, which specifies that themembershipNumber
property should be a string with a minimum length of 10 and a maximum length of 10.
If the value of isMember
is anything other than true:
- The
else
block is applied, which specifies that themembershipNumber
property can be any string.
Data
In this case, the isMember
property is set to true, so the then block is applied. The membershipNumber
property is a string with a length of 10 characters, satisfying the validation.
In this case, the isMember
property is false, so the else block is applied. The membershipNumber
property can be any string with minimum length greater than or equal to 15, so it satisfies the validation.
Need Help?
Did you find these docs helpful?
Help us make our docs great!
At JSON Schema, we value docs contributions as much as every other type of contribution!
Still Need Help?
Learning JSON Schema is often confusing, but don't worry, we are here to help!.