Comparing version 0.0.3 to 0.0.4
@@ -62,5 +62,9 @@ declare abstract class Type<T> { | ||
}>; | ||
declare type UnionOptions = { | ||
strict?: boolean; | ||
}; | ||
declare class UnionType<T extends TupleType> extends Type<InferTupleUnion<T>> { | ||
private readonly schemas; | ||
constructor(schemas: T); | ||
private readonly opts?; | ||
constructor(schemas: T, opts?: UnionOptions | undefined); | ||
parse(value: unknown): InferTupleUnion<T>; | ||
@@ -82,3 +86,3 @@ } | ||
export declare const array: <T extends Type<any>>(type: T) => ArrayType<T>; | ||
export declare const union: <T extends TupleType>(schemas: T) => UnionType<T>; | ||
export declare const union: <T extends TupleType>(schemas: T, opts?: UnionOptions | undefined) => UnionType<T>; | ||
export declare const intersection: <T extends TupleType>(schemas: T) => IntersectionType<T>; | ||
@@ -96,3 +100,3 @@ declare const undefinedValue: () => UndefinedType; | ||
array: <T_2 extends Type<any>>(type: T_2) => ArrayType<T_2>; | ||
union: <T_3 extends TupleType>(schemas: T_3) => UnionType<T_3>; | ||
union: <T_3 extends TupleType>(schemas: T_3, opts?: UnionOptions | undefined) => UnionType<T_3>; | ||
intersection: <T_4 extends TupleType>(schemas: T_4) => IntersectionType<T_4>; | ||
@@ -99,0 +103,0 @@ undefined: () => UndefinedType; |
@@ -20,6 +20,12 @@ "use strict"; | ||
exports.ValidationError = ValidationError; | ||
function typeOf(value) { | ||
if (value === null) { | ||
return 'null'; | ||
} | ||
if (Array.isArray(value)) { | ||
return 'array'; | ||
} | ||
return typeof value; | ||
} | ||
function prettyPrintPath(path) { | ||
if (!path || path.length === 0) { | ||
return ''; | ||
} | ||
return path.reduce((acc, elem, idx) => { | ||
@@ -42,3 +48,3 @@ if (typeof elem === 'number') { | ||
if (typeof value !== 'string') { | ||
throw new ValidationError('expected type to be string but got ' + typeof value); | ||
throw new ValidationError('expected type to be string but got ' + typeOf(value)); | ||
} | ||
@@ -51,3 +57,3 @@ return value; | ||
if (typeof value !== 'boolean') { | ||
throw new ValidationError('expected type to be boolean but got ' + typeof value); | ||
throw new ValidationError('expected type to be boolean but got ' + typeOf(value)); | ||
} | ||
@@ -60,3 +66,3 @@ return value; | ||
if (typeof value !== 'number') { | ||
throw new ValidationError('expected type to be number but got ' + typeof value); | ||
throw new ValidationError('expected type to be number but got ' + typeOf(value)); | ||
} | ||
@@ -69,3 +75,3 @@ return value; | ||
if (value !== undefined) { | ||
throw new ValidationError('expected type to be undefined but got ' + typeof value); | ||
throw new ValidationError('expected type to be undefined but got ' + typeOf(value)); | ||
} | ||
@@ -78,3 +84,3 @@ return value; | ||
if (value !== null) { | ||
throw new ValidationError('expected type to be null but got ' + typeof value); | ||
throw new ValidationError('expected type to be null but got ' + typeOf(value)); | ||
} | ||
@@ -91,3 +97,4 @@ return value; | ||
if (value !== this.literal) { | ||
throw new ValidationError(`expected value to be literal ${JSON.stringify(this.literal)} but got ${JSON.stringify(value)}`); | ||
const typeofValue = typeof value !== 'object' ? JSON.stringify(value) : typeOf(value); | ||
throw new ValidationError(`expected value to be literal ${JSON.stringify(this.literal)} but got ${typeofValue}`); | ||
} | ||
@@ -110,3 +117,3 @@ return value; | ||
if (typeof value !== 'object') { | ||
throw new ValidationError('expected type to be object but got ' + typeof value); | ||
throw new ValidationError('expected type to be object but got ' + typeOf(value)); | ||
} | ||
@@ -162,3 +169,3 @@ if (value === null) { | ||
if (!Array.isArray(value)) { | ||
throw new ValidationError('expected an array but got ' + typeof value); | ||
throw new ValidationError('expected an array but got ' + typeOf(value)); | ||
} | ||
@@ -184,12 +191,16 @@ value.forEach((elem, idx) => { | ||
class UnionType extends Type { | ||
constructor(schemas) { | ||
constructor(schemas, opts) { | ||
super(); | ||
this.schemas = schemas; | ||
this.opts = opts; | ||
} | ||
parse(value) { | ||
var _a; | ||
const errors = []; | ||
for (const schema of this.schemas) { | ||
try { | ||
schema.parse(value); | ||
return value; | ||
if (((_a = this.opts) === null || _a === void 0 ? void 0 : _a.strict) === false && schema instanceof ObjectType) { | ||
return schema.parse(value, { allowUnknown: true }); | ||
} | ||
return schema.parse(value); | ||
} | ||
@@ -228,3 +239,3 @@ catch (err) { | ||
exports.array = (type) => new ArrayType(type); | ||
exports.union = (schemas) => new UnionType(schemas); | ||
exports.union = (schemas, opts) => new UnionType(schemas, opts); | ||
exports.intersection = (schemas) => new IntersectionType(schemas); | ||
@@ -231,0 +242,0 @@ const undefinedValue = () => new UndefinedType(); |
{ | ||
"name": "myzod", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./libs/index.js", |
Sorry, the diff of this file is not supported yet
21253
372