Comparing version 0.0.25 to 0.0.26
@@ -15,3 +15,3 @@ declare abstract class Type<T> { | ||
declare type AnyType = Type<any>; | ||
declare type Eval<T> = T extends any[] ? T : { | ||
declare type Eval<T> = T extends any[] | Date ? T : { | ||
[Key in keyof T]: T[Key]; | ||
@@ -65,2 +65,6 @@ } & {}; | ||
} | ||
declare class DateType extends Type<Date> { | ||
constructor(); | ||
parse(value: unknown): Date; | ||
} | ||
declare type ObjectShape = Record<string, AnyType>; | ||
@@ -213,2 +217,3 @@ declare type OptionalKeys<T extends ObjectShape> = { | ||
export declare const tuple: <T extends [] | [AnyType, ...AnyType[]]>(schemas: T) => TupleType<T>; | ||
export declare const date: () => DateType; | ||
declare const undefinedValue: () => UndefinedType; | ||
@@ -233,2 +238,3 @@ declare const nullValue: () => NullType; | ||
literal: <T extends Literal>(literal: T) => LiteralType<T>; | ||
date: () => DateType; | ||
object: <T_1 extends Record<string, AnyType>>(shape: T_1, opts?: ObjectOptions | undefined) => ObjectType<T_1>; | ||
@@ -235,0 +241,0 @@ array: <T_2 extends AnyType>(type: T_2, opts?: Partial<{ |
@@ -52,2 +52,3 @@ "use strict"; | ||
const shapekeysSymbol = Symbol.for('shapeKeys'); | ||
const convTypeSymbol = Symbol.for('convtype'); | ||
class StringType extends Type { | ||
@@ -174,2 +175,25 @@ constructor(opts = {}) { | ||
} | ||
// Non Primitive types | ||
class DateType extends Type { | ||
constructor() { | ||
super(); | ||
this[convTypeSymbol] = true; | ||
} | ||
parse(value) { | ||
if (typeof value === 'string') { | ||
if (value.length === 0) { | ||
throw new ValidationError('expected date string but got empty string'); | ||
} | ||
const date = new Date(value); | ||
if (isNaN(date.getTime())) { | ||
throw new ValidationError(`expected date string to be valid date`); | ||
} | ||
return date; | ||
} | ||
if (!(value instanceof Date)) { | ||
throw new ValidationError('expected type Date but got ' + typeOf(value)); | ||
} | ||
return value; | ||
} | ||
} | ||
class ObjectType extends Type { | ||
@@ -190,2 +214,3 @@ constructor(objectShape, opts) { | ||
}); | ||
this[convTypeSymbol] = Object.values(this.objectShape).some(schema => schema[convTypeSymbol]); | ||
} | ||
@@ -216,2 +241,4 @@ parse(value, parseOpts = {}) { | ||
} | ||
//@ts-ignore | ||
const convVal = this[convTypeSymbol] ? (allowUnknown ? Object.assign({}, value) : {}) : undefined; | ||
for (const key of keys) { | ||
@@ -223,7 +250,17 @@ try { | ||
} | ||
if (schema instanceof ObjectType || schema instanceof ArrayType || schema instanceof RecordType) { | ||
schema.parse(value[key], { suppressPathErrMsg: true }); | ||
if (convVal) { | ||
if (schema instanceof ObjectType || schema instanceof ArrayType || schema instanceof RecordType) { | ||
convVal[key] = schema.parse(value[key], { suppressPathErrMsg: true }); | ||
} | ||
else { | ||
convVal[key] = schema.parse(value[key]); | ||
} | ||
} | ||
else { | ||
schema.parse(value[key]); | ||
if (schema instanceof ObjectType || schema instanceof ArrayType || schema instanceof RecordType) { | ||
schema.parse(value[key], { suppressPathErrMsg: true }); | ||
} | ||
else { | ||
schema.parse(value[key]); | ||
} | ||
} | ||
@@ -239,3 +276,3 @@ } | ||
} | ||
return value; | ||
return convVal || value; | ||
} | ||
@@ -262,2 +299,3 @@ pick(keys, opts) { | ||
this.schema = schema; | ||
this[convTypeSymbol] = schema[convTypeSymbol]; | ||
this._parse = (() => { | ||
@@ -282,5 +320,11 @@ if (this.schema instanceof ObjectType) { | ||
} | ||
const convValue = this[convTypeSymbol] ? {} : undefined; | ||
for (const key in value) { | ||
try { | ||
this._parse(value[key], opts); | ||
if (convValue) { | ||
convValue[key] = this._parse(value[key], opts); | ||
} | ||
else { | ||
this._parse(value[key], opts); | ||
} | ||
} | ||
@@ -294,3 +338,3 @@ catch (err) { | ||
} | ||
return value; | ||
return convValue || value; | ||
} | ||
@@ -303,2 +347,3 @@ } | ||
this.opts = opts; | ||
this[convTypeSymbol] = this.schema[convTypeSymbol]; | ||
this._parse = | ||
@@ -334,5 +379,11 @@ this.schema instanceof ObjectType || this.schema instanceof ArrayType | ||
} | ||
const convValue = this[convTypeSymbol] ? [] : undefined; | ||
for (let i = 0; i < value.length; i++) { | ||
try { | ||
this._parse(value[i]); | ||
if (convValue) { | ||
convValue[i] = this._parse(value[i]); | ||
} | ||
else { | ||
this._parse(value[i]); | ||
} | ||
} | ||
@@ -346,3 +397,3 @@ catch (err) { | ||
} | ||
return value; | ||
return convValue || value; | ||
} | ||
@@ -370,2 +421,3 @@ length(value) { | ||
this.schemas = schemas; | ||
this[convTypeSymbol] = schemas.some(schema => schema[convTypeSymbol]); | ||
} | ||
@@ -379,5 +431,11 @@ parse(value) { | ||
} | ||
const convValue = this[convTypeSymbol] ? [] : undefined; | ||
for (let i = 0; i < this.schemas.length; i++) { | ||
try { | ||
this.schemas[i].parse(value[i]); | ||
if (convValue) { | ||
convValue.push(this.schemas[i].parse(value[i])); | ||
} | ||
else { | ||
this.schemas[i].parse(value[i]); | ||
} | ||
} | ||
@@ -388,3 +446,3 @@ catch (err) { | ||
} | ||
return value; | ||
return convValue || value; | ||
} | ||
@@ -421,2 +479,3 @@ } | ||
this.right = right; | ||
this[convTypeSymbol] = this.left[convTypeSymbol] || this.right[convTypeSymbol]; | ||
this[allowUnknownSymbol] = !!(this.left[allowUnknownSymbol] || this.right[allowUnknownSymbol]); | ||
@@ -465,2 +524,5 @@ if (this.left[requiredKeysSymbol] && this.right[requiredKeysSymbol]) { | ||
return (value) => { | ||
if (this[convTypeSymbol]) { | ||
return Object.assign(Object.assign({}, this.left.parse(value, { allowUnknown: true })), this.right.parse(value, { allowUnknown: true })); | ||
} | ||
//@ts-ignore | ||
@@ -493,2 +555,5 @@ this.left.parse(value, { allowUnknown: true }); | ||
const parsingOptions = { suppressPathErrMsg: opts === null || opts === void 0 ? void 0 : opts.suppressPathErrMsg, allowUnknown: true }; | ||
if (this[convTypeSymbol]) { | ||
return Object.assign(Object.assign({}, this.left.parse(value, parsingOptions)), this.right.parse(value, parsingOptions)); | ||
} | ||
this.left.parse(value, parsingOptions); | ||
@@ -499,2 +564,13 @@ this.right.parse(value, parsingOptions); | ||
parseRecordObjectIntersection(value, recordSchema, objectSchema) { | ||
if (this[convTypeSymbol]) { | ||
const convObj = objectSchema.parse(value, { allowUnknown: true }); | ||
const proxy = Object.keys(value).reduce((acc, key) => { | ||
if (!objectKeys.includes(key)) { | ||
acc[key] = value[key]; | ||
} | ||
return acc; | ||
}, {}); | ||
const convRecord = recordSchema.parse(proxy); | ||
return Object.assign(Object.assign({}, convObj), convRecord); | ||
} | ||
objectSchema.parse(value, { allowUnknown: true }); | ||
@@ -565,2 +641,3 @@ const objectKeys = objectSchema[shapekeysSymbol]; | ||
this.schema = toPartialSchema(schema, opts); | ||
this[convTypeSymbol] = this.schema[convTypeSymbol]; | ||
} | ||
@@ -622,2 +699,3 @@ parse(value) { | ||
this[shapekeysSymbol] = rootShapeKeys && rootShapeKeys.filter((key) => pickedKeys.includes(key)); | ||
this[convTypeSymbol] = this.schema[convTypeSymbol]; | ||
} | ||
@@ -685,2 +763,3 @@ parse(value, parseOptions) { | ||
this[shapekeysSymbol] = rootShapeKeys && rootShapeKeys.filter((key) => !omittedKeys.includes(key)); | ||
this[convTypeSymbol] = this.schema[convTypeSymbol]; | ||
} | ||
@@ -716,2 +795,3 @@ parse(value, opts) { | ||
exports.tuple = (schemas) => new TupleType(schemas); | ||
exports.date = () => new DateType(); | ||
const undefinedValue = () => new UndefinedType(); | ||
@@ -730,2 +810,3 @@ exports.undefined = undefinedValue; | ||
literal: exports.literal, | ||
date: exports.date, | ||
object: exports.object, | ||
@@ -732,0 +813,0 @@ array: exports.array, |
{ | ||
"name": "myzod", | ||
"version": "0.0.25", | ||
"version": "0.0.26", | ||
"description": "", | ||
@@ -11,3 +11,3 @@ "main": "./libs/index.js", | ||
"pub": "npm t && npm run build && npm publish", | ||
"bench": "ts-node test/benchmark.ts > test/results.json" | ||
"bench": "ts-node test/benchmark.ts | tee ./test/results.json" | ||
}, | ||
@@ -14,0 +14,0 @@ "keywords": [ |
@@ -81,2 +81,3 @@ # myzod | ||
- [enum](#enum) | ||
- [date](#date) | ||
@@ -420,2 +421,16 @@ Logical Types | ||
#### Date | ||
the myzod.date function creates a date schema. Values that will be successfully parsed by this schema are | ||
Javascript Date instances and valid string representations of dates. The returned parse Date will be an instance of Date. | ||
```typescript | ||
const schema = myzod.date(); | ||
type Schema = myzod.Infer<typeof schema>; // => Date | ||
const date = new Date(); | ||
schema.parse(date); // returns date | ||
schema.parse(date.toISOString()); // returns a date instance equal to date | ||
``` | ||
#### Union | ||
@@ -422,0 +437,0 @@ |
59265
1080
533