Comparing version 0.0.10 to 0.0.11
@@ -118,5 +118,5 @@ declare abstract class Type<T> { | ||
parse(value: unknown, opts?: PathOptions): Eval<Infer<T> & Infer<K>>; | ||
parseObjectIntersection(value: any, opts?: PathOptions): any; | ||
parseRecordIntersection(value: any): any; | ||
parseRecordObjectIntersection(value: any, recordSchema: RecordType<any>, objectSchema: ObjectType<any>): any; | ||
private parseObjectIntersection; | ||
private parseRecordIntersection; | ||
private parseRecordObjectIntersection; | ||
} | ||
@@ -130,2 +130,7 @@ declare type ValueOf<T> = T[keyof T]; | ||
} | ||
declare class PartialType<T extends AnyType> extends Type<Partial<Infer<T>>> { | ||
private readonly schema; | ||
constructor(schema: T); | ||
parse(value: unknown): Partial<Infer<T>>; | ||
} | ||
export declare const string: (opts?: Partial<{ | ||
@@ -156,2 +161,3 @@ pattern: RegExp; | ||
export declare const dictionary: <T extends AnyType>(type: T) => RecordType<UnionType<(UndefinedType | T)[]>>; | ||
export declare const partial: <T extends AnyType>(type: T) => PartialType<T>; | ||
declare const undefinedValue: () => UndefinedType; | ||
@@ -185,6 +191,9 @@ declare const nullValue: () => NullType; | ||
intersection: <T_4 extends AnyType, K extends AnyType>(l: T_4, r: K) => IntersectionType<T_4, K>; | ||
record: <T_5 extends AnyType>(type: T_5) => RecordType<T_5>; | ||
dictionary: <T_6 extends AnyType>(type: T_6) => RecordType<UnionType<(UndefinedType | T_6)[]>>; | ||
partial: <T_7 extends AnyType>(type: T_7) => PartialType<T_7>; | ||
undefined: () => UndefinedType; | ||
null: () => NullType; | ||
enum: <T_5>(e: T_5) => EnumType<T_5>; | ||
enum: <T_8>(e: T_8) => EnumType<T_8>; | ||
}; | ||
export default _default; |
@@ -347,4 +347,3 @@ "use strict"; | ||
} | ||
// TODO If One is record and other is Object than remove object keys before parsing it as record | ||
// TODO if both are Object records we got to allowUnknown. | ||
// TODO Handle Partial Types???? | ||
parse(value, opts) { | ||
@@ -418,2 +417,34 @@ if (this.left instanceof ObjectType && this.right instanceof ObjectType) { | ||
} | ||
function toPartialSchema(schema) { | ||
if (schema instanceof ObjectType) { | ||
const originalShape = schema.objectShape; | ||
const shape = Object.keys(originalShape).reduce((acc, key) => { | ||
acc[key] = originalShape[key].optional(); | ||
return acc; | ||
}, {}); | ||
return new ObjectType(shape, schema.opts); | ||
} | ||
if (schema instanceof RecordType) { | ||
return new RecordType(schema.schema.optional()); | ||
} | ||
if (schema instanceof IntersectionType) { | ||
return new IntersectionType(toPartialSchema(schema.left), toPartialSchema(schema.right)); | ||
} | ||
if (schema instanceof UnionType) { | ||
return new UnionType(schema.schemas.map((x) => x.optional())); | ||
} | ||
if (schema instanceof ArrayType) { | ||
return new ArrayType(schema.schema.optional()); | ||
} | ||
return schema; | ||
} | ||
class PartialType extends Type { | ||
constructor(schema) { | ||
super(); | ||
this.schema = toPartialSchema(schema); | ||
} | ||
parse(value) { | ||
return this.schema.parse(value); | ||
} | ||
} | ||
exports.string = (opts) => new StringType(opts); | ||
@@ -430,2 +461,3 @@ exports.boolean = () => new BooleanType(); | ||
exports.dictionary = (type) => new RecordType(exports.union([type, undefinedValue()])); | ||
exports.partial = (type) => new PartialType(type); | ||
const undefinedValue = () => new UndefinedType(); | ||
@@ -448,2 +480,5 @@ exports.undefined = undefinedValue; | ||
intersection: exports.intersection, | ||
record: exports.record, | ||
dictionary: exports.dictionary, | ||
partial: exports.partial, | ||
undefined: undefinedValue, | ||
@@ -450,0 +485,0 @@ null: nullValue, |
{ | ||
"name": "myzod", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./libs/index.js", |
@@ -49,2 +49,3 @@ # myzod | ||
- [intersection](#intersection) | ||
- [partial](#partial) | ||
@@ -363,1 +364,15 @@ ### myzod.Type<T> | ||
``` | ||
#### Partial | ||
The myzod.partial function take a schema and generates a new schema equivalent to typescript's Partial<T> type for that schema. | ||
```typescript | ||
const personSchema = myzod.object({ name: myzod.string() }); | ||
const partialPersonSchema = myzod.partial(personSchema); | ||
type PartialPerson = Infer<typeof partialPersonSchema>; // => Partial<{ name: string }> || { name?: string } | ||
partialPersonSchema.parse({}); // Succeeds | ||
partialPersonSchema.parse({ nickName: 'lil kenny g' }); // throws validation error | ||
``` |
60531
36
1357
377