zod 4.1.0-canary.20250821T014902 → 4.1.0-canary.20250823T064644

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.
Files changed (106) hide show
  1. package/package.json +1 -1
  2. package/src/v3/tests/object.test.ts +2 -2
  3. package/src/v4/classic/external.ts +0 -1
  4. package/src/v4/classic/parse.ts +49 -0
  5. package/src/v4/classic/schemas.ts +145 -7
  6. package/src/v4/classic/tests/array.test.ts +6 -6
  7. package/src/v4/classic/tests/catch.test.ts +25 -0
  8. package/src/v4/classic/tests/codec-examples.test.ts +538 -0
  9. package/src/v4/classic/tests/codec.test.ts +532 -0
  10. package/src/v4/classic/tests/continuability.test.ts +1 -1
  11. package/src/v4/classic/tests/default.test.ts +32 -0
  12. package/src/v4/classic/tests/firstparty.test.ts +4 -0
  13. package/src/v4/classic/tests/function.test.ts +31 -31
  14. package/src/v4/classic/tests/hash.test.ts +68 -0
  15. package/src/v4/classic/tests/nonoptional.test.ts +15 -0
  16. package/src/v4/classic/tests/object.test.ts +33 -2
  17. package/src/v4/classic/tests/pipe.test.ts +25 -5
  18. package/src/v4/classic/tests/prefault.test.ts +25 -0
  19. package/src/v4/classic/tests/preprocess.test.ts +1 -6
  20. package/src/v4/classic/tests/refine.test.ts +78 -5
  21. package/src/v4/classic/tests/set.test.ts +1 -1
  22. package/src/v4/classic/tests/string-formats.test.ts +16 -0
  23. package/src/v4/classic/tests/string.test.ts +82 -1
  24. package/src/v4/classic/tests/stringbool.test.ts +40 -0
  25. package/src/v4/classic/tests/template-literal.test.ts +1 -1
  26. package/src/v4/classic/tests/to-json-schema.test.ts +21 -2
  27. package/src/v4/classic/tests/transform.test.ts +7 -0
  28. package/src/v4/classic/tests/union.test.ts +1 -1
  29. package/src/v4/core/api.ts +25 -35
  30. package/src/v4/core/core.ts +7 -26
  31. package/src/v4/core/index.ts +0 -1
  32. package/src/v4/core/json-schema.ts +1 -0
  33. package/src/v4/core/parse.ts +101 -0
  34. package/src/v4/core/regexes.ts +40 -1
  35. package/src/v4/core/schemas.ts +521 -129
  36. package/src/v4/core/to-json-schema.ts +43 -8
  37. package/src/v4/core/util.ts +73 -0
  38. package/src/v4/mini/external.ts +0 -1
  39. package/src/v4/mini/parse.ts +14 -1
  40. package/src/v4/mini/schemas.ts +153 -12
  41. package/src/v4/mini/tests/codec.test.ts +499 -0
  42. package/src/v4/mini/tests/object.test.ts +9 -0
  43. package/src/v4/mini/tests/string.test.ts +16 -0
  44. package/v4/classic/external.cjs +1 -2
  45. package/v4/classic/external.d.cts +1 -1
  46. package/v4/classic/external.d.ts +1 -1
  47. package/v4/classic/external.js +1 -1
  48. package/v4/classic/parse.cjs +10 -1
  49. package/v4/classic/parse.d.cts +8 -0
  50. package/v4/classic/parse.d.ts +8 -0
  51. package/v4/classic/parse.js +9 -0
  52. package/v4/classic/schemas.cjs +59 -4
  53. package/v4/classic/schemas.d.cts +48 -2
  54. package/v4/classic/schemas.d.ts +48 -2
  55. package/v4/classic/schemas.js +51 -3
  56. package/v4/core/api.cjs +19 -24
  57. package/v4/core/api.d.cts +3 -4
  58. package/v4/core/api.d.ts +3 -4
  59. package/v4/core/api.js +19 -24
  60. package/v4/core/core.cjs +8 -1
  61. package/v4/core/core.d.cts +3 -0
  62. package/v4/core/core.d.ts +3 -0
  63. package/v4/core/core.js +6 -0
  64. package/v4/core/index.cjs +0 -1
  65. package/v4/core/index.d.cts +0 -1
  66. package/v4/core/index.d.ts +0 -1
  67. package/v4/core/index.js +0 -1
  68. package/v4/core/json-schema.d.cts +1 -0
  69. package/v4/core/json-schema.d.ts +1 -0
  70. package/v4/core/parse.cjs +45 -1
  71. package/v4/core/parse.d.cts +24 -0
  72. package/v4/core/parse.d.ts +24 -0
  73. package/v4/core/parse.js +36 -0
  74. package/v4/core/regexes.cjs +34 -2
  75. package/v4/core/regexes.d.cts +16 -0
  76. package/v4/core/regexes.d.ts +16 -0
  77. package/v4/core/regexes.js +32 -1
  78. package/v4/core/schemas.cjs +309 -77
  79. package/v4/core/schemas.d.cts +61 -3
  80. package/v4/core/schemas.d.ts +61 -3
  81. package/v4/core/schemas.js +308 -76
  82. package/v4/core/to-json-schema.cjs +42 -5
  83. package/v4/core/to-json-schema.d.cts +4 -3
  84. package/v4/core/to-json-schema.d.ts +4 -3
  85. package/v4/core/to-json-schema.js +42 -5
  86. package/v4/core/util.cjs +69 -0
  87. package/v4/core/util.d.cts +10 -0
  88. package/v4/core/util.d.ts +10 -0
  89. package/v4/core/util.js +62 -0
  90. package/v4/mini/external.cjs +1 -2
  91. package/v4/mini/external.d.cts +1 -1
  92. package/v4/mini/external.d.ts +1 -1
  93. package/v4/mini/external.js +1 -1
  94. package/v4/mini/parse.cjs +9 -1
  95. package/v4/mini/parse.d.cts +1 -1
  96. package/v4/mini/parse.d.ts +1 -1
  97. package/v4/mini/parse.js +1 -1
  98. package/v4/mini/schemas.cjs +58 -3
  99. package/v4/mini/schemas.d.cts +49 -1
  100. package/v4/mini/schemas.d.ts +49 -1
  101. package/v4/mini/schemas.js +49 -2
  102. package/src/v4/core/function.ts +0 -176
  103. package/v4/core/function.cjs +0 -102
  104. package/v4/core/function.d.cts +0 -52
  105. package/v4/core/function.d.ts +0 -52
  106. package/v4/core/function.js +0 -75
@@ -1476,7 +1476,7 @@ type RawIssue<T extends errors.$ZodIssueBase> = T extends any
1476
1476
  util.MakePartial<T, "message" | "path"> & {
1477
1477
  /** The schema or check that originated this issue. */
1478
1478
  readonly inst?: schemas.$ZodType | checks.$ZodCheck;
1479
- /** If `true`, Zod will continue executing checks/refinements after this issue. */
1479
+ /** If `true`, Zod will execute subsequent checks/refinements instead of immediately aborting */
1480
1480
  readonly continue?: boolean | undefined;
1481
1481
  } & Record<string, unknown>
1482
1482
  >
@@ -1498,7 +1498,7 @@ export function _superRefine<T>(fn: (arg: T, payload: $RefinementCtx<T>) => void
1498
1498
  _issue.code ??= "custom";
1499
1499
  _issue.input ??= payload.value;
1500
1500
  _issue.inst ??= ch;
1501
- _issue.continue ??= !ch._zod.def.abort;
1501
+ _issue.continue ??= !ch._zod.def.abort; // abort is always undefined, so this is always true...
1502
1502
  payload.issues.push(util.issue(_issue));
1503
1503
  }
1504
1504
  };
@@ -1536,16 +1536,12 @@ export interface $ZodStringBoolParams extends TypeParams {
1536
1536
 
1537
1537
  export function _stringbool(
1538
1538
  Classes: {
1539
- Pipe?: typeof schemas.$ZodPipe;
1539
+ Codec?: typeof schemas.$ZodCodec;
1540
1540
  Boolean?: typeof schemas.$ZodBoolean;
1541
- Transform?: typeof schemas.$ZodTransform;
1542
1541
  String?: typeof schemas.$ZodString;
1543
1542
  },
1544
1543
  _params?: string | $ZodStringBoolParams
1545
- ): schemas.$ZodPipe<
1546
- schemas.$ZodPipe<schemas.$ZodString, schemas.$ZodTransform<boolean, string>>,
1547
- schemas.$ZodBoolean<boolean>
1548
- > {
1544
+ ): schemas.$ZodCodec<schemas.$ZodString, schemas.$ZodBoolean> {
1549
1545
  const params = util.normalizeParams(_params);
1550
1546
 
1551
1547
  let truthyArray = params.truthy ?? ["true", "1", "yes", "on", "y", "enabled"];
@@ -1558,15 +1554,19 @@ export function _stringbool(
1558
1554
  const truthySet = new Set(truthyArray);
1559
1555
  const falsySet = new Set(falsyArray);
1560
1556
 
1561
- const _Pipe = Classes.Pipe ?? schemas.$ZodPipe;
1557
+ const _Codec = Classes.Codec ?? schemas.$ZodCodec;
1562
1558
  const _Boolean = Classes.Boolean ?? schemas.$ZodBoolean;
1563
1559
  const _String = Classes.String ?? schemas.$ZodString;
1564
- const _Transform = Classes.Transform ?? schemas.$ZodTransform;
1565
1560
 
1566
- const tx = new _Transform({
1567
- type: "transform",
1568
- transform: (input, payload: schemas.ParsePayload<unknown>) => {
1569
- let data: string = input as string;
1561
+ const stringSchema = new _String({ type: "string", error: params.error });
1562
+ const booleanSchema = new _Boolean({ type: "boolean", error: params.error });
1563
+
1564
+ const codec = new _Codec({
1565
+ type: "pipe",
1566
+ in: stringSchema as any,
1567
+ out: booleanSchema as any,
1568
+ transform: ((input: string, payload: schemas.ParsePayload<string>) => {
1569
+ let data: string = input;
1570
1570
  if (params.case !== "sensitive") data = data.toLowerCase();
1571
1571
  if (truthySet.has(data)) {
1572
1572
  return true;
@@ -1578,33 +1578,23 @@ export function _stringbool(
1578
1578
  expected: "stringbool",
1579
1579
  values: [...truthySet, ...falsySet],
1580
1580
  input: payload.value,
1581
- inst: tx,
1581
+ inst: codec,
1582
1582
  continue: false,
1583
1583
  });
1584
1584
  return {} as never;
1585
1585
  }
1586
- },
1587
- error: params.error,
1588
- });
1589
- // params.error;
1590
-
1591
- const innerPipe = new _Pipe({
1592
- type: "pipe",
1593
- in: new _String({ type: "string", error: params.error }),
1594
- out: tx,
1586
+ }) as any,
1587
+ reverseTransform: ((input: boolean, _payload: schemas.ParsePayload<boolean>) => {
1588
+ if (input === true) {
1589
+ return truthyArray[0] || "true";
1590
+ } else {
1591
+ return falsyArray[0] || "false";
1592
+ }
1593
+ }) as any,
1595
1594
  error: params.error,
1596
- });
1595
+ }) as any;
1597
1596
 
1598
- const outerPipe = new _Pipe({
1599
- type: "pipe",
1600
- in: innerPipe,
1601
- out: new _Boolean({
1602
- type: "boolean",
1603
- error: params.error,
1604
- }),
1605
- error: params.error,
1606
- });
1607
- return outerPipe as any;
1597
+ return codec;
1608
1598
  }
1609
1599
 
1610
1600
  export function _stringFormat<Format extends string>(
@@ -78,6 +78,13 @@ export class $ZodAsyncError extends Error {
78
78
  }
79
79
  }
80
80
 
81
+ export class $ZodEncodeError extends Error {
82
+ constructor(name: string) {
83
+ super(`Encountered unidirectional transform during encode: ${name}`);
84
+ this.name = "ZodEncodeError";
85
+ }
86
+ }
87
+
81
88
  //////////////////////////// TYPE HELPERS ///////////////////////////////////
82
89
 
83
90
  // export type input<T extends schemas.$ZodType> = T["_zod"]["input"];
@@ -87,32 +94,6 @@ export class $ZodAsyncError extends Error {
87
94
  export type input<T> = T extends { _zod: { input: any } } ? T["_zod"]["input"] : unknown;
88
95
  export type output<T> = T extends { _zod: { output: any } } ? T["_zod"]["output"] : unknown;
89
96
 
90
- // Mk2
91
- // export type input<T> = T extends { _zod: { "~input": any } }
92
- // ? T["_zod"]["~input"]
93
- // : T extends { _zod: { input: any } }
94
- // ? T["_zod"]["input"]
95
- // : never;
96
- // export type output<T> = T extends { _zod: { "~output": any } }
97
- // ? T["_zod"]["~output"]
98
- // : T extends { _zod: { output: any } }
99
- // ? T["_zod"]["output"]
100
- // : never;
101
- // Mk 3
102
- // export type input<T extends schemas.$ZodType> = T["_zod"]["input"];
103
- // export type output<T extends schemas.$ZodType> = T["_zod"]["output"];
104
- // Mk 4
105
- // export type input<T extends schemas.$ZodType> = T[] extends { _zod: { "~input": any } }
106
- // ? T["_zod"]["~input"]
107
- // : T extends { _zod: { input: any } }
108
- // ? T["_zod"]["input"]
109
- // : never;
110
- // export type output<T extends schemas.$ZodType> = T extends { _zod: { "~output": any } }
111
- // ? T["_zod"]["~output"]
112
- // : T extends { _zod: { output: any } }
113
- // ? T["_zod"]["output"]
114
- // : never;
115
-
116
97
  export type { output as infer };
117
98
 
118
99
  ////////////////////////////// CONFIG ///////////////////////////////////////
@@ -9,7 +9,6 @@ export * as regexes from "./regexes.js";
9
9
  export * as locales from "../locales/index.js";
10
10
  export * from "./registries.js";
11
11
  export * from "./doc.js";
12
- export * from "./function.js";
13
12
  export * from "./api.js";
14
13
  export * from "./to-json-schema.js";
15
14
  export * as JSONSchema from "./json-schema.js";
@@ -104,6 +104,7 @@ export type JSONSchema = {
104
104
  deprecated?: boolean;
105
105
  readOnly?: boolean;
106
106
  writeOnly?: boolean;
107
+ nullable?: boolean;
107
108
  examples?: unknown[];
108
109
  format?: string;
109
110
  contentMediaType?: string;
@@ -92,3 +92,104 @@ export const _safeParseAsync: (_Err: $ZodErrorClass) => $SafeParseAsync = (_Err)
92
92
  };
93
93
 
94
94
  export const safeParseAsync: $SafeParseAsync = /* @__PURE__*/ _safeParseAsync(errors.$ZodRealError);
95
+
96
+ // Codec functions
97
+ export type $Encode = <T extends schemas.$ZodType>(
98
+ schema: T,
99
+ value: core.output<T>,
100
+ _ctx?: schemas.ParseContext<errors.$ZodIssue>
101
+ ) => core.input<T>;
102
+
103
+ export const _encode: (_Err: $ZodErrorClass) => $Encode = (_Err) => (schema, value, _ctx) => {
104
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" as const }) : { direction: "backward" as const };
105
+ return _parse(_Err)(schema, value, ctx as any) as any;
106
+ };
107
+
108
+ export const encode: $Encode = /* @__PURE__*/ _encode(errors.$ZodRealError);
109
+
110
+ export type $Decode = <T extends schemas.$ZodType>(
111
+ schema: T,
112
+ value: core.input<T>,
113
+ _ctx?: schemas.ParseContext<errors.$ZodIssue>
114
+ ) => core.output<T>;
115
+
116
+ export const _decode: (_Err: $ZodErrorClass) => $Decode = (_Err) => (schema, value, _ctx) => {
117
+ return _parse(_Err)(schema, value, _ctx);
118
+ };
119
+
120
+ export const decode: $Decode = /* @__PURE__*/ _decode(errors.$ZodRealError);
121
+
122
+ export type $EncodeAsync = <T extends schemas.$ZodType>(
123
+ schema: T,
124
+ value: core.output<T>,
125
+ _ctx?: schemas.ParseContext<errors.$ZodIssue>
126
+ ) => Promise<core.input<T>>;
127
+
128
+ export const _encodeAsync: (_Err: $ZodErrorClass) => $EncodeAsync = (_Err) => async (schema, value, _ctx) => {
129
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" as const }) : { direction: "backward" as const };
130
+ return _parseAsync(_Err)(schema, value, ctx as any) as any;
131
+ };
132
+
133
+ export const encodeAsync: $EncodeAsync = /* @__PURE__*/ _encodeAsync(errors.$ZodRealError);
134
+
135
+ export type $DecodeAsync = <T extends schemas.$ZodType>(
136
+ schema: T,
137
+ value: core.input<T>,
138
+ _ctx?: schemas.ParseContext<errors.$ZodIssue>
139
+ ) => Promise<core.output<T>>;
140
+
141
+ export const _decodeAsync: (_Err: $ZodErrorClass) => $DecodeAsync = (_Err) => async (schema, value, _ctx) => {
142
+ return _parseAsync(_Err)(schema, value, _ctx);
143
+ };
144
+
145
+ export const decodeAsync: $DecodeAsync = /* @__PURE__*/ _decodeAsync(errors.$ZodRealError);
146
+
147
+ export type $SafeEncode = <T extends schemas.$ZodType>(
148
+ schema: T,
149
+ value: core.output<T>,
150
+ _ctx?: schemas.ParseContext<errors.$ZodIssue>
151
+ ) => util.SafeParseResult<core.input<T>>;
152
+
153
+ export const _safeEncode: (_Err: $ZodErrorClass) => $SafeEncode = (_Err) => (schema, value, _ctx) => {
154
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" as const }) : { direction: "backward" as const };
155
+ return _safeParse(_Err)(schema, value, ctx as any) as any;
156
+ };
157
+
158
+ export const safeEncode: $SafeEncode = /* @__PURE__*/ _safeEncode(errors.$ZodRealError);
159
+
160
+ export type $SafeDecode = <T extends schemas.$ZodType>(
161
+ schema: T,
162
+ value: core.input<T>,
163
+ _ctx?: schemas.ParseContext<errors.$ZodIssue>
164
+ ) => util.SafeParseResult<core.output<T>>;
165
+
166
+ export const _safeDecode: (_Err: $ZodErrorClass) => $SafeDecode = (_Err) => (schema, value, _ctx) => {
167
+ return _safeParse(_Err)(schema, value, _ctx);
168
+ };
169
+
170
+ export const safeDecode: $SafeDecode = /* @__PURE__*/ _safeDecode(errors.$ZodRealError);
171
+
172
+ export type $SafeEncodeAsync = <T extends schemas.$ZodType>(
173
+ schema: T,
174
+ value: core.output<T>,
175
+ _ctx?: schemas.ParseContext<errors.$ZodIssue>
176
+ ) => Promise<util.SafeParseResult<core.input<T>>>;
177
+
178
+ export const _safeEncodeAsync: (_Err: $ZodErrorClass) => $SafeEncodeAsync = (_Err) => async (schema, value, _ctx) => {
179
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" as const }) : { direction: "backward" as const };
180
+ return _safeParseAsync(_Err)(schema, value, ctx as any) as any;
181
+ };
182
+
183
+ export const safeEncodeAsync: $SafeEncodeAsync = /* @__PURE__*/ _safeEncodeAsync(errors.$ZodRealError);
184
+
185
+ export type $SafeDecodeAsync = <T extends schemas.$ZodType>(
186
+ schema: T,
187
+ value: core.input<T>,
188
+ _ctx?: schemas.ParseContext<errors.$ZodIssue>
189
+ ) => Promise<util.SafeParseResult<core.output<T>>>;
190
+
191
+ export const _safeDecodeAsync: (_Err: $ZodErrorClass) => $SafeDecodeAsync = (_Err) => async (schema, value, _ctx) => {
192
+ return _safeParseAsync(_Err)(schema, value, _ctx);
193
+ };
194
+
195
+ export const safeDecodeAsync: $SafeDecodeAsync = /* @__PURE__*/ _safeDecodeAsync(errors.$ZodRealError);
@@ -21,7 +21,7 @@ export const guid: RegExp = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9
21
21
  * @param version Optionally specify a version 1-8. If no version is specified, all versions are supported. */
22
22
  export const uuid = (version?: number | undefined): RegExp => {
23
23
  if (!version)
24
- return /^([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)$/;
24
+ return /^([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)$/;
25
25
  return new RegExp(
26
26
  `^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`
27
27
  );
@@ -136,3 +136,42 @@ export { _undefined as undefined };
136
136
  export const lowercase: RegExp = /^[^A-Z]*$/;
137
137
  // regex for string with no lowercase letters
138
138
  export const uppercase: RegExp = /^[^a-z]*$/;
139
+
140
+ // regex for hexadecimal strings (any length)
141
+ export const hex: RegExp = /^[0-9a-fA-F]*$/;
142
+
143
+ // Hash regexes for different algorithms and encodings
144
+ // Helper function to create base64 regex with exact length and padding
145
+ function fixedBase64(bodyLength: number, padding: "" | "=" | "=="): RegExp {
146
+ return new RegExp(`^[A-Za-z0-9+/]{${bodyLength}}${padding}$`);
147
+ }
148
+
149
+ // Helper function to create base64url regex with exact length (no padding)
150
+ function fixedBase64url(length: number): RegExp {
151
+ return new RegExp(`^[A-Za-z0-9-_]{${length}}$`);
152
+ }
153
+
154
+ // MD5 (16 bytes): base64 = 24 chars total (22 + "==")
155
+ export const md5_hex: RegExp = /^[0-9a-fA-F]{32}$/;
156
+ export const md5_base64: RegExp = /*@__PURE__*/ fixedBase64(22, "==");
157
+ export const md5_base64url: RegExp = /*@__PURE__*/ fixedBase64url(22);
158
+
159
+ // SHA1 (20 bytes): base64 = 28 chars total (27 + "=")
160
+ export const sha1_hex: RegExp = /^[0-9a-fA-F]{40}$/;
161
+ export const sha1_base64: RegExp = /*@__PURE__*/ fixedBase64(27, "=");
162
+ export const sha1_base64url: RegExp = /*@__PURE__*/ fixedBase64url(27);
163
+
164
+ // SHA256 (32 bytes): base64 = 44 chars total (43 + "=")
165
+ export const sha256_hex: RegExp = /^[0-9a-fA-F]{64}$/;
166
+ export const sha256_base64: RegExp = /*@__PURE__*/ fixedBase64(43, "=");
167
+ export const sha256_base64url: RegExp = /*@__PURE__*/ fixedBase64url(43);
168
+
169
+ // SHA384 (48 bytes): base64 = 64 chars total (no padding)
170
+ export const sha384_hex: RegExp = /^[0-9a-fA-F]{96}$/;
171
+ export const sha384_base64: RegExp = /*@__PURE__*/ fixedBase64(64, "");
172
+ export const sha384_base64url: RegExp = /*@__PURE__*/ fixedBase64url(64);
173
+
174
+ // SHA512 (64 bytes): base64 = 88 chars total (86 + "==")
175
+ export const sha512_hex: RegExp = /^[0-9a-fA-F]{128}$/;
176
+ export const sha512_base64: RegExp = /*@__PURE__*/ fixedBase64(86, "==");
177
+ export const sha512_base64url: RegExp = /*@__PURE__*/ fixedBase64url(86);