Skip to content

[BUG]: fromJSONSchema silently drops minItems and maxItems when items is omitted #6749

Open

Description

@chinmayp995

Description

In JSON Schema (draft-04, draft-07, and draft-2020-12), the items keyword is optional on an array schema. A schema defining length constraints without restricting item types (e.g. { type: "array", minItems: 2, maxItems: 4 }) is standard.

z.fromJSONSchema currently drops minItems and maxItems whenever items is omitted, causing the resulting schema to accept invalid payloads that violate the length bounds.

Reproduction

import { z } from "zod";

const schema = z.fromJSONSchema({
  type: "array",
  minItems: 2,
  maxItems: 4,
});

// Fails silently: accepts empty array
console.log(schema.safeParse([]));
// Actual:   { success: true, data: [] }
// Expected: { success: false, error: ... }

// Fails silently: accepts array exceeding maxItems
console.log(schema.safeParse([1, 2, 3, 4, 5]));
// Actual:   { success: true, data: [1, 2, 3, 4, 5] }
// Expected: { success: false, error: ... }

// toJSONSchema round-trip also loses constraints
console.log(z.toJSONSchema(schema));
// Emits: { "$schema": "...", "type": "array", "items": {} }

Where it comes from

In packages/zod/src/v4/classic/from-json-schema.ts:

      } else if (items !== undefined) {
        // Regular array
        const element = convertSchema(items as JSONSchema.JSONSchema, ctx);
        let arraySchema = z.array(element);

        // Apply constraints
        if (typeof schema.minItems === "number") {
          arraySchema = arraySchema.min(schema.minItems);
        }
        if (typeof schema.maxItems === "number") {
          arraySchema = arraySchema.max(schema.maxItems);
        }

        zodSchema = arraySchema;
      } else {
        // No items specified - array of any
        zodSchema = z.array(z.any());
      }

The length constraints (minItems, maxItems) are evaluated exclusively inside else if (items !== undefined). When items is omitted, execution falls into else, returning an unconstrained z.array(z.any()).

Proposed Fix

Fold the two branches so items === undefined defaults to z.any() while continuing to apply minItems and maxItems:

      } else {
        // Regular array (items specified, or omitted meaning items: any)
        const element = items !== undefined ? convertSchema(items as JSONSchema.JSONSchema, ctx) : z.any();
        let arraySchema = z.array(element);

        // Apply constraints
        if (typeof schema.minItems === "number") {
          arraySchema = arraySchema.min(schema.minItems);
        }
        if (typeof schema.maxItems === "number") {
          arraySchema = arraySchema.max(schema.maxItems);
        }

        zodSchema = arraySchema;
      }

If everything works correctly, Can I be allowed to open a PR for the same?

Activity

  1. yassinelakchouch commented on Nov 9, 2026

    @yassinelakchouch

    Reproduced on zod 4.6.7: [], [1,2,3,4,5] and [1,2] all pass, and toJSONSchema gives back {"type":"array","items":{}}.

    Root cause: in convertBaseSchema, the type: "array" case has four branches. Only the items !== undefined branch applies minItems/maxItems; the last branch (// No items specified) returns a bare z.array(z.any()).

    Fix: fold the last branch into the regular-array one, so the bounds are applied either way:

    } else {
      // without `items` any element is allowed, but the length bounds still apply
      const element = items !== undefined ? convertSchema(items, ctx) : z.any();
      let arraySchema = z.array(element);
      if (typeof schema.minItems === "number") arraySchema = arraySchema.min(schema.minItems);
      if (typeof schema.maxItems === "number") arraySchema = arraySchema.max(schema.maxItems);
      zodSchema = arraySchema;
    }

    Tested on a patched 4.6.7: [] → false, five items → false, [1,2] → true; toJSONSchema now emits "minItems":2,"maxItems":4. Unchanged: { items: { type: "number" }, minItems: 1 } ([] false, [1] true, ["x"] false) and a bare { type: "array" } (anything passes). The uniqueItems/contains guard after the switch still runs, since it reads zodSchema.

  2. anthony95macias commented on Nov 17, 2026

    @anthony95macias

    Hey, I’d like to work on this if it’s available. I can add a regression test for arrays without items that checks both length bounds and the JSON Schema round-trip, then patch the array conversion. Is anyone already working on it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions