Skip to main content
Use "type": "null" when a field must be present with a null value.

Example

{
  "type": "object",
  "properties": {
    "middle_name": { "type": "null" },
    "deprecated_field": { "type": "null" }
  },
  "required": ["middle_name", "deprecated_field"],
  "additionalProperties": false
}
from pydantic import BaseModel, ConfigDict

class Payload(BaseModel):
    model_config = ConfigDict(extra="forbid")

    middle_name: None
    deprecated_field: None
import { z } from "zod";

const payloadSchema = z
  .object({
    middle_name: z.null(),
    deprecated_field: z.null(),
  })
  .strict();

null vs optional fields

  • type: "null": field is present and value is null.
  • Optional field: field may be absent.
  • Optional field + type: "null": field is either absent or present with null.
If you want a field that can be either string or null, use a union type:
{
  "type": "object",
  "properties": {
    "value": {
      "anyOf": [
        { "type": "string" },
        { "type": "null" }
      ]
    }
  },
  "required": ["value"],
  "additionalProperties": false
}
from pydantic import BaseModel, ConfigDict

class Payload(BaseModel):
    model_config = ConfigDict(extra="forbid")

    value: str | None
import { z } from "zod";

const payloadSchema = z
  .object({
    value: z.string().nullable(),
  })
  .strict();