fromJSONSchema silently drops assertions alongside enum/const, required keys outside properties, and typeless keywords
fromJSONSchema silently drops some keywords depending on which conversion branch wins, so the resulting schema accepts values the JSON Schema rejects. Three cases, still present in 4.6.5:
Reproduce
import { z } from "zod";
// 1. enum or const + type-specific keywords: the assertion is dropped
z.fromJSONSchema({ type: "string", enum: ["a"], minLength: 2 }).safeParse("a").success; // true — should be false
z.fromJSONSchema({ type: "string", const: "a", minLength: 2 }).safeParse("a").success; // true — should be false
z.fromJSONSchema({ type: "number", enum: [1, 5], minimum: 3 }).safeParse(1).success; // true — should be false
// 2. required naming a key not listed in properties
z.fromJSONSchema({ type: "object", required: ["x"] }).safeParse({}).success; // true — should be false
// 3. type-specific keywords without `type`
z.fromJSONSchema({ minLength: 2 }).safeParse("a").success; // true — should be false
// (per JSON Schema, minLength applies to any string instance)For contrast, { type: "string", minLength: 2 } and { type: "object", properties: { x: {} }, required: ["x"] } both reject correctly.
Where it comes from (src/v4/classic/from-json-schema.ts)
convertBaseSchemahandlesenumfirst and returns a literal/enum schema, so thetypeswitch that appliesminLength,pattern,minimum, etc. never runs.consthas the same early return just below it.- The
"object"case only buildsrequiredinto the shape for keys inproperties; keys that appear only inrequiredimpose nothing. - With no
type,convertBaseSchemareturnsz.any()(“No type specified - empty schema”), even when the schema carries assertions that apply conditionally to instances of a given type.
Expected
Every recognized assertion keyword constrains the result, whichever branch builds the base schema, or conversion throws for combinations it doesn't support rather than weakening them silently. Similar per-keyword gaps were fixed in #6463, #6494 and #6198.
Context
We validate tool inputs declared by untrusted authors with fromJSONSchema, so a silently dropped assertion is a validation hole rather than a cosmetic difference. We currently work around it by rejecting these schema shapes before conversion.
Source: colinhacks/zod