Comparing version 0.0.23 to 0.0.24
@@ -91,3 +91,5 @@ declare abstract class Type<T> { | ||
omit<K extends keyof T>(keys: K[], opts?: ObjectOptions): ObjectType<Eval<Omit<T, ToUnion<typeof keys>>>>; | ||
partial(opts?: ObjectOptions): ObjectType<PartialShape<T>>; | ||
partial<K extends ObjectOptions & PartialOpts>(opts?: K): ObjectType<Eval<K extends { | ||
deep: true; | ||
} ? DeepPartial<PartialShape<T>> : PartialShape<T>>>; | ||
} | ||
@@ -151,6 +153,16 @@ declare class RecordType<T extends AnyType> extends Type<Record<string, Infer<T>>> { | ||
} | ||
declare class PartialType<T extends AnyType> extends Type<Partial<Infer<T>>> { | ||
declare type DeepPartial<T> = { | ||
[key in keyof T]?: T[key] extends Object ? DeepPartial<T[key]> : T[key]; | ||
}; | ||
declare type PartialOpts = { | ||
deep: boolean; | ||
}; | ||
declare class PartialType<T extends AnyType, K extends PartialOpts> extends Type<K extends { | ||
deep: true; | ||
} ? Eval<DeepPartial<Infer<T>>> : Partial<Infer<T>>> { | ||
private readonly schema; | ||
constructor(schema: T); | ||
parse(value: unknown): Partial<Infer<T>>; | ||
constructor(schema: T, opts?: K); | ||
parse(value: unknown): K extends { | ||
deep: true; | ||
} ? Eval<DeepPartial<Infer<T>>> : Partial<Infer<T>>; | ||
} | ||
@@ -194,3 +206,3 @@ declare class PickType<T extends AnyType, K extends keyof Infer<T>> extends Type<Pick<Infer<T>, K>> { | ||
export declare const dictionary: <T extends AnyType>(type: T) => RecordType<UnionType<(UndefinedType | T)[]>>; | ||
export declare const partial: <T extends AnyType>(type: T) => PartialType<T>; | ||
export declare const partial: <T extends AnyType, K extends PartialOpts>(type: T, opts?: K | undefined) => PartialType<T, K>; | ||
export declare const pick: <T extends AnyType, K extends keyof Infer<T>>(type: T, keys: K[]) => PickType<T, K>; | ||
@@ -230,5 +242,5 @@ export declare const omit: <T extends AnyType, K extends keyof Infer<T>>(type: T, keys: K[]) => OmitType<T, K>; | ||
tuple: <T_7 extends [] | [AnyType, ...AnyType[]]>(schemas: T_7) => TupleType<T_7>; | ||
partial: <T_8 extends AnyType>(type: T_8) => PartialType<T_8>; | ||
pick: <T_9 extends AnyType, K_1 extends keyof Infer<T_9>>(type: T_9, keys: K_1[]) => PickType<T_9, K_1>; | ||
omit: <T_10 extends AnyType, K_2 extends keyof Infer<T_10>>(type: T_10, keys: K_2[]) => OmitType<T_10, K_2>; | ||
partial: <T_8 extends AnyType, K_1 extends PartialOpts>(type: T_8, opts?: K_1 | undefined) => PartialType<T_8, K_1>; | ||
pick: <T_9 extends AnyType, K_2 extends keyof Infer<T_9>>(type: T_9, keys: K_2[]) => PickType<T_9, K_2>; | ||
omit: <T_10 extends AnyType, K_3 extends keyof Infer<T_10>>(type: T_10, keys: K_3[]) => OmitType<T_10, K_3>; | ||
undefined: () => UndefinedType; | ||
@@ -235,0 +247,0 @@ null: () => NullType; |
@@ -99,3 +99,5 @@ "use strict"; | ||
this.opts.predicate = fn; | ||
this.opts.predicateErrMsg = errMsg; | ||
if (errMsg) { | ||
this.opts.predicateErrMsg = errMsg; | ||
} | ||
return this; | ||
@@ -247,7 +249,4 @@ } | ||
partial(opts) { | ||
const partialShape = this[shapekeysSymbol].reduce((acc, key) => { | ||
acc[key] = this.objectShape[key].optional(); | ||
return acc; | ||
}, {}); | ||
return new ObjectType(partialShape, opts); | ||
const schema = toPartialSchema(this, { deep: (opts === null || opts === void 0 ? void 0 : opts.deep) || false }).objectShape; | ||
return new ObjectType(schema, { allowUnknown: opts === null || opts === void 0 ? void 0 : opts.allowUnknown }); | ||
} | ||
@@ -511,7 +510,12 @@ } | ||
} | ||
function toPartialSchema(schema) { | ||
function toPartialSchema(schema, opts) { | ||
if (schema instanceof ObjectType) { | ||
const originalShape = schema.objectShape; | ||
const shape = Object.keys(originalShape).reduce((acc, key) => { | ||
acc[key] = originalShape[key].optional(); | ||
if (opts === null || opts === void 0 ? void 0 : opts.deep) { | ||
acc[key] = toPartialSchema(originalShape[key], opts).optional(); | ||
} | ||
else { | ||
acc[key] = originalShape[key].optional(); | ||
} | ||
return acc; | ||
@@ -522,11 +526,17 @@ }, {}); | ||
if (schema instanceof RecordType) { | ||
if (opts === null || opts === void 0 ? void 0 : opts.deep) { | ||
return new RecordType(toPartialSchema(schema.schema, opts).optional()); | ||
} | ||
return new RecordType(schema.schema.optional()); | ||
} | ||
if (schema instanceof IntersectionType) { | ||
return new IntersectionType(toPartialSchema(schema.left), toPartialSchema(schema.right)); | ||
return new IntersectionType(toPartialSchema(schema.left, opts), toPartialSchema(schema.right, opts)); | ||
} | ||
if (schema instanceof UnionType) { | ||
return new UnionType(schema.schemas.map(toPartialSchema)); | ||
return new UnionType(schema.schemas.map((schema) => toPartialSchema(schema, opts))); | ||
} | ||
if (schema instanceof ArrayType) { | ||
if (opts === null || opts === void 0 ? void 0 : opts.deep) { | ||
return new ArrayType(toPartialSchema(schema.schema, opts).optional()); | ||
} | ||
return new ArrayType(schema.schema.optional()); | ||
@@ -537,5 +547,5 @@ } | ||
class PartialType extends Type { | ||
constructor(schema) { | ||
constructor(schema, opts) { | ||
super(); | ||
this.schema = toPartialSchema(schema); | ||
this.schema = toPartialSchema(schema, opts); | ||
} | ||
@@ -685,3 +695,3 @@ parse(value) { | ||
exports.dictionary = (type) => new RecordType(exports.union([type, undefinedValue()])); | ||
exports.partial = (type) => new PartialType(type); | ||
exports.partial = (type, opts) => new PartialType(type, opts); | ||
exports.pick = (type, keys) => new PickType(type, keys); | ||
@@ -688,0 +698,0 @@ exports.omit = (type, keys) => new OmitType(type, keys); |
{ | ||
"name": "myzod", | ||
"version": "0.0.23", | ||
"version": "0.0.24", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./libs/index.js", |
@@ -288,2 +288,19 @@ # myzod | ||
Partial accepts an options object to allow for deeply nested partials: | ||
```typescript | ||
const schema = myzod | ||
.object({ | ||
name: myzod.string(), | ||
birthday: myzod.object({ | ||
year: myzod.number(), | ||
month: myzod.number().min(1).max(12), | ||
date: myzod.number().min(1).max(31), | ||
}), | ||
}) | ||
.partial({ deep: true }); | ||
type DeeplyPartialSchema = myzod.Infer<typeof schema>; // { name?: string; birthday?: { year?: number; month?: number; date?: number; } } | ||
``` | ||
#### Array | ||
@@ -444,2 +461,27 @@ | ||
The partial function accepts an options object as second argument to create a deeply partial object. | ||
options: | ||
- deep: `boolean` created a deeply partial schema for nested objects | ||
```typescript | ||
const schema = myzod.object({ | ||
name: myzod.string(), | ||
birthday: myzod.object({ | ||
year: myzod.number(), | ||
month: myzod.number().min(1).max(12), | ||
date: myzod.number().min(1).max(31), | ||
}), | ||
}); | ||
const partialSchema = myzod.partial(schema); | ||
type PartialSchema = myzod.Infer<typeof partialSchema>; // => { name?: string; birthday?: { year: number; month: number; date: number; } } | ||
const deeplyPartialSchema = myzod.partial(schema, { deep: true }); | ||
type DeeplyPartialSchema = myzod.Infer<typeof deeplyPartialSchema>; // { name?: string; birthday?: { year?: number; month?: number; date?: number; } } | ||
``` | ||
#### Pick | ||
@@ -446,0 +488,0 @@ |
54774
990
518