Required vs Optional Properties in JSON Schema

Understand the difference between required and optional properties in JSON Schema. Learn how generators decide which fields to mark required and when to adjust.

Object Schemas

Detailed Explanation

Required vs. Optional: What the Generator Decides

The required keyword is one of the most important parts of any object schema. It controls which properties must be present for the data to be valid.

How the Generator Handles It

When you provide a single JSON sample, the generator typically marks all observed keys as required:

{
  "required": ["id", "name", "email", "role"]
}

This makes sense as a starting point — every key in your sample was present, so the generator assumes they are all mandatory.

When to Relax Requirements

In practice, many fields are optional. After generation, review each property and ask:

  • Is this field always provided by clients? If not, remove it from required.
  • Does the field have a sensible default? If the server can fill it in, it should not be required.
  • Is this field conditionally present? For example, a company field that only appears for business accounts.

The required Keyword Semantics

{
  "type": "object",
  "properties": {
    "id": { "type": "integer" },
    "name": { "type": "string" },
    "nickname": { "type": "string" }
  },
  "required": ["id", "name"]
}

In this schema, id and name must be present. nickname is defined in properties but omitted from required, meaning it is optional. If nickname is present, it still must be a string — the type constraint applies regardless.

Common Mistakes

  • Confusing optional with nullable: A property can be optional (absent from the object) yet non-nullable (when present, it must not be null). These are orthogonal concepts.
  • Over-requiring: Marking every field as required breaks backward compatibility when you add new fields later. Start conservative — require only what is truly mandatory.
  • Under-requiring: Omitting critical fields from required can let invalid payloads through silently.

Multi-Sample Inference

Advanced generators accept multiple sample objects and compute the intersection of keys to determine required fields. A key present in every sample is likely required; a key missing from some samples is likely optional. This approach yields more accurate schemas from real data.

Use Case

Adjust required vs. optional properties when designing API contracts, especially for PATCH endpoints where clients send only the fields they want to update, leaving others unchanged.

Try It — JSON Schema Generator

Open full tool