Skip to content

Commit 51a1368

Browse files
fix(v4): let the includes(position) pattern match at or after the offset (#6024)
`String.prototype.includes(sub, position)` treats position as a start offset, so it matches at that index or any later one. The pattern this check advertised used `{N}`, which pins the substring to exactly index N, and `z.toJSONSchema()` emitted it verbatim — a schema generated from `z.string().includes("foo", { position: 2 })` rejected "xxxfoo" while the validator accepted it. At `position: 0` the emitted pattern was equivalent to startsWith. The pattern reaches past JSON Schema output. It becomes the string's `_zod.pattern`, which template literals compose into their own regex, and the AOT compiler treats it as the entire check — so the compiled fast path rejected a late match and fell back to the runtime on every one. The string tests have asserted the "at or later" reading since before the pattern existed: one parses "XXXincludesXX" through `position: 2`, where the substring starts at index 3.
1 parent a0bfdb3 commit 51a1368

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎packages/zod/src/v4/classic/tests/to-json-schema.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ describe("toJSONSchema", () => {
496496
"pattern": "cruel",
497497
},
498498
{
499-
"pattern": "^.{10}dark",
499+
"pattern": "^.{10,}dark",
500500
},
501501
{
502502
"pattern": ".*world$",
@@ -535,7 +535,7 @@ describe("toJSONSchema", () => {
535535
"type": "string",
536536
},
537537
{
538-
"pattern": "^.{10}dark",
538+
"pattern": "^.{10,}dark",
539539
"type": "string",
540540
},
541541
{
@@ -552,6 +552,19 @@ describe("toJSONSchema", () => {
552552
`);
553553
});
554554

555+
test("includes with position emits a pattern matching runtime semantics", () => {
556+
// String.prototype.includes(sub, position) matches `sub` at `position` OR
557+
// LATER, so the JSON Schema pattern must allow >= position leading chars.
558+
const schema = z.string().includes("foo", { position: 2 });
559+
const json = z.toJSONSchema(schema) as { pattern?: string };
560+
expect(json.pattern).toBe("^.{2,}foo");
561+
562+
const re = new RegExp(json.pattern!);
563+
for (const input of ["xxfoo", "xxxfoo", "xfoo", "ab"]) {
564+
expect(re.test(input)).toBe(schema.safeParse(input).success);
565+
}
566+
});
567+
555568
test("number constraints", () => {
556569
expect(z.toJSONSchema(z.number().min(5).max(10))).toMatchInlineSnapshot(
557570
`

‎packages/zod/src/v4/core/checks.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,10 @@ export const $ZodCheckIncludes: core.$constructor<$ZodCheckIncludes> = /*@__PURE
974974
$ZodCheck.init(inst, def);
975975

976976
const escapedRegex = util.escapeRegex(def.includes);
977-
const pattern = new RegExp(typeof def.position === "number" ? `^.{${def.position}}${escapedRegex}` : escapedRegex);
977+
// `String.prototype.includes(sub, position)` matches `sub` at `position`
978+
// OR LATER, so the pattern must allow at least `position` leading chars
979+
// (`{N,}`), not exactly `position` chars (`{N}`).
980+
const pattern = new RegExp(typeof def.position === "number" ? `^.{${def.position},}${escapedRegex}` : escapedRegex);
978981
def.pattern = pattern;
979982
inst._zod.onattach.push((inst) => {
980983
const bag = inst._zod.bag as schemas.$ZodStringInternals<unknown>["bag"];

0 commit comments

Comments
 (0)