zod 3.17.10 → 3.18.0
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 +59 -4
- package/lib/helpers/util.d.ts +18 -16
- package/lib/helpers/util.js +3 -2
- package/lib/index.mjs +29 -3
- package/lib/index.umd.js +30 -2
- package/lib/types.d.ts +19 -6
- package/lib/types.js +26 -2
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
- [Sponsors](#sponsors)
|
|
48
48
|
- [Ecosystem](#ecosystem)
|
|
49
49
|
- [Installation](#installation)
|
|
50
|
-
- [Node/npm](#
|
|
50
|
+
- [Node/npm](#nodenpm)
|
|
51
51
|
- [Deno](#deno)
|
|
52
52
|
- [Basic usage](#basic-usage)
|
|
53
53
|
- [Primitives](#primitives)
|
|
@@ -90,6 +90,7 @@
|
|
|
90
90
|
- [Instanceof](#instanceof)
|
|
91
91
|
- [Function schemas](#function-schemas)
|
|
92
92
|
- [Preprocess](#preprocess)
|
|
93
|
+
- [Branded types](#branded-types)
|
|
93
94
|
- [Schema methods](#schema-methods)
|
|
94
95
|
- [.parse](#parse)
|
|
95
96
|
- [.parseAsync](#parseasync)
|
|
@@ -227,7 +228,7 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
|
|
|
227
228
|
<a href="https://seasoned.cc">seasoned.cc</a>
|
|
228
229
|
</td>
|
|
229
230
|
<td align="center">
|
|
230
|
-
<a href="https://
|
|
231
|
+
<a href="https://interval.com">
|
|
231
232
|
<img src="https://avatars.githubusercontent.com/u/67802063?s=200&v=4" width="150px;" alt="" />
|
|
232
233
|
</a>
|
|
233
234
|
<br />
|
|
@@ -284,6 +285,17 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
|
|
|
284
285
|
<a href="https://adaptable.io/">adaptable.io</a>
|
|
285
286
|
<br />
|
|
286
287
|
</td>
|
|
288
|
+
<td align="center">
|
|
289
|
+
<a href="https://www.avanawallet.com/">
|
|
290
|
+
<img src="https://avatars.githubusercontent.com/u/105452197?s=200&v=4" width="100px;" alt="Avana Wallet logo"/>
|
|
291
|
+
</a>
|
|
292
|
+
<br />
|
|
293
|
+
<b>Avana Wallet</b>
|
|
294
|
+
<br/>
|
|
295
|
+
<a href="https://www.avanawallet.com/">avanawallet.com</a><br/>
|
|
296
|
+
<span>Solana non-custodial wallet</span>
|
|
297
|
+
<br />
|
|
298
|
+
</td>
|
|
287
299
|
</tr>
|
|
288
300
|
</table>
|
|
289
301
|
|
|
@@ -316,6 +328,8 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
316
328
|
- [`nestjs-graphql-zod`](https://github.com/incetarik/nestjs-graphql-zod): Generates NestJS GraphQL model classes from Zod schemas dynamically and provides GraphQL method decorators working with Zod schemas.
|
|
317
329
|
- [`zod-xlsx`](https://github.com/sidwebworks/zod-xlsx): A xlsx based resource validator using Zod schemas.
|
|
318
330
|
- [`remix-domains`](https://github.com/SeasonedSoftware/remix-domains/): Improves end-to-end type safety in [Remix](https://remix.run/) by leveraging Zod to parse the framework's inputs such as FormData, URLSearchParams, etc.
|
|
331
|
+
- [`@zodios/core`](https://github.com/ecyrbe/zodios): A typescript API client with runtime and compile time validation backed by axios and zod.
|
|
332
|
+
- [`@runtyping/zod`](https://github.com/johngeorgewright/runtyping/tree/master/packages/zod): Generate zod from static types & JSON schema.
|
|
319
333
|
|
|
320
334
|
#### Form integrations
|
|
321
335
|
|
|
@@ -576,7 +590,7 @@ z.date().max(new Date(), { message: "Too young!" });
|
|
|
576
590
|
|
|
577
591
|
**Supporting date strings**
|
|
578
592
|
|
|
579
|
-
To write a schema that accepts either a `Date` or a date string, use
|
|
593
|
+
To write a schema that accepts either a `Date` or a date string, use [`z.preprocess`](#preprocess).
|
|
580
594
|
|
|
581
595
|
```ts
|
|
582
596
|
const dateSchema = z.preprocess((arg) => {
|
|
@@ -1452,7 +1466,7 @@ All Zod schemas contain certain methods.
|
|
|
1452
1466
|
|
|
1453
1467
|
### `.parse`
|
|
1454
1468
|
|
|
1455
|
-
`.parse(data:unknown): T`
|
|
1469
|
+
`.parse(data: unknown): T`
|
|
1456
1470
|
|
|
1457
1471
|
Given any Zod schema, you can call its `.parse` method to check `data` is valid. If it is, a value is returned with full type information! Otherwise, an error is thrown.
|
|
1458
1472
|
|
|
@@ -1864,6 +1878,47 @@ z.object({ name: z.string() }).and(z.object({ age: z.number() })); // { name: st
|
|
|
1864
1878
|
z.intersection(z.object({ name: z.string() }), z.object({ age: z.number() }));
|
|
1865
1879
|
```
|
|
1866
1880
|
|
|
1881
|
+
### `.brand`
|
|
1882
|
+
|
|
1883
|
+
`.brand<T>() => ZodBranded<this, B>`
|
|
1884
|
+
|
|
1885
|
+
TypeScript's type system is structural, which means that any two types that are structurally equivalent are considered the same.
|
|
1886
|
+
|
|
1887
|
+
```ts
|
|
1888
|
+
type Cat = { name: string };
|
|
1889
|
+
type Dog = { name: string };
|
|
1890
|
+
|
|
1891
|
+
const petCat = (cat: Cat) => {};
|
|
1892
|
+
const fido: Dog = { name: "fido" };
|
|
1893
|
+
petCat(fido); // works fine
|
|
1894
|
+
```
|
|
1895
|
+
|
|
1896
|
+
In some cases, its can be desirable to simulate _nominal typing_ inside TypeScript. For instance, you may wish to write a function that only accepts an input that has been validated by Zod. This can be achieved with _branded types_ (AKA _opaque types_).
|
|
1897
|
+
|
|
1898
|
+
```ts
|
|
1899
|
+
const Cat = z.object({ name: z.string }).brand<"Cat">();
|
|
1900
|
+
type Cat = z.infer<typeof Cat>;
|
|
1901
|
+
|
|
1902
|
+
const petCat = (cat: Cat) => {};
|
|
1903
|
+
|
|
1904
|
+
// this works
|
|
1905
|
+
const simba = Cat.parse({ name: "simba" });
|
|
1906
|
+
petCat(simba);
|
|
1907
|
+
|
|
1908
|
+
// this doesn't
|
|
1909
|
+
petCat({ name: "fido" });
|
|
1910
|
+
```
|
|
1911
|
+
|
|
1912
|
+
Under the hood, this works by attaching a "brand" to the inferred type using an intersection type. This way, plain/unbranded data structures are no longer assignable to the inferred type of the schema.
|
|
1913
|
+
|
|
1914
|
+
```ts
|
|
1915
|
+
const Cat = z.object({ name: z.string }).brand<"Cat">();
|
|
1916
|
+
type Cat = z.infer<typeof Cat>;
|
|
1917
|
+
// {name: string} & {[symbol]: "Cat"}
|
|
1918
|
+
```
|
|
1919
|
+
|
|
1920
|
+
Note that branded types do not affect the runtime result of `.parse`. It is a static-only construct.
|
|
1921
|
+
|
|
1867
1922
|
## Guides and concepts
|
|
1868
1923
|
|
|
1869
1924
|
### Type inference
|
package/lib/helpers/util.d.ts
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
export declare namespace util {
|
|
2
|
-
type AssertEqual<T,
|
|
3
|
-
|
|
4
|
-
function
|
|
5
|
-
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
type
|
|
2
|
+
type AssertEqual<T, U> = (<V>() => V extends T ? 1 : 2) extends <V>() => V extends U ? 1 : 2 ? true : false;
|
|
3
|
+
export const assertEqual: <A, B>(val: AssertEqual<A, B>) => AssertEqual<A, B>;
|
|
4
|
+
export function assertIs<T>(_arg: T): void;
|
|
5
|
+
export function assertNever(_x: never): never;
|
|
6
|
+
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
|
7
|
+
export type OmitKeys<T, K extends string> = Pick<T, Exclude<keyof T, K>>;
|
|
8
|
+
export type MakePartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
9
|
+
export const arrayToEnum: <T extends string, U extends [T, ...T[]]>(items: U) => { [k in U[number]]: k; };
|
|
10
|
+
export const getValidEnumValues: (obj: any) => any[];
|
|
11
|
+
export const objectValues: (obj: any) => any[];
|
|
12
|
+
export const objectKeys: ObjectConstructor["keys"];
|
|
13
|
+
export const find: <T>(arr: T[], checker: (arg: T) => any) => T | undefined;
|
|
14
|
+
export type identity<T> = T;
|
|
15
|
+
export type flatten<T> = identity<{
|
|
15
16
|
[k in keyof T]: T[k];
|
|
16
17
|
}>;
|
|
17
|
-
type noUndefined<T> = T extends undefined ? never : T;
|
|
18
|
-
const isInteger: NumberConstructor["isInteger"];
|
|
19
|
-
function joinValues<T extends any[]>(array: T, separator?: string): string;
|
|
18
|
+
export type noUndefined<T> = T extends undefined ? never : T;
|
|
19
|
+
export const isInteger: NumberConstructor["isInteger"];
|
|
20
|
+
export function joinValues<T extends any[]>(array: T, separator?: string): string;
|
|
21
|
+
export {};
|
|
20
22
|
}
|
|
21
23
|
export declare const ZodParsedType: {
|
|
22
24
|
function: "function";
|
package/lib/helpers/util.js
CHANGED
|
@@ -3,8 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getParsedType = exports.ZodParsedType = exports.util = void 0;
|
|
4
4
|
var util;
|
|
5
5
|
(function (util) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
util.assertEqual = (val) => val;
|
|
7
|
+
function assertIs(_arg) { }
|
|
8
|
+
util.assertIs = assertIs;
|
|
8
9
|
function assertNever(_x) {
|
|
9
10
|
throw new Error();
|
|
10
11
|
}
|
package/lib/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
var util;
|
|
2
2
|
(function (util) {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
util.assertEqual = (val) => val;
|
|
4
|
+
function assertIs(_arg) { }
|
|
5
|
+
util.assertIs = assertIs;
|
|
5
6
|
function assertNever(_x) {
|
|
6
7
|
throw new Error();
|
|
7
8
|
}
|
|
@@ -706,6 +707,13 @@ class ZodType {
|
|
|
706
707
|
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
707
708
|
});
|
|
708
709
|
}
|
|
710
|
+
brand() {
|
|
711
|
+
return new ZodBranded({
|
|
712
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
713
|
+
type: this,
|
|
714
|
+
...processCreateParams(undefined),
|
|
715
|
+
});
|
|
716
|
+
}
|
|
709
717
|
describe(description) {
|
|
710
718
|
const This = this.constructor;
|
|
711
719
|
return new This({
|
|
@@ -2872,6 +2880,21 @@ ZodNaN.create = (params) => {
|
|
|
2872
2880
|
...processCreateParams(params),
|
|
2873
2881
|
});
|
|
2874
2882
|
};
|
|
2883
|
+
const BRAND = Symbol("zod_brand");
|
|
2884
|
+
class ZodBranded extends ZodType {
|
|
2885
|
+
_parse(input) {
|
|
2886
|
+
const { ctx } = this._processInputParams(input);
|
|
2887
|
+
const data = ctx.data;
|
|
2888
|
+
return this._def.type._parse({
|
|
2889
|
+
data,
|
|
2890
|
+
path: ctx.path,
|
|
2891
|
+
parent: ctx,
|
|
2892
|
+
});
|
|
2893
|
+
}
|
|
2894
|
+
unwrap() {
|
|
2895
|
+
return this._def.type;
|
|
2896
|
+
}
|
|
2897
|
+
}
|
|
2875
2898
|
const custom = (check, params = {}, fatal) => {
|
|
2876
2899
|
if (check)
|
|
2877
2900
|
return ZodAny.create().superRefine((data, ctx) => {
|
|
@@ -2919,6 +2942,7 @@ var ZodFirstPartyTypeKind;
|
|
|
2919
2942
|
ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
|
|
2920
2943
|
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
2921
2944
|
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
2945
|
+
ZodFirstPartyTypeKind["ZodBranded"] = "ZodBranded";
|
|
2922
2946
|
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
2923
2947
|
const instanceOfType = (cls, params = {
|
|
2924
2948
|
message: `Input not instance of ${cls.name}`,
|
|
@@ -3009,6 +3033,8 @@ var mod = /*#__PURE__*/Object.freeze({
|
|
|
3009
3033
|
ZodNullable: ZodNullable,
|
|
3010
3034
|
ZodDefault: ZodDefault,
|
|
3011
3035
|
ZodNaN: ZodNaN,
|
|
3036
|
+
BRAND: BRAND,
|
|
3037
|
+
ZodBranded: ZodBranded,
|
|
3012
3038
|
custom: custom,
|
|
3013
3039
|
Schema: ZodType,
|
|
3014
3040
|
ZodSchema: ZodType,
|
|
@@ -3059,4 +3085,4 @@ var mod = /*#__PURE__*/Object.freeze({
|
|
|
3059
3085
|
getErrorMap: getErrorMap
|
|
3060
3086
|
});
|
|
3061
3087
|
|
|
3062
|
-
export { DIRTY, EMPTY_PATH, INVALID, OK, ParseStatus, ZodType as Schema, ZodAny, ZodArray, ZodBigInt, ZodBoolean, ZodDate, ZodDefault, ZodDiscriminatedUnion, ZodEffects, ZodEnum, ZodError, ZodFirstPartyTypeKind, ZodFunction, ZodIntersection, ZodIssueCode, ZodLazy, ZodLiteral, ZodMap, ZodNaN, ZodNativeEnum, ZodNever, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodParsedType, ZodPromise, ZodRecord, ZodType as ZodSchema, ZodSet, ZodString, ZodEffects as ZodTransformer, ZodTuple, ZodType, ZodUndefined, ZodUnion, ZodUnknown, ZodVoid, addIssueToContext, anyType as any, arrayType as array, bigIntType as bigint, booleanType as boolean, custom, dateType as date, mod as default, defaultErrorMap, discriminatedUnionType as discriminatedUnion, effectsType as effect, enumType as enum, functionType as function, getErrorMap, getParsedType, instanceOfType as instanceof, intersectionType as intersection, isAborted, isAsync, isDirty, isValid, jsonStringifyReplacer, late, lazyType as lazy, literalType as literal, makeIssue, mapType as map, nanType as nan, nativeEnumType as nativeEnum, neverType as never, nullType as null, nullableType as nullable, numberType as number, objectType as object, objectUtil, oboolean, onumber, optionalType as optional, ostring, preprocessType as preprocess, promiseType as promise, quotelessJson, recordType as record, setType as set, setErrorMap, strictObjectType as strictObject, stringType as string, effectsType as transformer, tupleType as tuple, undefinedType as undefined, unionType as union, unknownType as unknown, voidType as void, mod as z };
|
|
3088
|
+
export { BRAND, DIRTY, EMPTY_PATH, INVALID, OK, ParseStatus, ZodType as Schema, ZodAny, ZodArray, ZodBigInt, ZodBoolean, ZodBranded, ZodDate, ZodDefault, ZodDiscriminatedUnion, ZodEffects, ZodEnum, ZodError, ZodFirstPartyTypeKind, ZodFunction, ZodIntersection, ZodIssueCode, ZodLazy, ZodLiteral, ZodMap, ZodNaN, ZodNativeEnum, ZodNever, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodParsedType, ZodPromise, ZodRecord, ZodType as ZodSchema, ZodSet, ZodString, ZodEffects as ZodTransformer, ZodTuple, ZodType, ZodUndefined, ZodUnion, ZodUnknown, ZodVoid, addIssueToContext, anyType as any, arrayType as array, bigIntType as bigint, booleanType as boolean, custom, dateType as date, mod as default, defaultErrorMap, discriminatedUnionType as discriminatedUnion, effectsType as effect, enumType as enum, functionType as function, getErrorMap, getParsedType, instanceOfType as instanceof, intersectionType as intersection, isAborted, isAsync, isDirty, isValid, jsonStringifyReplacer, late, lazyType as lazy, literalType as literal, makeIssue, mapType as map, nanType as nan, nativeEnumType as nativeEnum, neverType as never, nullType as null, nullableType as nullable, numberType as number, objectType as object, objectUtil, oboolean, onumber, optionalType as optional, ostring, preprocessType as preprocess, promiseType as promise, quotelessJson, recordType as record, setType as set, setErrorMap, strictObjectType as strictObject, stringType as string, effectsType as transformer, tupleType as tuple, undefinedType as undefined, unionType as union, unknownType as unknown, voidType as void, mod as z };
|
package/lib/index.umd.js
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
var util;
|
|
8
8
|
(function (util) {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
util.assertEqual = (val) => val;
|
|
10
|
+
function assertIs(_arg) { }
|
|
11
|
+
util.assertIs = assertIs;
|
|
11
12
|
function assertNever(_x) {
|
|
12
13
|
throw new Error();
|
|
13
14
|
}
|
|
@@ -712,6 +713,13 @@
|
|
|
712
713
|
typeName: exports.ZodFirstPartyTypeKind.ZodDefault,
|
|
713
714
|
});
|
|
714
715
|
}
|
|
716
|
+
brand() {
|
|
717
|
+
return new ZodBranded({
|
|
718
|
+
typeName: exports.ZodFirstPartyTypeKind.ZodBranded,
|
|
719
|
+
type: this,
|
|
720
|
+
...processCreateParams(undefined),
|
|
721
|
+
});
|
|
722
|
+
}
|
|
715
723
|
describe(description) {
|
|
716
724
|
const This = this.constructor;
|
|
717
725
|
return new This({
|
|
@@ -2878,6 +2886,21 @@
|
|
|
2878
2886
|
...processCreateParams(params),
|
|
2879
2887
|
});
|
|
2880
2888
|
};
|
|
2889
|
+
const BRAND = Symbol("zod_brand");
|
|
2890
|
+
class ZodBranded extends ZodType {
|
|
2891
|
+
_parse(input) {
|
|
2892
|
+
const { ctx } = this._processInputParams(input);
|
|
2893
|
+
const data = ctx.data;
|
|
2894
|
+
return this._def.type._parse({
|
|
2895
|
+
data,
|
|
2896
|
+
path: ctx.path,
|
|
2897
|
+
parent: ctx,
|
|
2898
|
+
});
|
|
2899
|
+
}
|
|
2900
|
+
unwrap() {
|
|
2901
|
+
return this._def.type;
|
|
2902
|
+
}
|
|
2903
|
+
}
|
|
2881
2904
|
const custom = (check, params = {}, fatal) => {
|
|
2882
2905
|
if (check)
|
|
2883
2906
|
return ZodAny.create().superRefine((data, ctx) => {
|
|
@@ -2925,6 +2948,7 @@
|
|
|
2925
2948
|
ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
|
|
2926
2949
|
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
2927
2950
|
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
2951
|
+
ZodFirstPartyTypeKind["ZodBranded"] = "ZodBranded";
|
|
2928
2952
|
})(exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
|
|
2929
2953
|
const instanceOfType = (cls, params = {
|
|
2930
2954
|
message: `Input not instance of ${cls.name}`,
|
|
@@ -3015,6 +3039,8 @@
|
|
|
3015
3039
|
ZodNullable: ZodNullable,
|
|
3016
3040
|
ZodDefault: ZodDefault,
|
|
3017
3041
|
ZodNaN: ZodNaN,
|
|
3042
|
+
BRAND: BRAND,
|
|
3043
|
+
ZodBranded: ZodBranded,
|
|
3018
3044
|
custom: custom,
|
|
3019
3045
|
Schema: ZodType,
|
|
3020
3046
|
ZodSchema: ZodType,
|
|
@@ -3065,6 +3091,7 @@
|
|
|
3065
3091
|
getErrorMap: getErrorMap
|
|
3066
3092
|
});
|
|
3067
3093
|
|
|
3094
|
+
exports.BRAND = BRAND;
|
|
3068
3095
|
exports.DIRTY = DIRTY;
|
|
3069
3096
|
exports.EMPTY_PATH = EMPTY_PATH;
|
|
3070
3097
|
exports.INVALID = INVALID;
|
|
@@ -3075,6 +3102,7 @@
|
|
|
3075
3102
|
exports.ZodArray = ZodArray;
|
|
3076
3103
|
exports.ZodBigInt = ZodBigInt;
|
|
3077
3104
|
exports.ZodBoolean = ZodBoolean;
|
|
3105
|
+
exports.ZodBranded = ZodBranded;
|
|
3078
3106
|
exports.ZodDate = ZodDate;
|
|
3079
3107
|
exports.ZodDefault = ZodDefault;
|
|
3080
3108
|
exports.ZodDiscriminatedUnion = ZodDiscriminatedUnion;
|
package/lib/types.d.ts
CHANGED
|
@@ -75,6 +75,7 @@ export declare abstract class ZodType<Output = any, Def extends ZodTypeDef = Zod
|
|
|
75
75
|
transform<NewOut>(transform: (arg: Output, ctx: RefinementCtx) => NewOut | Promise<NewOut>): ZodEffects<this, NewOut>;
|
|
76
76
|
default(def: util.noUndefined<Input>): ZodDefault<this>;
|
|
77
77
|
default(def: () => util.noUndefined<Input>): ZodDefault<this>;
|
|
78
|
+
brand<B extends string | number | symbol>(): ZodBranded<this, B>;
|
|
78
79
|
describe(description: string): this;
|
|
79
80
|
isOptional(): boolean;
|
|
80
81
|
isNullable(): boolean;
|
|
@@ -353,9 +354,6 @@ export declare type objectInputType<Shape extends ZodRawShape, Catchall extends
|
|
|
353
354
|
declare type deoptional<T extends ZodTypeAny> = T extends ZodOptional<infer U> ? deoptional<U> : T;
|
|
354
355
|
export declare type SomeZodObject = ZodObject<ZodRawShape, UnknownKeysParam, ZodTypeAny, any, any>;
|
|
355
356
|
export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends UnknownKeysParam = "strip", Catchall extends ZodTypeAny = ZodTypeAny, Output = objectOutputType<T, Catchall>, Input = objectInputType<T, Catchall>> extends ZodType<Output, ZodObjectDef<T, UnknownKeys, Catchall>, Input> {
|
|
356
|
-
readonly _shape: T;
|
|
357
|
-
readonly _unknownKeys: UnknownKeys;
|
|
358
|
-
readonly _catchall: Catchall;
|
|
359
357
|
private _cached;
|
|
360
358
|
_getCached(): {
|
|
361
359
|
shape: T;
|
|
@@ -381,7 +379,7 @@ export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends Unknow
|
|
|
381
379
|
* inferred type of merged objects. Please
|
|
382
380
|
* upgrade if you are experiencing issues.
|
|
383
381
|
*/
|
|
384
|
-
merge<Incoming extends AnyZodObject>(merging: Incoming): ZodObject<extendShape<T, Incoming["
|
|
382
|
+
merge<Incoming extends AnyZodObject>(merging: Incoming): ZodObject<extendShape<T, ReturnType<Incoming["_def"]["shape"]>>, UnknownKeys, Catchall>;
|
|
385
383
|
catchall<Index extends ZodTypeAny>(index: Index): ZodObject<T, UnknownKeys, Index>;
|
|
386
384
|
pick<Mask extends {
|
|
387
385
|
[k in keyof T]?: true;
|
|
@@ -666,6 +664,20 @@ export declare class ZodNaN extends ZodType<number, ZodNaNDef> {
|
|
|
666
664
|
_parse(input: ParseInput): ParseReturnType<any>;
|
|
667
665
|
static create: (params?: RawCreateParams) => ZodNaN;
|
|
668
666
|
}
|
|
667
|
+
export interface ZodBrandedDef<T extends ZodTypeAny> extends ZodTypeDef {
|
|
668
|
+
type: T;
|
|
669
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded;
|
|
670
|
+
}
|
|
671
|
+
export declare const BRAND: unique symbol;
|
|
672
|
+
export declare type BRAND<T extends string | number | symbol> = {
|
|
673
|
+
[BRAND]: {
|
|
674
|
+
[k in T]: true;
|
|
675
|
+
};
|
|
676
|
+
};
|
|
677
|
+
export declare class ZodBranded<T extends ZodTypeAny, B extends string | number | symbol> extends ZodType<T["_output"] & BRAND<B>, ZodBrandedDef<T>, T["_input"] & BRAND<B>> {
|
|
678
|
+
_parse(input: ParseInput): ParseReturnType<any>;
|
|
679
|
+
unwrap(): T;
|
|
680
|
+
}
|
|
669
681
|
export declare const custom: <T>(check?: ((data: unknown) => any) | undefined, params?: Parameters<ZodTypeAny["refine"]>[1], fatal?: boolean | undefined) => ZodType<T, ZodTypeDef, T>;
|
|
670
682
|
export { ZodType as Schema, ZodType as ZodSchema };
|
|
671
683
|
export declare const late: {
|
|
@@ -702,9 +714,10 @@ export declare enum ZodFirstPartyTypeKind {
|
|
|
702
714
|
ZodOptional = "ZodOptional",
|
|
703
715
|
ZodNullable = "ZodNullable",
|
|
704
716
|
ZodDefault = "ZodDefault",
|
|
705
|
-
ZodPromise = "ZodPromise"
|
|
717
|
+
ZodPromise = "ZodPromise",
|
|
718
|
+
ZodBranded = "ZodBranded"
|
|
706
719
|
}
|
|
707
|
-
export declare type ZodFirstPartySchemaTypes = ZodString | ZodNumber | ZodNaN | ZodBigInt | ZodBoolean | ZodDate | ZodUndefined | ZodNull | ZodAny | ZodUnknown | ZodNever | ZodVoid | ZodArray<any, any> | ZodObject<any, any, any, any, any> | ZodUnion<any> | ZodDiscriminatedUnion<any, any, any> | ZodIntersection<any, any> | ZodTuple<any, any> | ZodRecord<any, any> | ZodMap<any> | ZodSet<any> | ZodFunction<any, any> | ZodLazy<any> | ZodLiteral<any> | ZodEnum<any> | ZodEffects<any, any, any> | ZodNativeEnum<any> | ZodOptional<any> | ZodNullable<any> | ZodDefault<any> | ZodPromise<any>;
|
|
720
|
+
export declare type ZodFirstPartySchemaTypes = ZodString | ZodNumber | ZodNaN | ZodBigInt | ZodBoolean | ZodDate | ZodUndefined | ZodNull | ZodAny | ZodUnknown | ZodNever | ZodVoid | ZodArray<any, any> | ZodObject<any, any, any, any, any> | ZodUnion<any> | ZodDiscriminatedUnion<any, any, any> | ZodIntersection<any, any> | ZodTuple<any, any> | ZodRecord<any, any> | ZodMap<any> | ZodSet<any> | ZodFunction<any, any> | ZodLazy<any> | ZodLiteral<any> | ZodEnum<any> | ZodEffects<any, any, any> | ZodNativeEnum<any> | ZodOptional<any> | ZodNullable<any> | ZodDefault<any> | ZodPromise<any> | ZodBranded<any, any>;
|
|
708
721
|
declare const instanceOfType: <T extends new (...args: any[]) => any>(cls: T, params?: Parameters<ZodTypeAny["refine"]>[1]) => ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>;
|
|
709
722
|
declare const stringType: (params?: RawCreateParams) => ZodString;
|
|
710
723
|
declare const numberType: (params?: RawCreateParams) => ZodNumber;
|
package/lib/types.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.void = exports.unknown = exports.union = exports.undefined = exports.tuple = exports.transformer = exports.string = exports.strictObject = exports.set = exports.record = exports.promise = exports.preprocess = exports.ostring = exports.optional = exports.onumber = exports.oboolean = exports.object = exports.number = exports.nullable = exports.null = exports.never = exports.nativeEnum = exports.nan = exports.map = exports.literal = exports.lazy = void 0;
|
|
3
|
+
exports.function = exports.enum = exports.effect = exports.discriminatedUnion = exports.date = exports.boolean = exports.bigint = exports.array = exports.any = exports.ZodFirstPartyTypeKind = exports.late = exports.ZodSchema = exports.Schema = exports.custom = exports.ZodBranded = exports.BRAND = exports.ZodNaN = exports.ZodDefault = exports.ZodNullable = exports.ZodOptional = exports.ZodTransformer = exports.ZodEffects = exports.ZodPromise = exports.ZodNativeEnum = exports.ZodEnum = exports.ZodLiteral = exports.ZodLazy = exports.ZodFunction = exports.ZodSet = exports.ZodMap = exports.ZodRecord = exports.ZodTuple = exports.ZodIntersection = exports.ZodDiscriminatedUnion = exports.ZodUnion = exports.ZodObject = exports.objectUtil = exports.ZodArray = exports.ZodVoid = exports.ZodNever = exports.ZodUnknown = exports.ZodAny = exports.ZodNull = exports.ZodUndefined = exports.ZodDate = exports.ZodBoolean = exports.ZodBigInt = exports.ZodNumber = exports.ZodString = exports.ZodType = void 0;
|
|
4
|
+
exports.void = exports.unknown = exports.union = exports.undefined = exports.tuple = exports.transformer = exports.string = exports.strictObject = exports.set = exports.record = exports.promise = exports.preprocess = exports.ostring = exports.optional = exports.onumber = exports.oboolean = exports.object = exports.number = exports.nullable = exports.null = exports.never = exports.nativeEnum = exports.nan = exports.map = exports.literal = exports.lazy = exports.intersection = exports.instanceof = void 0;
|
|
5
5
|
const errorUtil_1 = require("./helpers/errorUtil");
|
|
6
6
|
const parseUtil_1 = require("./helpers/parseUtil");
|
|
7
7
|
const util_1 = require("./helpers/util");
|
|
@@ -257,6 +257,13 @@ class ZodType {
|
|
|
257
257
|
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
258
258
|
});
|
|
259
259
|
}
|
|
260
|
+
brand() {
|
|
261
|
+
return new ZodBranded({
|
|
262
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
263
|
+
type: this,
|
|
264
|
+
...processCreateParams(undefined),
|
|
265
|
+
});
|
|
266
|
+
}
|
|
260
267
|
describe(description) {
|
|
261
268
|
const This = this.constructor;
|
|
262
269
|
return new This({
|
|
@@ -2459,6 +2466,22 @@ ZodNaN.create = (params) => {
|
|
|
2459
2466
|
...processCreateParams(params),
|
|
2460
2467
|
});
|
|
2461
2468
|
};
|
|
2469
|
+
exports.BRAND = Symbol("zod_brand");
|
|
2470
|
+
class ZodBranded extends ZodType {
|
|
2471
|
+
_parse(input) {
|
|
2472
|
+
const { ctx } = this._processInputParams(input);
|
|
2473
|
+
const data = ctx.data;
|
|
2474
|
+
return this._def.type._parse({
|
|
2475
|
+
data,
|
|
2476
|
+
path: ctx.path,
|
|
2477
|
+
parent: ctx,
|
|
2478
|
+
});
|
|
2479
|
+
}
|
|
2480
|
+
unwrap() {
|
|
2481
|
+
return this._def.type;
|
|
2482
|
+
}
|
|
2483
|
+
}
|
|
2484
|
+
exports.ZodBranded = ZodBranded;
|
|
2462
2485
|
const custom = (check, params = {}, fatal) => {
|
|
2463
2486
|
if (check)
|
|
2464
2487
|
return ZodAny.create().superRefine((data, ctx) => {
|
|
@@ -2507,6 +2530,7 @@ var ZodFirstPartyTypeKind;
|
|
|
2507
2530
|
ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
|
|
2508
2531
|
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
2509
2532
|
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
2533
|
+
ZodFirstPartyTypeKind["ZodBranded"] = "ZodBranded";
|
|
2510
2534
|
})(ZodFirstPartyTypeKind = exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
|
|
2511
2535
|
const instanceOfType = (cls, params = {
|
|
2512
2536
|
message: `Input not instance of ${cls.name}`,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.18.0",
|
|
4
4
|
"description": "TypeScript-first schema declaration and validation library with static type inference",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"test": "jest --coverage",
|
|
60
60
|
"test:deno": "cd deno && deno test",
|
|
61
61
|
"prepublishOnly": "npm run test && npm run build && npm run build:deno",
|
|
62
|
-
"play": "nodemon -e ts -w . -x
|
|
62
|
+
"play": "nodemon -e ts -w . -x tsx src/playground.ts",
|
|
63
63
|
"depcruise": "depcruise -c .dependency-cruiser.js src",
|
|
64
64
|
"benchmark": "esr src/benchmarks/index.ts",
|
|
65
65
|
"prepare": "husky install"
|
|
@@ -73,8 +73,6 @@
|
|
|
73
73
|
"@typescript-eslint/parser": "^5.15.0",
|
|
74
74
|
"benchmark": "^2.1.4",
|
|
75
75
|
"dependency-cruiser": "^9.19.0",
|
|
76
|
-
"esbuild": "^0.14.49",
|
|
77
|
-
"esbuild-runner": "^2.2.1",
|
|
78
76
|
"eslint": "^8.11.0",
|
|
79
77
|
"eslint-config-prettier": "^8.5.0",
|
|
80
78
|
"eslint-plugin-ban": "^1.6.0",
|
|
@@ -91,6 +89,7 @@
|
|
|
91
89
|
"ts-jest": "^27.1.3",
|
|
92
90
|
"ts-morph": "^14.0.0",
|
|
93
91
|
"tslib": "^2.3.1",
|
|
92
|
+
"tsx": "^3.8.0",
|
|
94
93
|
"typescript": "4.1"
|
|
95
94
|
},
|
|
96
95
|
"lint-staged": {
|