diff --git a/README.md b/README.md index 98689cd8d..f5f2ed756 100644 --- a/README.md +++ b/README.md @@ -1183,7 +1183,7 @@ User.omit({ outer: { inner: { prop2: true } } }); // { outer: { prop1: string, i ## Errors -There is a dedicated guide on Zod's error handling system here: [ERROR_HANDLING.md](https://github.com/vriad/zod/blob/beta/ERROR_HANDLING.md) +There is a dedicated guide on Zod's error handling system here: [ERROR_HANDLING.md](https://github.com/vriad/zod/blob/master/ERROR_HANDLING.md) # Comparison @@ -1346,21 +1346,21 @@ If you want to validate function inputs, use function schemas in Zod! It's a muc # Changelog -| zod version | release notes | -| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| zod@1.9 | Added z.instanceof() and z.custom(). Implemented ZodSchema.array() method. | -| zod@1.8 | Introduced z.void(). Major overhaul to error handling system, including the introduction of custom error maps. Wrote new [error handling guide](https://github.com/vriad/zod/blob/beta/ERROR_HANDLING.md). | -| zod@1.7 | Added several built-in validators to string, number, and array schemas. Calls to `.refine` now return new instance. | -| zod@1.5 | Any and unknown types | -| zod@1.4 | Refinement types (`.refine`), `.parse` no longer returns deep clone | -| zod@1.3 | Promise schemas | -| zod@1.2.6 | `.parse` accepts `unknown`, `bigint` schemas | -| zod@1.2.5 | `.partial` and `.deepPartial` on object schemas | -| zod@1.2.3 | Date schemas | -| zod@1.2.0 | `.pick`, `.omit`, and `.extend` on object schemas | -| zod@1.1.0 | Records | -| zod@1.0.11 | `.nonstrict` | -| zod@1.0.10 | Type assertions with `.check` | -| zod@1.0.4 | Empty tuples | -| zod@1.0.0 | Type assertions, literals, enums, detailed error reporting | -| zod@1.0.0 | Initial release | +| zod version | release notes | +| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| zod@1.9 | Added z.instanceof() and z.custom(). Implemented ZodSchema.array() method. | +| zod@1.8 | Introduced z.void(). Major overhaul to error handling system, including the introduction of custom error maps. Wrote new [error handling guide](https://github.com/vriad/zod/blob/master/ERROR_HANDLING.md). | +| zod@1.7 | Added several built-in validators to string, number, and array schemas. Calls to `.refine` now return new instance. | +| zod@1.5 | Any and unknown types | +| zod@1.4 | Refinement types (`.refine`), `.parse` no longer returns deep clone | +| zod@1.3 | Promise schemas | +| zod@1.2.6 | `.parse` accepts `unknown`, `bigint` schemas | +| zod@1.2.5 | `.partial` and `.deepPartial` on object schemas | +| zod@1.2.3 | Date schemas | +| zod@1.2.0 | `.pick`, `.omit`, and `.extend` on object schemas | +| zod@1.1.0 | Records | +| zod@1.0.11 | `.nonstrict` | +| zod@1.0.10 | Type assertions with `.check` | +| zod@1.0.4 | Empty tuples | +| zod@1.0.0 | Type assertions, literals, enums, detailed error reporting | +| zod@1.0.0 | Initial release | diff --git a/src/playground.ts b/src/playground.ts index d12ed5612..629d3a025 100644 --- a/src/playground.ts +++ b/src/playground.ts @@ -1,2 +1,21 @@ // import * as z from '.'; -// z.date().parse(new Date('invalid')); +// // z.date().parse(new Date('invalid')); + +// // const myFunc = z.function(z.tuple([z.string()]), z.boolean()).implement(str => str.length > 5); + +// // try { +// // myFunc(12 as any); +// // } catch (err) { +// // console.log(JSON.stringify(err, null, 2)); +// // } + +// const $Cat = z.object({ +// type: z.literal('cat'), +// ability: z.literal('meow'), +// }); +// const $Dog = z.object({ +// type: z.literal('dog'), +// ability: z.literal('bark'), +// }); +// const $AnimalAbility = z.union([$Cat, $Dog]).distribute($A => $A.shape.ability); +// type Ability = z.infer; // "meow" | "bark" diff --git a/src/types/union.ts b/src/types/union.ts index 7b6ef08c8..587485767 100644 --- a/src/types/union.ts +++ b/src/types/union.ts @@ -22,6 +22,10 @@ export class ZodUnion options: this._def.options.map(x => x.toJSON()), }); + distribute = z.ZodTypeAny>(f: F): ZodUnion<{ [k in keyof T]: ReturnType }> => { + return ZodUnion.create(this._def.options.map(f) as any); + }; + static create = (types: T): ZodUnion => { return new ZodUnion({ t: z.ZodTypes.union,