All articles
165 articles · updated weekly See our Tools
All articles
Tutorials

JSON Schema required: What It Actually Validates (and What It Doesn't)

The required keyword in JSON Schema validates key presence, not value presence. Understand why null passes in required fields and how to properly close that contract.

JSON Schema required: What It Actually Validates (and What It Doesn't)
COVER · Tutorials

There was an email field marked as required in the schema. The payload arrived with "email": null. Validation passed. The null email was persisted, the user never got the confirmation message, and we only found out a week later, looking at a send queue piling up failures.

The schema wasn't "wrong": the field was right there in required. The problem is that required doesn't mean what most devs assume. This post separates what it validates from what it doesn't, and shows how to close each case: null, empty strings, nested objects, conditionally required fields, and the required rules that LLM structured outputs impose. The examples use draft 2020-12 and were run on Ajv 8.20 and Python's jsonschema 4.26.

What required means in JSON Schema

required is an array with the names of the keys that must exist in the object. That's all. The specification says the instance is valid if every name in the array is a property present in the object. It says nothing about the value: it doesn't guarantee the value isn't null, that the string has content, or that the number isn't zero.

With this schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "email": { "type": "string" }
  },
  "required": ["email"]
}

Ajv answers like this for four payloads:

{ "name": "Alice" }               -> / must have required property 'email'
{ "email": null }                 -> /email must be string
{ "email": "" }                   -> valid
{ "email": "alice@example.com" }  -> valid

Look at where each error comes from. A missing key is the only case required catches. The null failed only because the property has "type": "string"; it was type that rejected it, not required. And the empty string passed, because it is a string and the key exists. null in JSON is not absence: it's an explicit value, and required doesn't look at values.

required without type: anything goes

My incident was this case, a field listed in required with no constraint at all:

{
  "type": "object",
  "properties": {
    "email": {}
  },
  "required": ["email"]
}

The empty subschema {} accepts anything. I tested it: null, "" and {} all pass; only the object without the key fails. This pattern shows up when someone adds the name to required "so we don't forget" and leaves the definition for later. The contract now says "the key must exist" and nothing more.

Required field that rejects null

For a required, non-null field you need two keywords, each with its own job: required demands the key, type restricts the value.

{
  "type": "object",
  "properties": {
    "email": { "type": "string", "format": "email" }
  },
  "required": ["email"]
}

With "type": "string", null fails because it isn't a string. A warning about format, which trips many people: in Ajv v8 it is not validated by the core. Without the ajv-formats package registered, the default behavior (strict mode) is to throw at compile time: unknown format "email" ignored in schema. If someone turned strict mode off, the schema compiles and "xyz" passes as an email with no warning at validation time. In Python, jsonschema also ignores format unless you pass a FormatChecker. The setup for both sides is in how to validate JSON Schema in Node.js and Python.

Required field that rejects an empty string

This is what my first schema forgot: "email": "" passes required + type. To demand content, use minLength:

"name": { "type": "string", "minLength": 1, "pattern": "\\S" }

minLength: 1 rejects "". The pattern with \S requires at least one non-whitespace character, and catches the " " a user typed just to get past the form. Real Ajv output:

{ "name": "" }      -> must NOT have fewer than 1 characters | must match pattern "\S"
{ "name": "   " }   -> must match pattern "\S"
{ "name": " Ana " } -> valid

Remember that pattern isn't anchored: \S looks for a non-space character anywhere, which is exactly what we want here. If you need the value trimmed, that's normalization, and normalization is the code's job, not the schema's. JSON Schema validates; it doesn't transform.

Optional field that accepts null

The reverse has its own pitfall. A field that may be missing and, when present, may be null or a string:

"middle_name": { "type": ["string", "null"] }

Outside required, it can be absent. When present, it accepts null or text, and rejects 1 with must be string,null. If you declare only "type": "string" without putting it in required, the field may be missing, but when present it can't be null. The distinction matters for APIs that treat "absent" and "null" differently: in JSON Merge Patch (RFC 7396), null means "delete this field", while absence means "leave it alone".

A lot of the null confusion comes from OpenAPI 3.0, which didn't accept type as an array and used its own nullable: true extension. In OpenAPI 3.1, which adopts JSON Schema 2020-12, nullable is gone and the right way is "type": ["string", "null"]. A schema copied from a 3.0 spec behaves differently depending on the validator: Ajv recognizes nullable as an OpenAPI extension and accepts the null; Python's jsonschema ignores the keyword, and the same payload fails with None is not of type 'string'. I tested both. Convert to the type array and the problem goes away.

required in nested objects

required only applies at the level where it's declared. Nothing propagates inward:

{
  "type": "object",
  "properties": {
    "address": {
      "type": "object",
      "properties": {
        "street": { "type": "string" },
        "city": { "type": "string" }
      },
      "required": ["street", "city"]
    }
  },
  "required": ["address"]
}

The root required guarantees address exists. The inner one guarantees street and city. Without the inner one, "address": {} passes: the key exists, the object is empty. With it, Ajv returns both errors at path /address.

Conditionally required: dependentRequired and if/then

A field that is required "only sometimes" is what shows up most in real payloads. JSON Schema has two tools.

dependentRequired (draft 2019-09 onward) handles "if key A exists, key B must exist too":

"dependentRequired": { "installments": ["card_token"] }

Sent installments, you must send card_token. In draft-07 the same rule was written with dependencies, which was split into dependentRequired and dependentSchemas. The default Ajv class (draft-07) rejects dependentRequired in strict mode with unknown keyword; use Ajv2020.

if/then handles "if the value of A is X, B is required":

{
  "type": "object",
  "properties": {
    "payment_method": { "enum": ["pix", "credit_card"] },
    "card_token": { "type": "string", "minLength": 1 }
  },
  "required": ["payment_method"],
  "if": {
    "properties": { "payment_method": { "const": "credit_card" } },
    "required": ["payment_method"]
  },
  "then": { "required": ["card_token"] }
}

The required inside if is not redundant. Without it, an object without payment_method satisfies the if (the properties keyword only validates keys that exist), and then starts demanding card_token from someone who hasn't even picked a payment method. I tested the version without the inner required: {} fails with must have required property 'card_token'. It's the most common conditional-schema bug I see in code review.

required in LLM structured outputs

If you use JSON Schema to force a model's response shape, the rules change. In OpenAI's Structured Outputs strict mode, the documentation is explicit: every property must be listed in required, and additionalProperties: false must be set on every object. Optional fields are emulated with a union with null:

{
  "type": "object",
  "properties": {
    "title": { "type": "string" },
    "subtitle": { "type": ["string", "null"] }
  },
  "required": ["title", "subtitle"],
  "additionalProperties": false
}

So there, "optional" means "the key always comes, the value may be null". It's the same mental model as this post, just enforced: presence is required, value is type.

required and default

Having a default doesn't replace required. Per the specification, default is an annotation: the validator fills in nothing. Ajv has a useDefaults: true option that injects the value into the object during validation (I tested it: {} becomes {"status":"active"}), but that's Ajv behavior, not the standard. Another validator, or the same Ajv without the option, leaves the field absent and your code gets undefined.

Frequently asked questions

What is required used for in JSON Schema?

To list the keys that must exist in the object. If any is missing, validation fails with "must have required property". It doesn't validate the value: that's what type, minLength, enum and the other keywords are for.

Why does null pass in a required field?

Because null is a value, and required only checks that the key exists. To reject null, declare a type that doesn't include it, such as "type": "string".

How do I make a required field that rejects empty strings?

Combine required with "type": "string" and "minLength": 1. To also reject whitespace-only strings, add "pattern": "\\S".

How do I make a field required only under certain conditions?

Use dependentRequired when the rule depends on another key being present, and if/then when it depends on another key's value. In the if, include required for the tested key so objects without it don't trigger the then.

Presence is required, the value is everything else

The costliest mistake isn't using required wrong; it's assuming it does type's job. Before closing a schema, answer for each field: can it be missing? Can it be null? Can it be empty? Does it depend on another field? Each answer becomes a different keyword, and only the first one belongs to required. For the other value errors ("must be string", "must be integer"), the post on JSON Schema type errors explains each message, and the complete JSON Schema example for a REST API shows these decisions together in a real payload.

The editorial content ends here. To test this post's edge cases (missing key, null, "", empty nested object) without setting up a project, I paste the schema and the payloads into the Quick Tools JSON Schema validator; and to see the difference between "absent" and "null" in the client type, I paste an array of three sample payloads (one with "middle_name": "Carlo", one with null, one without the key) into the JSON to TypeScript converter, which generates middle_name?: string | null.

RD
Author
Rafael Duarte
Backend developer with a background in fintech and B2B SaaS — worked on teams that scaled APIs from zero to millions of requests. Carries enough production scars to hold strong opinions on tools, patterns and architecture decisions. Not an academic: he read the UUID RFC when he had to choose between v4 and v7 for a write-heavy table.
View profile