From 50927911d29d7866a00180a97890817cdfd6130d Mon Sep 17 00:00:00 2001 From: Yarchik Date: Fri, 12 Jun 2026 14:56:51 +0100 Subject: [PATCH] fix(regex): include the empty string when a zero-min quantifier applies to a number pattern --- ark/regex/__tests__/regex.test.ts | 11 +++++++++++ ark/regex/quantify.ts | 8 ++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ark/regex/__tests__/regex.test.ts b/ark/regex/__tests__/regex.test.ts index fa143c069c..3a887e5ddf 100644 --- a/ark/regex/__tests__/regex.test.ts +++ b/ark/regex/__tests__/regex.test.ts @@ -223,6 +223,17 @@ contextualize(() => { attest>(S) }) + // https://github.com/arktypeio/arktype/issues/1625 + it("* over a digit class allows the empty string", () => { + const S = regex("^(\\d*)$") + attest>(S) + }) + + it("? over a digit class allows the empty string", () => { + const S = regex("^(\\d?)$") + attest>(S) + }) + it("unmatched ?", () => { // @ts-expect-error attest(() => regex("?")).type.errors(writeUnmatchedQuantifierError("?")) diff --git a/ark/regex/quantify.ts b/ark/regex/quantify.ts index b1de64aa83..df7d5cc537 100644 --- a/ark/regex/quantify.ts +++ b/ark/regex/quantify.ts @@ -98,9 +98,13 @@ type tryFastPath< max extends number | null > = max extends 0 ? "" - : // repeating string or `${number}` any number of times will not change the type + : // repeating string or `${number}` any number of times will not change the + // type, but zero repetitions produce "", which `${number}` does not include string extends pattern ? string - : `${number}` extends pattern ? `${number}` + : `${number}` extends pattern ? + min extends 0 ? + "" | `${number}` + : `${number}` : min extends 0 ? max extends 1 ? "" | pattern : max extends number ? loopFromZero