Repository navigation
Description
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
yassinelakchouch commentedon Nov 9, 2026
Reproduced on zod 4.6.7:
[],[1,2,3,4,5]and[1,2]all pass, andtoJSONSchemagives back{"type":"array","items":{}}.Root cause: in
convertBaseSchema, thetype: "array"case has four branches. Only theitems !== undefinedbranch appliesminItems/maxItems; the last branch (// No items specified) returns a barez.array(z.any()).Fix: fold the last branch into the regular-array one, so the bounds are applied either way:
Tested on a patched 4.6.7:
[]→ false, five items → false,[1,2]→ true;toJSONSchemanow emits"minItems":2,"maxItems":4. Unchanged:{ items: { type: "number" }, minItems: 1 }([]false,[1]true,["x"]false) and a bare{ type: "array" }(anything passes). TheuniqueItems/containsguard after the switch still runs, since it readszodSchema.anthony95macias commentedon Nov 17, 2026
Hey, I’d like to work on this if it’s available. I can add a regression test for arrays without
itemsthat checks both length bounds and the JSON Schema round-trip, then patch the array conversion. Is anyone already working on it?