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
package/v4/core/api.js CHANGED
@@ -838,7 +838,7 @@ export function _superRefine(fn) {
838
838
  _issue.code ?? (_issue.code = "custom");
839
839
  _issue.input ?? (_issue.input = payload.value);
840
840
  _issue.inst ?? (_issue.inst = ch);
841
- _issue.continue ?? (_issue.continue = !ch._zod.def.abort);
841
+ _issue.continue ?? (_issue.continue = !ch._zod.def.abort); // abort is always undefined, so this is always true...
842
842
  payload.issues.push(util.issue(_issue));
843
843
  }
844
844
  };
@@ -864,13 +864,16 @@ export function _stringbool(Classes, _params) {
864
864
  }
865
865
  const truthySet = new Set(truthyArray);
866
866
  const falsySet = new Set(falsyArray);
867
- const _Pipe = Classes.Pipe ?? schemas.$ZodPipe;
867
+ const _Codec = Classes.Codec ?? schemas.$ZodCodec;
868
868
  const _Boolean = Classes.Boolean ?? schemas.$ZodBoolean;
869
869
  const _String = Classes.String ?? schemas.$ZodString;
870
- const _Transform = Classes.Transform ?? schemas.$ZodTransform;
871
- const tx = new _Transform({
872
- type: "transform",
873
- transform: (input, payload) => {
870
+ const stringSchema = new _String({ type: "string", error: params.error });
871
+ const booleanSchema = new _Boolean({ type: "boolean", error: params.error });
872
+ const codec = new _Codec({
873
+ type: "pipe",
874
+ in: stringSchema,
875
+ out: booleanSchema,
876
+ transform: ((input, payload) => {
874
877
  let data = input;
875
878
  if (params.case !== "sensitive")
876
879
  data = data.toLowerCase();
@@ -886,31 +889,23 @@ export function _stringbool(Classes, _params) {
886
889
  expected: "stringbool",
887
890
  values: [...truthySet, ...falsySet],
888
891
  input: payload.value,
889
- inst: tx,
892
+ inst: codec,
890
893
  continue: false,
891
894
  });
892
895
  return {};
893
896
  }
894
- },
895
- error: params.error,
896
- });
897
- // params.error;
898
- const innerPipe = new _Pipe({
899
- type: "pipe",
900
- in: new _String({ type: "string", error: params.error }),
901
- out: tx,
902
- error: params.error,
903
- });
904
- const outerPipe = new _Pipe({
905
- type: "pipe",
906
- in: innerPipe,
907
- out: new _Boolean({
908
- type: "boolean",
909
- error: params.error,
897
+ }),
898
+ reverseTransform: ((input, _payload) => {
899
+ if (input === true) {
900
+ return truthyArray[0] || "true";
901
+ }
902
+ else {
903
+ return falsyArray[0] || "false";
904
+ }
910
905
  }),
911
906
  error: params.error,
912
907
  });
913
- return outerPipe;
908
+ return codec;
914
909
  }
915
910
  export function _stringFormat(Class, format, fnOrRegex, _params = {}) {
916
911
  const params = util.normalizeParams(_params);
package/v4/core/core.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.globalConfig = exports.$ZodAsyncError = exports.$brand = exports.NEVER = void 0;
3
+ exports.globalConfig = exports.$ZodEncodeError = exports.$ZodAsyncError = exports.$brand = exports.NEVER = void 0;
4
4
  exports.$constructor = $constructor;
5
5
  exports.config = config;
6
6
  /** A special constant with type `never` */
@@ -59,6 +59,13 @@ class $ZodAsyncError extends Error {
59
59
  }
60
60
  }
61
61
  exports.$ZodAsyncError = $ZodAsyncError;
62
+ class $ZodEncodeError extends Error {
63
+ constructor(name) {
64
+ super(`Encountered unidirectional transform during encode: ${name}`);
65
+ this.name = "ZodEncodeError";
66
+ }
67
+ }
68
+ exports.$ZodEncodeError = $ZodEncodeError;
62
69
  exports.globalConfig = {};
63
70
  function config(newConfig) {
64
71
  if (newConfig)
@@ -26,6 +26,9 @@ export type $ZodBranded<T extends schemas.SomeType, Brand extends string | numbe
26
26
  export declare class $ZodAsyncError extends Error {
27
27
  constructor();
28
28
  }
29
+ export declare class $ZodEncodeError extends Error {
30
+ constructor(name: string);
31
+ }
29
32
  export type input<T> = T extends {
30
33
  _zod: {
31
34
  input: any;
package/v4/core/core.d.ts CHANGED
@@ -26,6 +26,9 @@ export type $ZodBranded<T extends schemas.SomeType, Brand extends string | numbe
26
26
  export declare class $ZodAsyncError extends Error {
27
27
  constructor();
28
28
  }
29
+ export declare class $ZodEncodeError extends Error {
30
+ constructor(name: string);
31
+ }
29
32
  export type input<T> = T extends {
30
33
  _zod: {
31
34
  input: any;
package/v4/core/core.js CHANGED
@@ -53,6 +53,12 @@ export class $ZodAsyncError extends Error {
53
53
  super(`Encountered Promise during synchronous parse. Use .parseAsync() instead.`);
54
54
  }
55
55
  }
56
+ export class $ZodEncodeError extends Error {
57
+ constructor(name) {
58
+ super(`Encountered unidirectional transform during encode: ${name}`);
59
+ this.name = "ZodEncodeError";
60
+ }
61
+ }
56
62
  export const globalConfig = {};
57
63
  export function config(newConfig) {
58
64
  if (newConfig)
package/v4/core/index.cjs CHANGED
@@ -38,7 +38,6 @@ exports.regexes = __importStar(require("./regexes.cjs"));
38
38
  exports.locales = __importStar(require("../locales/index.cjs"));
39
39
  __exportStar(require("./registries.cjs"), exports);
40
40
  __exportStar(require("./doc.cjs"), exports);
41
- __exportStar(require("./function.cjs"), exports);
42
41
  __exportStar(require("./api.cjs"), exports);
43
42
  __exportStar(require("./to-json-schema.cjs"), exports);
44
43
  exports.JSONSchema = __importStar(require("./json-schema.cjs"));
@@ -9,7 +9,6 @@ export * as regexes from "./regexes.cjs";
9
9
  export * as locales from "../locales/index.cjs";
10
10
  export * from "./registries.cjs";
11
11
  export * from "./doc.cjs";
12
- export * from "./function.cjs";
13
12
  export * from "./api.cjs";
14
13
  export * from "./to-json-schema.cjs";
15
14
  export * as JSONSchema from "./json-schema.cjs";
@@ -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";
package/v4/core/index.js CHANGED
@@ -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";
@@ -56,6 +56,7 @@ export type JSONSchema = {
56
56
  deprecated?: boolean;
57
57
  readOnly?: boolean;
58
58
  writeOnly?: boolean;
59
+ nullable?: boolean;
59
60
  examples?: unknown[];
60
61
  format?: string;
61
62
  contentMediaType?: string;
@@ -56,6 +56,7 @@ export type JSONSchema = {
56
56
  deprecated?: boolean;
57
57
  readOnly?: boolean;
58
58
  writeOnly?: boolean;
59
+ nullable?: boolean;
59
60
  examples?: unknown[];
60
61
  format?: string;
61
62
  contentMediaType?: string;
package/v4/core/parse.cjs CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.safeParseAsync = exports._safeParseAsync = exports.safeParse = exports._safeParse = exports.parseAsync = exports._parseAsync = exports.parse = exports._parse = void 0;
26
+ exports.safeDecodeAsync = exports._safeDecodeAsync = exports.safeEncodeAsync = exports._safeEncodeAsync = exports.safeDecode = exports._safeDecode = exports.safeEncode = exports._safeEncode = exports.decodeAsync = exports._decodeAsync = exports.encodeAsync = exports._encodeAsync = exports.decode = exports._decode = exports.encode = exports._encode = exports.safeParseAsync = exports._safeParseAsync = exports.safeParse = exports._safeParse = exports.parseAsync = exports._parseAsync = exports.parse = exports._parse = void 0;
27
27
  const core = __importStar(require("./core.cjs"));
28
28
  const errors = __importStar(require("./errors.cjs"));
29
29
  const util = __importStar(require("./util.cjs"));
@@ -85,3 +85,47 @@ const _safeParseAsync = (_Err) => async (schema, value, _ctx) => {
85
85
  };
86
86
  exports._safeParseAsync = _safeParseAsync;
87
87
  exports.safeParseAsync = (0, exports._safeParseAsync)(errors.$ZodRealError);
88
+ const _encode = (_Err) => (schema, value, _ctx) => {
89
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
90
+ return (0, exports._parse)(_Err)(schema, value, ctx);
91
+ };
92
+ exports._encode = _encode;
93
+ exports.encode = (0, exports._encode)(errors.$ZodRealError);
94
+ const _decode = (_Err) => (schema, value, _ctx) => {
95
+ return (0, exports._parse)(_Err)(schema, value, _ctx);
96
+ };
97
+ exports._decode = _decode;
98
+ exports.decode = (0, exports._decode)(errors.$ZodRealError);
99
+ const _encodeAsync = (_Err) => async (schema, value, _ctx) => {
100
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
101
+ return (0, exports._parseAsync)(_Err)(schema, value, ctx);
102
+ };
103
+ exports._encodeAsync = _encodeAsync;
104
+ exports.encodeAsync = (0, exports._encodeAsync)(errors.$ZodRealError);
105
+ const _decodeAsync = (_Err) => async (schema, value, _ctx) => {
106
+ return (0, exports._parseAsync)(_Err)(schema, value, _ctx);
107
+ };
108
+ exports._decodeAsync = _decodeAsync;
109
+ exports.decodeAsync = (0, exports._decodeAsync)(errors.$ZodRealError);
110
+ const _safeEncode = (_Err) => (schema, value, _ctx) => {
111
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
112
+ return (0, exports._safeParse)(_Err)(schema, value, ctx);
113
+ };
114
+ exports._safeEncode = _safeEncode;
115
+ exports.safeEncode = (0, exports._safeEncode)(errors.$ZodRealError);
116
+ const _safeDecode = (_Err) => (schema, value, _ctx) => {
117
+ return (0, exports._safeParse)(_Err)(schema, value, _ctx);
118
+ };
119
+ exports._safeDecode = _safeDecode;
120
+ exports.safeDecode = (0, exports._safeDecode)(errors.$ZodRealError);
121
+ const _safeEncodeAsync = (_Err) => async (schema, value, _ctx) => {
122
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
123
+ return (0, exports._safeParseAsync)(_Err)(schema, value, ctx);
124
+ };
125
+ exports._safeEncodeAsync = _safeEncodeAsync;
126
+ exports.safeEncodeAsync = (0, exports._safeEncodeAsync)(errors.$ZodRealError);
127
+ const _safeDecodeAsync = (_Err) => async (schema, value, _ctx) => {
128
+ return (0, exports._safeParseAsync)(_Err)(schema, value, _ctx);
129
+ };
130
+ exports._safeDecodeAsync = _safeDecodeAsync;
131
+ exports.safeDecodeAsync = (0, exports._safeDecodeAsync)(errors.$ZodRealError);
@@ -23,3 +23,27 @@ export declare const safeParse: $SafeParse;
23
23
  export type $SafeParseAsync = <T extends schemas.$ZodType>(schema: T, value: unknown, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<util.SafeParseResult<core.output<T>>>;
24
24
  export declare const _safeParseAsync: (_Err: $ZodErrorClass) => $SafeParseAsync;
25
25
  export declare const safeParseAsync: $SafeParseAsync;
26
+ export type $Encode = <T extends schemas.$ZodType>(schema: T, value: core.output<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => core.input<T>;
27
+ export declare const _encode: (_Err: $ZodErrorClass) => $Encode;
28
+ export declare const encode: $Encode;
29
+ export type $Decode = <T extends schemas.$ZodType>(schema: T, value: core.input<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => core.output<T>;
30
+ export declare const _decode: (_Err: $ZodErrorClass) => $Decode;
31
+ export declare const decode: $Decode;
32
+ export type $EncodeAsync = <T extends schemas.$ZodType>(schema: T, value: core.output<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<core.input<T>>;
33
+ export declare const _encodeAsync: (_Err: $ZodErrorClass) => $EncodeAsync;
34
+ export declare const encodeAsync: $EncodeAsync;
35
+ export type $DecodeAsync = <T extends schemas.$ZodType>(schema: T, value: core.input<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<core.output<T>>;
36
+ export declare const _decodeAsync: (_Err: $ZodErrorClass) => $DecodeAsync;
37
+ export declare const decodeAsync: $DecodeAsync;
38
+ export type $SafeEncode = <T extends schemas.$ZodType>(schema: T, value: core.output<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => util.SafeParseResult<core.input<T>>;
39
+ export declare const _safeEncode: (_Err: $ZodErrorClass) => $SafeEncode;
40
+ export declare const safeEncode: $SafeEncode;
41
+ export type $SafeDecode = <T extends schemas.$ZodType>(schema: T, value: core.input<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => util.SafeParseResult<core.output<T>>;
42
+ export declare const _safeDecode: (_Err: $ZodErrorClass) => $SafeDecode;
43
+ export declare const safeDecode: $SafeDecode;
44
+ export type $SafeEncodeAsync = <T extends schemas.$ZodType>(schema: T, value: core.output<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<util.SafeParseResult<core.input<T>>>;
45
+ export declare const _safeEncodeAsync: (_Err: $ZodErrorClass) => $SafeEncodeAsync;
46
+ export declare const safeEncodeAsync: $SafeEncodeAsync;
47
+ export type $SafeDecodeAsync = <T extends schemas.$ZodType>(schema: T, value: core.input<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<util.SafeParseResult<core.output<T>>>;
48
+ export declare const _safeDecodeAsync: (_Err: $ZodErrorClass) => $SafeDecodeAsync;
49
+ export declare const safeDecodeAsync: $SafeDecodeAsync;
@@ -23,3 +23,27 @@ export declare const safeParse: $SafeParse;
23
23
  export type $SafeParseAsync = <T extends schemas.$ZodType>(schema: T, value: unknown, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<util.SafeParseResult<core.output<T>>>;
24
24
  export declare const _safeParseAsync: (_Err: $ZodErrorClass) => $SafeParseAsync;
25
25
  export declare const safeParseAsync: $SafeParseAsync;
26
+ export type $Encode = <T extends schemas.$ZodType>(schema: T, value: core.output<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => core.input<T>;
27
+ export declare const _encode: (_Err: $ZodErrorClass) => $Encode;
28
+ export declare const encode: $Encode;
29
+ export type $Decode = <T extends schemas.$ZodType>(schema: T, value: core.input<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => core.output<T>;
30
+ export declare const _decode: (_Err: $ZodErrorClass) => $Decode;
31
+ export declare const decode: $Decode;
32
+ export type $EncodeAsync = <T extends schemas.$ZodType>(schema: T, value: core.output<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<core.input<T>>;
33
+ export declare const _encodeAsync: (_Err: $ZodErrorClass) => $EncodeAsync;
34
+ export declare const encodeAsync: $EncodeAsync;
35
+ export type $DecodeAsync = <T extends schemas.$ZodType>(schema: T, value: core.input<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<core.output<T>>;
36
+ export declare const _decodeAsync: (_Err: $ZodErrorClass) => $DecodeAsync;
37
+ export declare const decodeAsync: $DecodeAsync;
38
+ export type $SafeEncode = <T extends schemas.$ZodType>(schema: T, value: core.output<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => util.SafeParseResult<core.input<T>>;
39
+ export declare const _safeEncode: (_Err: $ZodErrorClass) => $SafeEncode;
40
+ export declare const safeEncode: $SafeEncode;
41
+ export type $SafeDecode = <T extends schemas.$ZodType>(schema: T, value: core.input<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => util.SafeParseResult<core.output<T>>;
42
+ export declare const _safeDecode: (_Err: $ZodErrorClass) => $SafeDecode;
43
+ export declare const safeDecode: $SafeDecode;
44
+ export type $SafeEncodeAsync = <T extends schemas.$ZodType>(schema: T, value: core.output<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<util.SafeParseResult<core.input<T>>>;
45
+ export declare const _safeEncodeAsync: (_Err: $ZodErrorClass) => $SafeEncodeAsync;
46
+ export declare const safeEncodeAsync: $SafeEncodeAsync;
47
+ export type $SafeDecodeAsync = <T extends schemas.$ZodType>(schema: T, value: core.input<T>, _ctx?: schemas.ParseContext<errors.$ZodIssue>) => Promise<util.SafeParseResult<core.output<T>>>;
48
+ export declare const _safeDecodeAsync: (_Err: $ZodErrorClass) => $SafeDecodeAsync;
49
+ export declare const safeDecodeAsync: $SafeDecodeAsync;
package/v4/core/parse.js CHANGED
@@ -55,3 +55,39 @@ export const _safeParseAsync = (_Err) => async (schema, value, _ctx) => {
55
55
  : { success: true, data: result.value };
56
56
  };
57
57
  export const safeParseAsync = /* @__PURE__*/ _safeParseAsync(errors.$ZodRealError);
58
+ export const _encode = (_Err) => (schema, value, _ctx) => {
59
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
60
+ return _parse(_Err)(schema, value, ctx);
61
+ };
62
+ export const encode = /* @__PURE__*/ _encode(errors.$ZodRealError);
63
+ export const _decode = (_Err) => (schema, value, _ctx) => {
64
+ return _parse(_Err)(schema, value, _ctx);
65
+ };
66
+ export const decode = /* @__PURE__*/ _decode(errors.$ZodRealError);
67
+ export const _encodeAsync = (_Err) => async (schema, value, _ctx) => {
68
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
69
+ return _parseAsync(_Err)(schema, value, ctx);
70
+ };
71
+ export const encodeAsync = /* @__PURE__*/ _encodeAsync(errors.$ZodRealError);
72
+ export const _decodeAsync = (_Err) => async (schema, value, _ctx) => {
73
+ return _parseAsync(_Err)(schema, value, _ctx);
74
+ };
75
+ export const decodeAsync = /* @__PURE__*/ _decodeAsync(errors.$ZodRealError);
76
+ export const _safeEncode = (_Err) => (schema, value, _ctx) => {
77
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
78
+ return _safeParse(_Err)(schema, value, ctx);
79
+ };
80
+ export const safeEncode = /* @__PURE__*/ _safeEncode(errors.$ZodRealError);
81
+ export const _safeDecode = (_Err) => (schema, value, _ctx) => {
82
+ return _safeParse(_Err)(schema, value, _ctx);
83
+ };
84
+ export const safeDecode = /* @__PURE__*/ _safeDecode(errors.$ZodRealError);
85
+ export const _safeEncodeAsync = (_Err) => async (schema, value, _ctx) => {
86
+ const ctx = _ctx ? Object.assign(_ctx, { direction: "backward" }) : { direction: "backward" };
87
+ return _safeParseAsync(_Err)(schema, value, ctx);
88
+ };
89
+ export const safeEncodeAsync = /* @__PURE__*/ _safeEncodeAsync(errors.$ZodRealError);
90
+ export const _safeDecodeAsync = (_Err) => async (schema, value, _ctx) => {
91
+ return _safeParseAsync(_Err)(schema, value, _ctx);
92
+ };
93
+ export const safeDecodeAsync = /* @__PURE__*/ _safeDecodeAsync(errors.$ZodRealError);
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uppercase = exports.lowercase = exports.undefined = exports.null = exports.boolean = exports.number = exports.integer = exports.bigint = exports.string = exports.date = exports.e164 = exports.domain = exports.hostname = exports.base64url = exports.base64 = exports.cidrv6 = exports.cidrv4 = exports.ipv6 = exports.ipv4 = exports.browserEmail = exports.idnEmail = exports.unicodeEmail = exports.rfc5322Email = exports.html5Email = exports.email = exports.uuid7 = exports.uuid6 = exports.uuid4 = exports.uuid = exports.guid = exports.extendedDuration = exports.duration = exports.nanoid = exports.ksuid = exports.xid = exports.ulid = exports.cuid2 = exports.cuid = void 0;
3
+ exports.sha384_base64 = exports.sha384_hex = exports.sha256_base64url = exports.sha256_base64 = exports.sha256_hex = exports.sha1_base64url = exports.sha1_base64 = exports.sha1_hex = exports.md5_base64url = exports.md5_base64 = exports.md5_hex = exports.hex = exports.uppercase = exports.lowercase = exports.undefined = exports.null = exports.boolean = exports.number = exports.integer = exports.bigint = exports.string = exports.date = exports.e164 = exports.domain = exports.hostname = exports.base64url = exports.base64 = exports.cidrv6 = exports.cidrv4 = exports.ipv6 = exports.ipv4 = exports.browserEmail = exports.idnEmail = exports.unicodeEmail = exports.rfc5322Email = exports.html5Email = exports.email = exports.uuid7 = exports.uuid6 = exports.uuid4 = exports.uuid = exports.guid = exports.extendedDuration = exports.duration = exports.nanoid = exports.ksuid = exports.xid = exports.ulid = exports.cuid2 = exports.cuid = void 0;
4
+ exports.sha512_base64url = exports.sha512_base64 = exports.sha512_hex = exports.sha384_base64url = void 0;
4
5
  exports.emoji = emoji;
5
6
  exports.time = time;
6
7
  exports.datetime = datetime;
@@ -21,7 +22,7 @@ exports.guid = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0
21
22
  * @param version Optionally specify a version 1-8. If no version is specified, all versions are supported. */
22
23
  const uuid = (version) => {
23
24
  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)$/;
25
+ 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
26
  return new RegExp(`^([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})$`);
26
27
  };
27
28
  exports.uuid = uuid;
@@ -102,3 +103,34 @@ exports.undefined = _undefined;
102
103
  exports.lowercase = /^[^A-Z]*$/;
103
104
  // regex for string with no lowercase letters
104
105
  exports.uppercase = /^[^a-z]*$/;
106
+ // regex for hexadecimal strings (any length)
107
+ exports.hex = /^[0-9a-fA-F]*$/;
108
+ // Hash regexes for different algorithms and encodings
109
+ // Helper function to create base64 regex with exact length and padding
110
+ function fixedBase64(bodyLength, padding) {
111
+ return new RegExp(`^[A-Za-z0-9+/]{${bodyLength}}${padding}$`);
112
+ }
113
+ // Helper function to create base64url regex with exact length (no padding)
114
+ function fixedBase64url(length) {
115
+ return new RegExp(`^[A-Za-z0-9-_]{${length}}$`);
116
+ }
117
+ // MD5 (16 bytes): base64 = 24 chars total (22 + "==")
118
+ exports.md5_hex = /^[0-9a-fA-F]{32}$/;
119
+ exports.md5_base64 = fixedBase64(22, "==");
120
+ exports.md5_base64url = fixedBase64url(22);
121
+ // SHA1 (20 bytes): base64 = 28 chars total (27 + "=")
122
+ exports.sha1_hex = /^[0-9a-fA-F]{40}$/;
123
+ exports.sha1_base64 = fixedBase64(27, "=");
124
+ exports.sha1_base64url = fixedBase64url(27);
125
+ // SHA256 (32 bytes): base64 = 44 chars total (43 + "=")
126
+ exports.sha256_hex = /^[0-9a-fA-F]{64}$/;
127
+ exports.sha256_base64 = fixedBase64(43, "=");
128
+ exports.sha256_base64url = fixedBase64url(43);
129
+ // SHA384 (48 bytes): base64 = 64 chars total (no padding)
130
+ exports.sha384_hex = /^[0-9a-fA-F]{96}$/;
131
+ exports.sha384_base64 = fixedBase64(64, "");
132
+ exports.sha384_base64url = fixedBase64url(64);
133
+ // SHA512 (64 bytes): base64 = 88 chars total (86 + "==")
134
+ exports.sha512_hex = /^[0-9a-fA-F]{128}$/;
135
+ exports.sha512_base64 = fixedBase64(86, "==");
136
+ exports.sha512_base64url = fixedBase64url(86);
@@ -60,3 +60,19 @@ declare const _undefined: RegExp;
60
60
  export { _undefined as undefined };
61
61
  export declare const lowercase: RegExp;
62
62
  export declare const uppercase: RegExp;
63
+ export declare const hex: RegExp;
64
+ export declare const md5_hex: RegExp;
65
+ export declare const md5_base64: RegExp;
66
+ export declare const md5_base64url: RegExp;
67
+ export declare const sha1_hex: RegExp;
68
+ export declare const sha1_base64: RegExp;
69
+ export declare const sha1_base64url: RegExp;
70
+ export declare const sha256_hex: RegExp;
71
+ export declare const sha256_base64: RegExp;
72
+ export declare const sha256_base64url: RegExp;
73
+ export declare const sha384_hex: RegExp;
74
+ export declare const sha384_base64: RegExp;
75
+ export declare const sha384_base64url: RegExp;
76
+ export declare const sha512_hex: RegExp;
77
+ export declare const sha512_base64: RegExp;
78
+ export declare const sha512_base64url: RegExp;
@@ -60,3 +60,19 @@ declare const _undefined: RegExp;
60
60
  export { _undefined as undefined };
61
61
  export declare const lowercase: RegExp;
62
62
  export declare const uppercase: RegExp;
63
+ export declare const hex: RegExp;
64
+ export declare const md5_hex: RegExp;
65
+ export declare const md5_base64: RegExp;
66
+ export declare const md5_base64url: RegExp;
67
+ export declare const sha1_hex: RegExp;
68
+ export declare const sha1_base64: RegExp;
69
+ export declare const sha1_base64url: RegExp;
70
+ export declare const sha256_hex: RegExp;
71
+ export declare const sha256_base64: RegExp;
72
+ export declare const sha256_base64url: RegExp;
73
+ export declare const sha384_hex: RegExp;
74
+ export declare const sha384_base64: RegExp;
75
+ export declare const sha384_base64url: RegExp;
76
+ export declare const sha512_hex: RegExp;
77
+ export declare const sha512_base64: RegExp;
78
+ export declare const sha512_base64url: RegExp;
@@ -15,7 +15,7 @@ export const guid = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{
15
15
  * @param version Optionally specify a version 1-8. If no version is specified, all versions are supported. */
16
16
  export const uuid = (version) => {
17
17
  if (!version)
18
- 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)$/;
18
+ 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)$/;
19
19
  return new RegExp(`^([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})$`);
20
20
  };
21
21
  export const uuid4 = /*@__PURE__*/ uuid(4);
@@ -94,3 +94,34 @@ export { _undefined as undefined };
94
94
  export const lowercase = /^[^A-Z]*$/;
95
95
  // regex for string with no lowercase letters
96
96
  export const uppercase = /^[^a-z]*$/;
97
+ // regex for hexadecimal strings (any length)
98
+ export const hex = /^[0-9a-fA-F]*$/;
99
+ // Hash regexes for different algorithms and encodings
100
+ // Helper function to create base64 regex with exact length and padding
101
+ function fixedBase64(bodyLength, padding) {
102
+ return new RegExp(`^[A-Za-z0-9+/]{${bodyLength}}${padding}$`);
103
+ }
104
+ // Helper function to create base64url regex with exact length (no padding)
105
+ function fixedBase64url(length) {
106
+ return new RegExp(`^[A-Za-z0-9-_]{${length}}$`);
107
+ }
108
+ // MD5 (16 bytes): base64 = 24 chars total (22 + "==")
109
+ export const md5_hex = /^[0-9a-fA-F]{32}$/;
110
+ export const md5_base64 = /*@__PURE__*/ fixedBase64(22, "==");
111
+ export const md5_base64url = /*@__PURE__*/ fixedBase64url(22);
112
+ // SHA1 (20 bytes): base64 = 28 chars total (27 + "=")
113
+ export const sha1_hex = /^[0-9a-fA-F]{40}$/;
114
+ export const sha1_base64 = /*@__PURE__*/ fixedBase64(27, "=");
115
+ export const sha1_base64url = /*@__PURE__*/ fixedBase64url(27);
116
+ // SHA256 (32 bytes): base64 = 44 chars total (43 + "=")
117
+ export const sha256_hex = /^[0-9a-fA-F]{64}$/;
118
+ export const sha256_base64 = /*@__PURE__*/ fixedBase64(43, "=");
119
+ export const sha256_base64url = /*@__PURE__*/ fixedBase64url(43);
120
+ // SHA384 (48 bytes): base64 = 64 chars total (no padding)
121
+ export const sha384_hex = /^[0-9a-fA-F]{96}$/;
122
+ export const sha384_base64 = /*@__PURE__*/ fixedBase64(64, "");
123
+ export const sha384_base64url = /*@__PURE__*/ fixedBase64url(64);
124
+ // SHA512 (64 bytes): base64 = 88 chars total (86 + "==")
125
+ export const sha512_hex = /^[0-9a-fA-F]{128}$/;
126
+ export const sha512_base64 = /*@__PURE__*/ fixedBase64(86, "==");
127
+ export const sha512_base64url = /*@__PURE__*/ fixedBase64url(86);