Comparing version 1.2.4 to 1.2.5
@@ -28,5 +28,6 @@ import { ZodRawShape } from '../types/base'; | ||
}>; | ||
type FlattenObject<T extends ZodRawShape> = { | ||
type Identity<T> = T; | ||
type FlattenObject<T extends ZodRawShape> = Identity<{ | ||
[k in keyof T]: T[k]; | ||
}; | ||
}>; | ||
type NoNeverKeys<T extends object> = { | ||
@@ -33,0 +34,0 @@ [k in keyof T]: T[k] extends never ? never : k; |
export declare namespace util { | ||
type AssertEqual<T, Expected> = T extends Expected ? (Expected extends T ? true : never) : never; | ||
type AssertEqual<T, Expected> = T extends Expected ? (Expected extends T ? true : false) : false; | ||
function assertNever(_x: never): never; | ||
} |
@@ -5,2 +5,8 @@ "use strict"; | ||
(function (util) { | ||
// export type ObjectsEqual<X extends object, Y extends object> = AssertEqual<X, Y> extends true | ||
// ? 'bad' extends { [k in keyof X & keyof Y]: AssertEqual<X[k], Y[k]> extends true ? 'good' : 'bad' }[keyof X & | ||
// keyof Y] | ||
// ? { [k in keyof X & keyof Y]: AssertEqual<X[k], Y[k]> extends true ? 'good' : 'bad' } | ||
// : true | ||
// : false; | ||
function assertNever(_x) { | ||
@@ -7,0 +13,0 @@ throw new Error(); |
@@ -90,2 +90,3 @@ "use strict"; | ||
if (extraKeys.length) { | ||
// console.log(def); | ||
throw ZodError_1.ZodError.fromString("Unexpected key(s) in object: " + extraKeys.map(function (k) { return "'" + k + "'"; }).join(', ')); | ||
@@ -92,0 +93,0 @@ } |
"use strict"; | ||
var partial = function (params) { | ||
return params || { deep: true }; | ||
}; | ||
var qwer = partial(); | ||
//# sourceMappingURL=playground.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var parser_1 = require("../parser"); | ||
var masker_1 = require("../masker"); | ||
// import { maskUtil } from '../helpers/maskUtil'; | ||
// import { Masker } from '../masker'; | ||
// import { ZodString } from './string'; | ||
@@ -33,2 +34,5 @@ // import { maskUtil } from '../helpers/maskUtil'; | ||
var ZodType = /** @class */ (function () { | ||
// mask = <P extends maskUtil.Params<Type>>(_params: P): ZodType<maskUtil.Pick<Type, P>> => { | ||
// return Masker(this, _params) as any; | ||
// }; | ||
// pick = <Params extends maskUtil.Params<Type>>(_params: Params): maskUtil.Mask<Type, Params> => { | ||
@@ -38,6 +42,2 @@ // return 'asdf' as any; | ||
function ZodType(def) { | ||
var _this = this; | ||
this.mask = function (_params) { | ||
return masker_1.Masker(_this, _params); | ||
}; | ||
this.parse = parser_1.ZodParser(def); | ||
@@ -44,0 +44,0 @@ this._def = def; |
@@ -45,2 +45,16 @@ import * as z from './base'; | ||
omit: <Mask extends { [k in keyof T]?: true | undefined; }>(mask: Mask) => ZodObject<{ [k in keyof T]: k extends keyof Mask ? never : T[k]; }, Params>; | ||
partial: () => ZodObject<{ [k in keyof T]: ZodUnion<[T[k], ZodUndefined]>; }, Params>; | ||
deepPartial: () => ZodObject<{ [k in keyof T]: { | ||
object: T[k] extends ZodObject<infer Shape, infer Params_1> ? ZodUnion<[ZodObject<{ [k in keyof Shape]: { | ||
object: Shape[k] extends ZodObject<infer Shape, infer Params_1> ? ZodUnion<[ZodObject<any, Params_1>, ZodUndefined]> : never; | ||
rest: ZodUnion<[Shape[k], ZodUndefined]>; | ||
}[Shape[k] extends ZodObject<any, { | ||
strict: true; | ||
}> ? "object" : "rest"]; }, Params_1>, ZodUndefined]> : never; | ||
rest: ZodUnion<[T[k], ZodUndefined]>; | ||
}[T[k] extends ZodObject<any, { | ||
strict: true; | ||
}> ? "object" : "rest"]; }, { | ||
strict: true; | ||
}>; | ||
static create: <T_1 extends z.ZodRawShape>(shape: T_1) => ZodObject<T_1, { | ||
@@ -47,0 +61,0 @@ strict: true; |
@@ -111,2 +111,22 @@ "use strict"; | ||
}; | ||
_this.partial = function () { | ||
var newShape = {}; | ||
for (var key in _this.shape) { | ||
newShape[key] = _this.shape[key].optional(); | ||
} | ||
return new ZodObject(__assign({}, _this._def, { shape: newShape })); | ||
}; | ||
_this.deepPartial = function () { | ||
var newShape = {}; | ||
for (var key in _this.shape) { | ||
var fieldSchema = _this.shape[key]; | ||
if (fieldSchema instanceof ZodObject) { | ||
newShape[key] = fieldSchema.deepPartial().optional(); | ||
} | ||
else { | ||
newShape[key] = _this.shape[key].optional(); | ||
} | ||
} | ||
return new ZodObject(__assign({}, _this._def, { shape: newShape })); | ||
}; | ||
return _this; | ||
@@ -128,12 +148,2 @@ } | ||
}); | ||
// partial = <P extends {deep:boolean} | undefined>(params:P):ZodObject<{[k in keyof T]?: T[k]}, Params> => { | ||
// const newShape:any = {}; | ||
// for (const key in this.shape){ | ||
// newShape[key] = this.shape[key].optional(); | ||
// } | ||
// return new ZodObject({ | ||
// ...this._def, | ||
// shape | ||
// }) | ||
// } | ||
// pick = <Mask extends zodmaskUtil.Params<ZodObject<T>>>( | ||
@@ -140,0 +150,0 @@ // mask: Mask, |
{ | ||
"name": "zod", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "TypeScript-first schema declaration and validation library with static type inference", | ||
@@ -5,0 +5,0 @@ "main": "./lib/src/index.js", |
176
README.md
@@ -35,2 +35,3 @@ <p align="center"> | ||
- [.pick/.omit](#masking) | ||
- [.partial/.deepPartial](#partials) | ||
- [Records](#records) | ||
@@ -47,3 +48,3 @@ - [Arrays](#arrays) | ||
- [Function schemas](#function-schemas) | ||
- [Masking](#masking) | ||
<!-- - [Masking](#masking) --> | ||
- [Errors](#errors) | ||
@@ -193,51 +194,14 @@ - [Changelog](#changelog) | ||
#### Unknown keys | ||
#### `.shape` property | ||
IMPORTANT: By default, Zod object schemas _do not_ allow unknown keys. | ||
```ts | ||
dogSchema.parse({ | ||
name: 'Spot', | ||
neutered: true, | ||
color: 'brown', | ||
}); // Error(`Unexpected keys in object: 'color'`) | ||
``` | ||
const Location = z.shape({ latitude: z.number(), longitude: z.number() }); | ||
This is an intentional decision to make Zod's behavior consistent with TypeScript. Consider this: | ||
const Business = z.shape({ | ||
location: Location, | ||
}); | ||
```ts | ||
const spot: Dog = { | ||
name: 'Spot', | ||
neutered: true, | ||
color: 'brown', | ||
}; | ||
// TypeError: Object literal may only specify known | ||
// properties, and 'color' does not exist in type Dog | ||
Business.shape.location; // => Location schema | ||
``` | ||
TypeScript doesn't allow unknown keys when assigning to an object type, so neither does Zod (by default). If you want to allow this, just call the `.nonstrict()` method on any object schema: | ||
```ts | ||
const dogSchemaNonstrict = dogSchema.nonstrict(); | ||
dogSchemaNonstrict.parse({ | ||
name: 'Spot', | ||
neutered: true, | ||
color: 'brown', | ||
}); // passes | ||
``` | ||
This change is reflected in the inferred type as well: | ||
```ts | ||
type NonstrictDog = z.infer<typeof dogSchemaNonstrict>; | ||
/* | ||
{ | ||
name:string; | ||
neutered: boolean; | ||
[k:string]: any; | ||
} | ||
*/ | ||
``` | ||
#### Merging | ||
@@ -295,3 +259,3 @@ | ||
If the argument to `.augment` contains a key that already exists in the base schema, the type gets overwritten: | ||
⚠️ You can use `.augment` to overwrite fields! Be careful with this power! | ||
@@ -341,2 +305,109 @@ ```ts | ||
#### Partials | ||
Inspired by the built-in TypeScript utility type [Partial](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialt), all Zod object schemas have a `.partial` method that makes all properties optional. | ||
Starting from this object: | ||
```ts | ||
const user = z.object({ | ||
username: z.string(), | ||
location: z.object({ | ||
latitude: z.number(), | ||
longitude: z.number(), | ||
}), | ||
}); | ||
/* | ||
{ username: string, location: { city: number, state: number } } | ||
*/ | ||
``` | ||
We can create a partial version: | ||
```ts | ||
const partialUser = user.partial(); // => { username?: string | undefined } | ||
/* | ||
{ | ||
username?: string | undefined, | ||
location?: { | ||
city: number; | ||
state: number; | ||
} | undefined | ||
} | ||
*/ | ||
// equivalent to: | ||
const partialUser = z.object({ | ||
username: user.shape.username.optional(), | ||
location: user.shape.location.optional(), | ||
}); | ||
``` | ||
Or you can use `.deepPartial`: | ||
```ts | ||
const deepPartialUser = user.deepPartial(); | ||
/* | ||
{ | ||
username?: string | undefined, | ||
location?: { | ||
latitude?: number | undefined; | ||
longitude?: number | undefined; | ||
} | undefined | ||
} | ||
*/ | ||
``` | ||
Important limitation: deep partials only work as expected in hierarchies of object schemas. It also can't be used on recursive schemas currently, since creating a recursive schema requires casting to the generic `ZodType` type (which doesn't include all the methods of the `ZodObject` class). Currently an improved version of Zod is under development that will have better support for recursive schemas. | ||
#### Unknown keys | ||
IMPORTANT: By default, Zod object schemas _do not_ allow unknown keys. | ||
```ts | ||
dogSchema.parse({ | ||
name: 'Spot', | ||
neutered: true, | ||
color: 'brown', | ||
}); // Error(`Unexpected keys in object: 'color'`) | ||
``` | ||
This is an intentional decision to make Zod's behavior consistent with TypeScript. Consider this: | ||
```ts | ||
const spot: Dog = { | ||
name: 'Spot', | ||
neutered: true, | ||
color: 'brown', | ||
}; | ||
// TypeError: Object literal may only specify known | ||
// properties, and 'color' does not exist in type Dog | ||
``` | ||
TypeScript doesn't allow unknown keys when assigning to an object type, so neither does Zod (by default). If you want to allow this, just call the `.nonstrict()` method on any object schema: | ||
```ts | ||
const dogSchemaNonstrict = dogSchema.nonstrict(); | ||
dogSchemaNonstrict.parse({ | ||
name: 'Spot', | ||
neutered: true, | ||
color: 'brown', | ||
}); // passes | ||
``` | ||
This change is reflected in the inferred type as well: | ||
```ts | ||
type NonstrictDog = z.infer<typeof dogSchemaNonstrict>; | ||
/* | ||
{ | ||
name:string; | ||
neutered: boolean; | ||
[k:string]: any; | ||
} | ||
*/ | ||
``` | ||
## Records | ||
@@ -1007,9 +1078,10 @@ | ||
| ----------- | ---------------------------------------------------------- | | ||
| zod@1.2.5 | `.partial` and `.deepPartial` on object schemas | | ||
| zod@1.2.3 | Date schemas | | ||
| zod@1.2.0 | `.pick`, `.omit`, and `.augment` 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@1.0.0 | Type assertions, literals, enums, detailed error reporting | | ||
| zod@1.0.4 | Empty tuples | | ||
| zod@1.0.10 | Type assertions with `.check` | | ||
| zod@1.0.11 | `.nonstrict` | | ||
| zod@1.1.0 | Records | | ||
| zod@1.2.0 | `.pick`, `.omit`, and `.augment` on object schemas | | ||
| zod@1.2.3 | Date schemas | |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115
1082
197246
2818