Required Properties Validation in JSON Schema
Learn how to use the required keyword in JSON Schema to mandate specific properties. Understand the difference between required, optional, and nullable fields.
JSON Schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string",
"minLength": 1
},
"email": {
"type": "string",
"format": "email"
},
"phone": {
"type": "string"
},
"role": {
"type": "string",
"enum": ["admin", "editor", "viewer"],
"default": "viewer"
}
},
"required": ["id", "name", "email"]
}Test Data
{
"id": 1001,
"name": "Alice Chen",
"email": "alice@example.com",
"role": "editor"
}Detailed Explanation
Required Properties in JSON Schema
The required keyword specifies which properties must be present in a JSON object. It takes an array of property names. If any listed property is missing from the object, validation fails.
Basic Syntax
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["name"]
}
Here name is required and age is optional. The object {"name": "Alice"} is valid, but {"age": 30} is not because name is missing.
How the Example Schema Works
The schema defines a user record with five properties, three of which are required:
id(required) — an integer identifier. Must always be present.name(required) — a non-empty string. TheminLength: 1prevents empty names.email(required) — a valid email address.phone(optional) — when provided, must be a string. When omitted, no error.role(optional) — an enum with adefaultof"viewer". Clients may omit it.
The test data includes id, name, and email, satisfying all requirements. The phone property is absent, which is fine because it is not in the required array. The role is present and set to "editor", which is one of the allowed enum values.
Required vs. Present vs. Valid
A common misconception is that required validates the value. It does not — it only checks for the presence of the key:
| Scenario | Required check | Type check |
|---|---|---|
| Key missing | Fails | Not evaluated |
Key present, value null |
Passes | Fails (unless nullable) |
| Key present, wrong type | Passes | Fails |
| Key present, correct type | Passes | Passes |
Dynamic Required Properties
In advanced schemas, you can use if/then to make requirements conditional:
{
"if": { "properties": { "role": { "const": "admin" } } },
"then": { "required": ["phone"] }
}
This makes phone required only when role is "admin". See the conditional-if-then-else example for more on this pattern.
Best Practices
- List all mandatory fields in
required. Do not rely on consumers reading documentation. - Combine with
minLength: 1for required strings to reject empty values. - Keep
requiredat the object level, not inside individual property definitions — this is a common syntax mistake.
Use Case
Use required properties in every API request/response schema where certain fields are mandatory. This is the foundation of input validation for user registration, order placement, configuration APIs, and any endpoint where missing data would cause downstream failures.