zod-compiler 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +171 -149
- package/dist/core/codegen/build-path.d.ts.map +1 -1
- package/dist/core/codegen/build-path.js +55 -12
- package/dist/core/codegen/build-path.js.map +1 -1
- package/dist/core/codegen/issue-decls.d.ts +43 -1
- package/dist/core/codegen/issue-decls.d.ts.map +1 -1
- package/dist/core/codegen/issue-decls.js +45 -2
- package/dist/core/codegen/issue-decls.js.map +1 -1
- package/dist/core/codegen/schemas/discriminated-union.d.ts +6 -2
- package/dist/core/codegen/schemas/discriminated-union.d.ts.map +1 -1
- package/dist/core/codegen/schemas/discriminated-union.js +14 -55
- package/dist/core/codegen/schemas/discriminated-union.js.map +1 -1
- package/dist/core/codegen/schemas/string-bool.d.ts +12 -1
- package/dist/core/codegen/schemas/string-bool.d.ts.map +1 -1
- package/dist/core/codegen/schemas/string-bool.js +19 -4
- package/dist/core/codegen/schemas/string-bool.js.map +1 -1
- package/dist/core/codegen/schemas/string.d.ts.map +1 -1
- package/dist/core/codegen/schemas/string.js +15 -10
- package/dist/core/codegen/schemas/string.js.map +1 -1
- package/dist/core/codegen/well-known-regex.d.ts +19 -1
- package/dist/core/codegen/well-known-regex.d.ts.map +1 -1
- package/dist/core/codegen/well-known-regex.js +21 -1
- package/dist/core/codegen/well-known-regex.js.map +1 -1
- package/dist/runtime.d.ts +1 -0
- package/dist/runtime.js +2 -1
- package/package.json +1 -1
|
@@ -29,8 +29,26 @@ declare const EMAIL_REGEX_SOURCE: string;
|
|
|
29
29
|
*
|
|
30
30
|
* Equivalence is enforced by tests/core/codegen/email-fast-regex.test.ts
|
|
31
31
|
* (exhaustive short-string sweep + structured cases + random fuzz).
|
|
32
|
+
*
|
|
33
|
+
* Generated code no longer runs this pattern: the flag-less default is tested
|
|
34
|
+
* by the `__zcEmail` scanner instead (see {@link isDefaultEmailPattern} and
|
|
35
|
+
* ZC_EMAIL_DECL in issue-decls.ts). The table entry stays because its `Src`
|
|
36
|
+
* companion is what issue sites report, and so lean mode keeps that string a
|
|
37
|
+
* single bundle-wide constant; the RegExp export itself is simply unreferenced.
|
|
32
38
|
*/
|
|
33
39
|
declare const EMAIL_FAST_REGEX_SOURCE: string;
|
|
40
|
+
/**
|
|
41
|
+
* Is this exactly zod's default email pattern, flag-less?
|
|
42
|
+
*
|
|
43
|
+
* Generated code tests that one pattern with the `__zcEmail` scanner (see
|
|
44
|
+
* ZC_EMAIL_DECL) rather than with either RegExp above, whatever format name
|
|
45
|
+
* the check carries — `z.email()`, `z.string().regex(z.regexes.email)` and a
|
|
46
|
+
* `z.stringFormat("x", z.regexes.email)` all run the same regex in zod and so
|
|
47
|
+
* get the same verdict here. Only the TEST changes: the issue still reports the
|
|
48
|
+
* pattern string, and a flagged copy keeps its RegExp since the flags would be
|
|
49
|
+
* part of what it reports.
|
|
50
|
+
*/
|
|
51
|
+
declare function isDefaultEmailPattern(pattern: string, flags: string | undefined): boolean;
|
|
34
52
|
/** Fallback UUID regex used when the extractor doesn't provide a pattern (e.g. in unit tests). */
|
|
35
53
|
declare const UUID_REGEX_SOURCE = "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$";
|
|
36
54
|
interface WellKnownRegex {
|
|
@@ -84,5 +102,5 @@ declare function fastTestSource(source: string): string | null;
|
|
|
84
102
|
*/
|
|
85
103
|
declare function wellKnownRegexSourceName(source: string): string | null;
|
|
86
104
|
//#endregion
|
|
87
|
-
export { EMAIL_FAST_REGEX_SOURCE, EMAIL_REGEX_SOURCE, UUID_REGEX_SOURCE, WELL_KNOWN_REGEXES, WellKnownRegex, fastTestSource, lookupFastRegexSource, lookupWellKnownRegex, wellKnownRegexSourceName };
|
|
105
|
+
export { EMAIL_FAST_REGEX_SOURCE, EMAIL_REGEX_SOURCE, UUID_REGEX_SOURCE, WELL_KNOWN_REGEXES, WellKnownRegex, fastTestSource, isDefaultEmailPattern, lookupFastRegexSource, lookupWellKnownRegex, wellKnownRegexSourceName };
|
|
88
106
|
//# sourceMappingURL=well-known-regex.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"well-known-regex.d.ts","names":[],"sources":["../../../src/core/codegen/well-known-regex.ts"],"mappings":";;;;;;;;;;;;;;;;;;;cAqBa
|
|
1
|
+
{"version":3,"file":"well-known-regex.d.ts","names":[],"sources":["../../../src/core/codegen/well-known-regex.ts"],"mappings":";;;;;;;;;;;;;;;;;;;cAqBa;;;;;;;;;;;;;;;;;;;cAoBA;;;;;;;;;;;;iBAaG,sBAAsB,iBAAiB;;cAK1C;UAGI;;EAEf;;EAEA;;;;;;EAMA;;cAGW,6BAA6B;;;;;iBAqE1B,qBAAqB;;;;;;;;;iBAYrB,sBAAsB;;;;;;;;;;;;;;;;iBAmBtB,eAAe;;;;;;;iBAWf,yBAAyB"}
|
|
@@ -30,8 +30,28 @@ const EMAIL_REGEX_SOURCE = String.raw`^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+\-\.]*)[A-Z
|
|
|
30
30
|
*
|
|
31
31
|
* Equivalence is enforced by tests/core/codegen/email-fast-regex.test.ts
|
|
32
32
|
* (exhaustive short-string sweep + structured cases + random fuzz).
|
|
33
|
+
*
|
|
34
|
+
* Generated code no longer runs this pattern: the flag-less default is tested
|
|
35
|
+
* by the `__zcEmail` scanner instead (see {@link isDefaultEmailPattern} and
|
|
36
|
+
* ZC_EMAIL_DECL in issue-decls.ts). The table entry stays because its `Src`
|
|
37
|
+
* companion is what issue sites report, and so lean mode keeps that string a
|
|
38
|
+
* single bundle-wide constant; the RegExp export itself is simply unreferenced.
|
|
33
39
|
*/
|
|
34
40
|
const EMAIL_FAST_REGEX_SOURCE = String.raw`^(?:[A-Za-z0-9_'+\-]+\.)*[A-Za-z0-9_'+\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$`;
|
|
41
|
+
/**
|
|
42
|
+
* Is this exactly zod's default email pattern, flag-less?
|
|
43
|
+
*
|
|
44
|
+
* Generated code tests that one pattern with the `__zcEmail` scanner (see
|
|
45
|
+
* ZC_EMAIL_DECL) rather than with either RegExp above, whatever format name
|
|
46
|
+
* the check carries — `z.email()`, `z.string().regex(z.regexes.email)` and a
|
|
47
|
+
* `z.stringFormat("x", z.regexes.email)` all run the same regex in zod and so
|
|
48
|
+
* get the same verdict here. Only the TEST changes: the issue still reports the
|
|
49
|
+
* pattern string, and a flagged copy keeps its RegExp since the flags would be
|
|
50
|
+
* part of what it reports.
|
|
51
|
+
*/
|
|
52
|
+
function isDefaultEmailPattern(pattern, flags) {
|
|
53
|
+
return !flags && pattern === EMAIL_REGEX_SOURCE;
|
|
54
|
+
}
|
|
35
55
|
/** Fallback UUID regex used when the extractor doesn't provide a pattern (e.g. in unit tests). */
|
|
36
56
|
const UUID_REGEX_SOURCE = "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$";
|
|
37
57
|
const WELL_KNOWN_REGEXES = [
|
|
@@ -160,6 +180,6 @@ function wellKnownRegexSourceName(source) {
|
|
|
160
180
|
return fastTestSource(source) !== null ? `${name}Src` : null;
|
|
161
181
|
}
|
|
162
182
|
//#endregion
|
|
163
|
-
export { EMAIL_FAST_REGEX_SOURCE, EMAIL_REGEX_SOURCE, UUID_REGEX_SOURCE, WELL_KNOWN_REGEXES, fastTestSource, lookupFastRegexSource, lookupWellKnownRegex, wellKnownRegexSourceName };
|
|
183
|
+
export { EMAIL_FAST_REGEX_SOURCE, EMAIL_REGEX_SOURCE, UUID_REGEX_SOURCE, WELL_KNOWN_REGEXES, fastTestSource, isDefaultEmailPattern, lookupFastRegexSource, lookupWellKnownRegex, wellKnownRegexSourceName };
|
|
164
184
|
|
|
165
185
|
//# sourceMappingURL=well-known-regex.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"well-known-regex.js","names":[],"sources":["../../../src/core/codegen/well-known-regex.ts"],"sourcesContent":["/**\n * Well-known regex sources hosted in the virtual module \"virtual:zod-compiler/runtime\".\n *\n * In lean mode (unplugin), `g.regex()` consults this registry and emits a reference\n * like `__zcReEmail` instead of declaring `var __re_email_*=new RegExp(...)`.\n * The bundler then deduplicates the regex literal across all transformed files.\n *\n * Pattern sources are matched verbatim (string equality). Add new entries as Zod\n * exposes additional well-known formats and we want bundle-wide dedup.\n *\n * Verbatim matching means an entry goes STALE SILENTLY when Zod edits a pattern:\n * nothing breaks, the lookup just stops hitting and every transformed file\n * re-declares its own RegExp. The 4.5 bump did exactly that to `cuid`, `ulid`\n * and `iso.datetime` — the last being the ~330-character source this table\n * exists for. tests/core/codegen/well-known-regex.test.ts pins every entry\n * against the live Zod pattern so the next upgrade fails loudly instead.\n */\n\nimport { unrollRepeats } from \"./regex-unroll.js\";\n\n/** Zod v4's email regex source (string.ts uses this directly when format === \"email\"). */\nexport const EMAIL_REGEX_SOURCE = String.raw`^(?!\\.)(?!.*\\.\\.)([A-Za-z0-9_'+\\-\\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$`;\n\n/**\n * Behavior-equivalent rewrite of EMAIL_REGEX_SOURCE that runs ~1.45x faster on V8.\n *\n * Zod's pattern fronts two lookaheads; `(?!.*\\.\\.)` re-scans the entire string\n * before matching starts. The rewrite encodes the same constraints structurally:\n * the local part is dot-separated runs of `[A-Za-z0-9_'+-]` (no leading dot, no\n * empty run ⇒ no `..`) ending in `[A-Za-z0-9_+-]`, and the domain grammar already\n * makes `..` impossible (every label starts with an alphanumeric).\n *\n * Equivalence is enforced by tests/core/codegen/email-fast-regex.test.ts\n * (exhaustive short-string sweep + structured cases + random fuzz).\n */\nexport const EMAIL_FAST_REGEX_SOURCE = String.raw`^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$`;\n\n/** Fallback UUID regex used when the extractor doesn't provide a pattern (e.g. in unit tests). */\nexport const UUID_REGEX_SOURCE =\n \"^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$\";\n\nexport interface WellKnownRegex {\n /** Stable virtual-module export name. Always starts with \"__zcRe\". */\n name: string;\n /** Pattern source string (verbatim match against `g.regex()` 2nd argument). */\n source: string;\n /**\n * Behavior-equivalent faster pattern used for the actual `.test()` regex.\n * Issue reporting (`pattern:` field) always uses `source` so generated\n * issues stay byte-identical to zod's.\n */\n testSource?: string;\n}\n\nexport const WELL_KNOWN_REGEXES: readonly WellKnownRegex[] = [\n { name: \"__zcReEmail\", source: EMAIL_REGEX_SOURCE, testSource: EMAIL_FAST_REGEX_SOURCE },\n { name: \"__zcReUuid\", source: UUID_REGEX_SOURCE },\n { name: \"__zcReCuid\", source: \"^[cC][0-9a-z]{6,}$\" },\n { name: \"__zcReCuid2\", source: \"^[0-9a-z]+$\" },\n { name: \"__zcReUlid\", source: \"^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$\" },\n { name: \"__zcReNanoid\", source: \"^[a-zA-Z0-9_-]{21}$\" },\n { name: \"__zcReXid\", source: \"^[0-9a-vA-V]{20}$\" },\n { name: \"__zcReKsuid\", source: \"^[A-Za-z0-9]{27}$\" },\n {\n name: \"__zcReIpv4\",\n source:\n \"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$\",\n },\n {\n name: \"__zcReIpv6\",\n source:\n \"^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$\",\n },\n {\n name: \"__zcReBase64\",\n source: \"^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$\",\n },\n { name: \"__zcReBase64Url\", source: \"^[A-Za-z0-9_-]*$\" },\n { name: \"__zcReE164\", source: \"^\\\\+[1-9]\\\\d{6,14}$\" },\n {\n name: \"__zcReGuid\",\n source: \"^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$\",\n },\n // The ISO family. Zod BUILDS these from the call's options, so only the\n // default spelling is a fixed string — `z.iso.datetime({ offset: true })` or\n // `{ precision: 3 }` produces a different source that simply misses this\n // exact-match table and keeps its per-IIFE declaration. Defaults are worth\n // listing anyway: `z.iso.datetime()` is everywhere in API schemas and its\n // pattern is ~330 characters, so a bundle that repeats it per validator pays\n // for it in bytes and in a RegExp construction per schema at module init.\n {\n name: \"__zcReIsoDate\",\n source:\n \"^(?:(?:\\\\d\\\\d[2468][048]|\\\\d\\\\d[13579][26]|\\\\d\\\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\\\d|30)|(?:02)-(?:0[1-9]|1\\\\d|2[0-8])))$\",\n },\n { name: \"__zcReIsoTime\", source: \"^(?:[01]\\\\d|2[0-3]):[0-5]\\\\d(?::[0-5]\\\\d(?:\\\\.\\\\d+)?)?$\" },\n {\n name: \"__zcReIsoDateTime\",\n source:\n \"^(?:(?:\\\\d\\\\d[2468][048]|\\\\d\\\\d[13579][26]|\\\\d\\\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\\\d|30)|(?:02)-(?:0[1-9]|1\\\\d|2[0-8])))T(?:(?:[01]\\\\d|2[0-3]):[0-5]\\\\d:[0-5]\\\\d(?:\\\\.\\\\d+)?(?:Z))$\",\n },\n {\n name: \"__zcReIsoDuration\",\n source:\n \"^P(?:(\\\\d+W)|(?!.*W)(?=\\\\d|T\\\\d)(\\\\d+Y)?(\\\\d+M)?(\\\\d+D)?(T(?=\\\\d)(\\\\d+H)?(\\\\d+M)?(\\\\d+([.,]\\\\d+)?S)?)?)$\",\n },\n];\n\nconst SOURCE_TO_NAME: ReadonlyMap<string, string> = new Map(\n WELL_KNOWN_REGEXES.map((r) => [r.source, r.name]),\n);\n\nconst SOURCE_TO_TEST_SOURCE: ReadonlyMap<string, string> = new Map(\n WELL_KNOWN_REGEXES.filter((r) => r.testSource !== undefined).map((r) => [\n r.source,\n r.testSource as string,\n ]),\n);\n\n/**\n * Look up a well-known regex by its pattern source string.\n * Returns the virtual-module export name or null if the pattern is user-defined.\n */\nexport function lookupWellKnownRegex(source: string): string | null {\n return SOURCE_TO_NAME.get(source) ?? null;\n}\n\n/**\n * Look up the hand-written behavior-equivalent test pattern for a regex source.\n * Returns null when no rewrite is registered in {@link WELL_KNOWN_REGEXES}.\n *\n * This is the TABLE lookup only. Codegen calls {@link fastTestSource}, which\n * layers automatic repeat unrolling on top and also covers user-supplied\n * patterns that are in no table.\n */\nexport function lookupFastRegexSource(source: string): string | null {\n return SOURCE_TO_TEST_SOURCE.get(source) ?? null;\n}\n\n/**\n * The pattern a generated `.test()` should actually run for `source`, or null\n * when `source` is already the best form.\n *\n * Two behavior-preserving rewrites compose here: the hand-written table entry\n * above (currently just email), then {@link unrollRepeats}, which turns bounded\n * repeats of single-character atoms into explicit repetition — worth 1.3-3.4x\n * on the string formats everyday schemas use (uuid, guid, ulid, nanoid, xid,\n * ksuid, base64, e164, iso.date). Unrolling runs on the table rewrite when\n * there is one, so the two stack.\n *\n * Callers must apply this only to flag-less regexes (the flagged form would\n * need its flags carried into the reported pattern too) and must keep\n * reporting the ORIGINAL source in issues — see `emitRegexSourceString`.\n */\nexport function fastTestSource(source: string): string | null {\n const tableRewrite = SOURCE_TO_TEST_SOURCE.get(source) ?? null;\n return unrollRepeats(tableRewrite ?? source) ?? tableRewrite;\n}\n\n/**\n * Virtual-module export name for the ORIGINAL `/source/` pattern string of a\n * rewritten well-known regex (e.g. \"__zcReEmailSrc\"). Issue sites reference it\n * so the original pattern stays a single bundle-wide string even though the\n * runtime regex object is built from testSource.\n */\nexport function wellKnownRegexSourceName(source: string): string | null {\n const name = SOURCE_TO_NAME.get(source);\n if (name === undefined) return null;\n return fastTestSource(source) !== null ? `${name}Src` : null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqBA,MAAa,qBAAqB,OAAO,GAAG;;;;;;;;;;;;;AAc5C,MAAa,0BAA0B,OAAO,GAAG;;AAGjD,MAAa,oBACX;AAeF,MAAa,qBAAgD;CAC3D;EAAE,MAAM;EAAe,QAAQ;EAAoB,YAAY;CAAwB;CACvF;EAAE,MAAM;EAAc,QAAQ;CAAkB;CAChD;EAAE,MAAM;EAAc,QAAQ;CAAqB;CACnD;EAAE,MAAM;EAAe,QAAQ;CAAc;CAC7C;EAAE,MAAM;EAAc,QAAQ;CAA6C;CAC3E;EAAE,MAAM;EAAgB,QAAQ;CAAsB;CACtD;EAAE,MAAM;EAAa,QAAQ;CAAoB;CACjD;EAAE,MAAM;EAAe,QAAQ;CAAoB;CACnD;EACE,MAAM;EACN,QACE;CACJ;CACA;EACE,MAAM;EACN,QACE;CACJ;CACA;EACE,MAAM;EACN,QAAQ;CACV;CACA;EAAE,MAAM;EAAmB,QAAQ;CAAmB;CACtD;EAAE,MAAM;EAAc,QAAQ;CAAsB;CACpD;EACE,MAAM;EACN,QAAQ;CACV;CAQA;EACE,MAAM;EACN,QACE;CACJ;CACA;EAAE,MAAM;EAAiB,QAAQ;CAA0D;CAC3F;EACE,MAAM;EACN,QACE;CACJ;CACA;EACE,MAAM;EACN,QACE;CACJ;AACF;AAEA,MAAM,iBAA8C,IAAI,IACtD,mBAAmB,KAAK,MAAM,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,CAClD;AAEA,MAAM,wBAAqD,IAAI,IAC7D,mBAAmB,QAAQ,MAAM,EAAE,eAAe,KAAA,CAAS,CAAC,CAAC,KAAK,MAAM,CACtE,EAAE,QACF,EAAE,UACJ,CAAC,CACH;;;;;AAMA,SAAgB,qBAAqB,QAA+B;CAClE,OAAO,eAAe,IAAI,MAAM,KAAK;AACvC;;;;;;;;;AAUA,SAAgB,sBAAsB,QAA+B;CACnE,OAAO,sBAAsB,IAAI,MAAM,KAAK;AAC9C;;;;;;;;;;;;;;;;AAiBA,SAAgB,eAAe,QAA+B;CAC5D,MAAM,eAAe,sBAAsB,IAAI,MAAM,KAAK;CAC1D,OAAO,cAAc,gBAAgB,MAAM,KAAK;AAClD;;;;;;;AAQA,SAAgB,yBAAyB,QAA+B;CACtE,MAAM,OAAO,eAAe,IAAI,MAAM;CACtC,IAAI,SAAS,KAAA,GAAW,OAAO;CAC/B,OAAO,eAAe,MAAM,MAAM,OAAO,GAAG,KAAK,OAAO;AAC1D"}
|
|
1
|
+
{"version":3,"file":"well-known-regex.js","names":[],"sources":["../../../src/core/codegen/well-known-regex.ts"],"sourcesContent":["/**\n * Well-known regex sources hosted in the virtual module \"virtual:zod-compiler/runtime\".\n *\n * In lean mode (unplugin), `g.regex()` consults this registry and emits a reference\n * like `__zcReEmail` instead of declaring `var __re_email_*=new RegExp(...)`.\n * The bundler then deduplicates the regex literal across all transformed files.\n *\n * Pattern sources are matched verbatim (string equality). Add new entries as Zod\n * exposes additional well-known formats and we want bundle-wide dedup.\n *\n * Verbatim matching means an entry goes STALE SILENTLY when Zod edits a pattern:\n * nothing breaks, the lookup just stops hitting and every transformed file\n * re-declares its own RegExp. The 4.5 bump did exactly that to `cuid`, `ulid`\n * and `iso.datetime` — the last being the ~330-character source this table\n * exists for. tests/core/codegen/well-known-regex.test.ts pins every entry\n * against the live Zod pattern so the next upgrade fails loudly instead.\n */\n\nimport { unrollRepeats } from \"./regex-unroll.js\";\n\n/** Zod v4's email regex source (string.ts uses this directly when format === \"email\"). */\nexport const EMAIL_REGEX_SOURCE = String.raw`^(?!\\.)(?!.*\\.\\.)([A-Za-z0-9_'+\\-\\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$`;\n\n/**\n * Behavior-equivalent rewrite of EMAIL_REGEX_SOURCE that runs ~1.45x faster on V8.\n *\n * Zod's pattern fronts two lookaheads; `(?!.*\\.\\.)` re-scans the entire string\n * before matching starts. The rewrite encodes the same constraints structurally:\n * the local part is dot-separated runs of `[A-Za-z0-9_'+-]` (no leading dot, no\n * empty run ⇒ no `..`) ending in `[A-Za-z0-9_+-]`, and the domain grammar already\n * makes `..` impossible (every label starts with an alphanumeric).\n *\n * Equivalence is enforced by tests/core/codegen/email-fast-regex.test.ts\n * (exhaustive short-string sweep + structured cases + random fuzz).\n *\n * Generated code no longer runs this pattern: the flag-less default is tested\n * by the `__zcEmail` scanner instead (see {@link isDefaultEmailPattern} and\n * ZC_EMAIL_DECL in issue-decls.ts). The table entry stays because its `Src`\n * companion is what issue sites report, and so lean mode keeps that string a\n * single bundle-wide constant; the RegExp export itself is simply unreferenced.\n */\nexport const EMAIL_FAST_REGEX_SOURCE = String.raw`^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$`;\n\n/**\n * Is this exactly zod's default email pattern, flag-less?\n *\n * Generated code tests that one pattern with the `__zcEmail` scanner (see\n * ZC_EMAIL_DECL) rather than with either RegExp above, whatever format name\n * the check carries — `z.email()`, `z.string().regex(z.regexes.email)` and a\n * `z.stringFormat(\"x\", z.regexes.email)` all run the same regex in zod and so\n * get the same verdict here. Only the TEST changes: the issue still reports the\n * pattern string, and a flagged copy keeps its RegExp since the flags would be\n * part of what it reports.\n */\nexport function isDefaultEmailPattern(pattern: string, flags: string | undefined): boolean {\n return !flags && pattern === EMAIL_REGEX_SOURCE;\n}\n\n/** Fallback UUID regex used when the extractor doesn't provide a pattern (e.g. in unit tests). */\nexport const UUID_REGEX_SOURCE =\n \"^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$\";\n\nexport interface WellKnownRegex {\n /** Stable virtual-module export name. Always starts with \"__zcRe\". */\n name: string;\n /** Pattern source string (verbatim match against `g.regex()` 2nd argument). */\n source: string;\n /**\n * Behavior-equivalent faster pattern used for the actual `.test()` regex.\n * Issue reporting (`pattern:` field) always uses `source` so generated\n * issues stay byte-identical to zod's.\n */\n testSource?: string;\n}\n\nexport const WELL_KNOWN_REGEXES: readonly WellKnownRegex[] = [\n { name: \"__zcReEmail\", source: EMAIL_REGEX_SOURCE, testSource: EMAIL_FAST_REGEX_SOURCE },\n { name: \"__zcReUuid\", source: UUID_REGEX_SOURCE },\n { name: \"__zcReCuid\", source: \"^[cC][0-9a-z]{6,}$\" },\n { name: \"__zcReCuid2\", source: \"^[0-9a-z]+$\" },\n { name: \"__zcReUlid\", source: \"^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$\" },\n { name: \"__zcReNanoid\", source: \"^[a-zA-Z0-9_-]{21}$\" },\n { name: \"__zcReXid\", source: \"^[0-9a-vA-V]{20}$\" },\n { name: \"__zcReKsuid\", source: \"^[A-Za-z0-9]{27}$\" },\n {\n name: \"__zcReIpv4\",\n source:\n \"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$\",\n },\n {\n name: \"__zcReIpv6\",\n source:\n \"^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$\",\n },\n {\n name: \"__zcReBase64\",\n source: \"^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$\",\n },\n { name: \"__zcReBase64Url\", source: \"^[A-Za-z0-9_-]*$\" },\n { name: \"__zcReE164\", source: \"^\\\\+[1-9]\\\\d{6,14}$\" },\n {\n name: \"__zcReGuid\",\n source: \"^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$\",\n },\n // The ISO family. Zod BUILDS these from the call's options, so only the\n // default spelling is a fixed string — `z.iso.datetime({ offset: true })` or\n // `{ precision: 3 }` produces a different source that simply misses this\n // exact-match table and keeps its per-IIFE declaration. Defaults are worth\n // listing anyway: `z.iso.datetime()` is everywhere in API schemas and its\n // pattern is ~330 characters, so a bundle that repeats it per validator pays\n // for it in bytes and in a RegExp construction per schema at module init.\n {\n name: \"__zcReIsoDate\",\n source:\n \"^(?:(?:\\\\d\\\\d[2468][048]|\\\\d\\\\d[13579][26]|\\\\d\\\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\\\d|30)|(?:02)-(?:0[1-9]|1\\\\d|2[0-8])))$\",\n },\n { name: \"__zcReIsoTime\", source: \"^(?:[01]\\\\d|2[0-3]):[0-5]\\\\d(?::[0-5]\\\\d(?:\\\\.\\\\d+)?)?$\" },\n {\n name: \"__zcReIsoDateTime\",\n source:\n \"^(?:(?:\\\\d\\\\d[2468][048]|\\\\d\\\\d[13579][26]|\\\\d\\\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\\\d|30)|(?:02)-(?:0[1-9]|1\\\\d|2[0-8])))T(?:(?:[01]\\\\d|2[0-3]):[0-5]\\\\d:[0-5]\\\\d(?:\\\\.\\\\d+)?(?:Z))$\",\n },\n {\n name: \"__zcReIsoDuration\",\n source:\n \"^P(?:(\\\\d+W)|(?!.*W)(?=\\\\d|T\\\\d)(\\\\d+Y)?(\\\\d+M)?(\\\\d+D)?(T(?=\\\\d)(\\\\d+H)?(\\\\d+M)?(\\\\d+([.,]\\\\d+)?S)?)?)$\",\n },\n];\n\nconst SOURCE_TO_NAME: ReadonlyMap<string, string> = new Map(\n WELL_KNOWN_REGEXES.map((r) => [r.source, r.name]),\n);\n\nconst SOURCE_TO_TEST_SOURCE: ReadonlyMap<string, string> = new Map(\n WELL_KNOWN_REGEXES.filter((r) => r.testSource !== undefined).map((r) => [\n r.source,\n r.testSource as string,\n ]),\n);\n\n/**\n * Look up a well-known regex by its pattern source string.\n * Returns the virtual-module export name or null if the pattern is user-defined.\n */\nexport function lookupWellKnownRegex(source: string): string | null {\n return SOURCE_TO_NAME.get(source) ?? null;\n}\n\n/**\n * Look up the hand-written behavior-equivalent test pattern for a regex source.\n * Returns null when no rewrite is registered in {@link WELL_KNOWN_REGEXES}.\n *\n * This is the TABLE lookup only. Codegen calls {@link fastTestSource}, which\n * layers automatic repeat unrolling on top and also covers user-supplied\n * patterns that are in no table.\n */\nexport function lookupFastRegexSource(source: string): string | null {\n return SOURCE_TO_TEST_SOURCE.get(source) ?? null;\n}\n\n/**\n * The pattern a generated `.test()` should actually run for `source`, or null\n * when `source` is already the best form.\n *\n * Two behavior-preserving rewrites compose here: the hand-written table entry\n * above (currently just email), then {@link unrollRepeats}, which turns bounded\n * repeats of single-character atoms into explicit repetition — worth 1.3-3.4x\n * on the string formats everyday schemas use (uuid, guid, ulid, nanoid, xid,\n * ksuid, base64, e164, iso.date). Unrolling runs on the table rewrite when\n * there is one, so the two stack.\n *\n * Callers must apply this only to flag-less regexes (the flagged form would\n * need its flags carried into the reported pattern too) and must keep\n * reporting the ORIGINAL source in issues — see `emitRegexSourceString`.\n */\nexport function fastTestSource(source: string): string | null {\n const tableRewrite = SOURCE_TO_TEST_SOURCE.get(source) ?? null;\n return unrollRepeats(tableRewrite ?? source) ?? tableRewrite;\n}\n\n/**\n * Virtual-module export name for the ORIGINAL `/source/` pattern string of a\n * rewritten well-known regex (e.g. \"__zcReEmailSrc\"). Issue sites reference it\n * so the original pattern stays a single bundle-wide string even though the\n * runtime regex object is built from testSource.\n */\nexport function wellKnownRegexSourceName(source: string): string | null {\n const name = SOURCE_TO_NAME.get(source);\n if (name === undefined) return null;\n return fastTestSource(source) !== null ? `${name}Src` : null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqBA,MAAa,qBAAqB,OAAO,GAAG;;;;;;;;;;;;;;;;;;;AAoB5C,MAAa,0BAA0B,OAAO,GAAG;;;;;;;;;;;;AAajD,SAAgB,sBAAsB,SAAiB,OAAoC;CACzF,OAAO,CAAC,SAAS,YAAY;AAC/B;;AAGA,MAAa,oBACX;AAeF,MAAa,qBAAgD;CAC3D;EAAE,MAAM;EAAe,QAAQ;EAAoB,YAAY;CAAwB;CACvF;EAAE,MAAM;EAAc,QAAQ;CAAkB;CAChD;EAAE,MAAM;EAAc,QAAQ;CAAqB;CACnD;EAAE,MAAM;EAAe,QAAQ;CAAc;CAC7C;EAAE,MAAM;EAAc,QAAQ;CAA6C;CAC3E;EAAE,MAAM;EAAgB,QAAQ;CAAsB;CACtD;EAAE,MAAM;EAAa,QAAQ;CAAoB;CACjD;EAAE,MAAM;EAAe,QAAQ;CAAoB;CACnD;EACE,MAAM;EACN,QACE;CACJ;CACA;EACE,MAAM;EACN,QACE;CACJ;CACA;EACE,MAAM;EACN,QAAQ;CACV;CACA;EAAE,MAAM;EAAmB,QAAQ;CAAmB;CACtD;EAAE,MAAM;EAAc,QAAQ;CAAsB;CACpD;EACE,MAAM;EACN,QAAQ;CACV;CAQA;EACE,MAAM;EACN,QACE;CACJ;CACA;EAAE,MAAM;EAAiB,QAAQ;CAA0D;CAC3F;EACE,MAAM;EACN,QACE;CACJ;CACA;EACE,MAAM;EACN,QACE;CACJ;AACF;AAEA,MAAM,iBAA8C,IAAI,IACtD,mBAAmB,KAAK,MAAM,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,CAClD;AAEA,MAAM,wBAAqD,IAAI,IAC7D,mBAAmB,QAAQ,MAAM,EAAE,eAAe,KAAA,CAAS,CAAC,CAAC,KAAK,MAAM,CACtE,EAAE,QACF,EAAE,UACJ,CAAC,CACH;;;;;AAMA,SAAgB,qBAAqB,QAA+B;CAClE,OAAO,eAAe,IAAI,MAAM,KAAK;AACvC;;;;;;;;;AAUA,SAAgB,sBAAsB,QAA+B;CACnE,OAAO,sBAAsB,IAAI,MAAM,KAAK;AAC9C;;;;;;;;;;;;;;;;AAiBA,SAAgB,eAAe,QAA+B;CAC5D,MAAM,eAAe,sBAAsB,IAAI,MAAM,KAAK;CAC1D,OAAO,cAAc,gBAAgB,MAAM,KAAK;AAClD;;;;;;;AAQA,SAAgB,yBAAyB,QAA+B;CACtE,MAAM,OAAO,eAAe,IAAI,MAAM;CACtC,IAAI,SAAS,KAAA,GAAW,OAAO;CAC/B,OAAO,eAAe,MAAM,MAAM,OAAO,GAAG,KAAK,OAAO;AAC1D"}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export declare const __zcHop: any;
|
|
|
23
23
|
export declare const __zcLo: any;
|
|
24
24
|
export declare const __zcSo: any;
|
|
25
25
|
export declare const __zcCpl: any;
|
|
26
|
+
export declare const __zcEmail: any;
|
|
26
27
|
export declare const __zcPs: any;
|
|
27
28
|
export declare const __zcPlain: any;
|
|
28
29
|
export declare const __zcPfx: any;
|
package/dist/runtime.js
CHANGED
|
@@ -25,8 +25,9 @@ export const __zcHop=Object.prototype.hasOwnProperty;
|
|
|
25
25
|
export function __zcLo(v){return Array.isArray(v)?"array":typeof v==="string"?"string":"unknown";}
|
|
26
26
|
export function __zcSo(v){return v instanceof Set?"set":v instanceof Map?"map":(typeof File!=="undefined"&&v instanceof File)?"file":"unknown";}
|
|
27
27
|
export function __zcCpl(s){var n=s.length;if(!/[\uD800-\uDBFF]/.test(s))return n;var c=n;for(var i=0;i<n-1;i++){if((s.charCodeAt(i)&0xfc00)===0xd800&&(s.charCodeAt(i+1)&0xfc00)===0xdc00){c--;i++;}}return c;}
|
|
28
|
+
export function __zcEmail(s){var n=s.length,i=0,c,p=46;for(;;){if(i===n)return false;c=s.charCodeAt(i);if(c===64)break;if(c===46){if(p===46)return false;}else if(!((c>=97&&c<=122)||(c>=65&&c<=90)||(c>=48&&c<=57)||c===95||c===39||c===43||c===45))return false;p=c;i++;}if(p===46||p===39)return false;var l=++i,d=0,t=true;for(;i<n;i++){c=s.charCodeAt(i);if((c>=97&&c<=122)||(c>=65&&c<=90))continue;if(c===46){if(i===l)return false;d++;l=i+1;t=true;continue;}if((c>=48&&c<=57)||c===45){if(c===45&&i===l)return false;t=false;continue;}return false;}return d>0&&t&&n-l>=2;}
|
|
28
29
|
export function __zcPs(o){if(!Object.prototype.hasOwnProperty.call(o,"__proto__"))return o;var c={...o};delete c["__proto__"];return c;}
|
|
29
|
-
export function __zcPlain(o){if(typeof o!=="object"||o===null||Array.isArray(o))return false;var c=o.constructor;if(c===undefined||typeof c!=="function")return true;var p=c.prototype;if(typeof p!=="object"||p===null||Array.isArray(p))return false;return Object.prototype.hasOwnProperty.call(p,"isPrototypeOf");}
|
|
30
|
+
export function __zcPlain(o){if(typeof o!=="object"||o===null||Array.isArray(o))return false;var c=o.constructor;if(c===Object||c===undefined||typeof c!=="function")return true;var p=c.prototype;if(typeof p!=="object"||p===null||Array.isArray(p))return false;return Object.prototype.hasOwnProperty.call(p,"isPrototypeOf");}
|
|
30
31
|
export function __zcPfx(d,s,b,k){for(var i=0;i<s.length;i++){var x=s[i];x.path=b.concat(k,x.path);d.push(x);}}
|
|
31
32
|
export function __zcCu(f,v){var r=f(v);if(r&&typeof r.then==="function"){throw new __zcCore.$ZodAsyncError();}return !!r;}
|
|
32
33
|
export function __zcSr(f,v,p,e,m){var q={value:v,issues:[]};__zcSrRun(f,q);for(var i=0;i<q.issues.length;i++){var s=q.issues[i],t={};for(var k in s){if(k!=="inst"&&k!=="continue")t[k]=s[k];}if(s.continue!==true)q.aborted=true;t.path=s.path&&s.path.length?p.concat(s.path):p;if(t.message===undefined&&m!==undefined)t.message=m;e.push(t);}return q;}
|