Comparing version 0.0.8 to 0.0.9
@@ -16,3 +16,3 @@ declare abstract class Type<T> { | ||
declare type Eval<T> = { | ||
[K in keyof T]: T[K]; | ||
[Key in keyof T]: T[Key]; | ||
} & {}; | ||
@@ -24,2 +24,4 @@ export declare type Infer<T extends AnyType> = T extends Type<infer K> ? Eval<K> : any; | ||
max: number; | ||
predicate: (value: string) => boolean; | ||
predicateErrMsg: string; | ||
}>; | ||
@@ -33,2 +35,3 @@ declare class StringType extends Type<string> { | ||
max(x: number): this; | ||
predicate(fn: StringOptions['predicate'], errMsg?: string): this; | ||
} | ||
@@ -77,4 +80,3 @@ declare class BooleanType extends Type<boolean> { | ||
constructor(objectShape: T, opts?: ObjectOptions | undefined); | ||
parse(value: unknown, optOverrides?: ObjectOptions & PathOptions): InferObjectShape<T>; | ||
getShapeKeys(): string[]; | ||
parse(value: unknown, optOverrides?: ObjectOptions & PathOptions): Eval<InferObjectShape<T>>; | ||
} | ||
@@ -86,6 +88,17 @@ declare class RecordType<T extends AnyType> extends Type<Record<string, Infer<T>>> { | ||
} | ||
declare type ArrayOptions = Partial<{ | ||
length: number; | ||
min: number; | ||
max: number; | ||
unique: boolean; | ||
}>; | ||
declare class ArrayType<T extends AnyType> extends Type<Infer<T>[]> { | ||
private readonly schema; | ||
constructor(schema: T); | ||
parse(value: unknown, opts?: PathOptions): Infer<T>[]; | ||
private readonly opts; | ||
constructor(schema: T, opts?: ArrayOptions); | ||
parse(value: unknown, parseOptions?: PathOptions): Infer<T>[]; | ||
length(value: number): this; | ||
min(value: number): this; | ||
max(value: number): this; | ||
unique(value?: boolean): this; | ||
} | ||
@@ -105,6 +118,6 @@ declare type TupleToUnion<T extends any[]> = T[number]; | ||
} | ||
declare class IntersectionType<T extends AnyType, K extends AnyType> extends Type<Infer<T> & Infer<K>> { | ||
declare class IntersectionType<T extends AnyType, K extends AnyType> extends Type<Eval<Infer<T> & Infer<K>>> { | ||
private readonly schemas; | ||
constructor(left: T, right: K); | ||
parse(value: unknown): Infer<T> & Infer<K>; | ||
parse(value: unknown): Eval<Infer<T> & Infer<K>>; | ||
} | ||
@@ -122,2 +135,4 @@ declare type ValueOf<T> = T[keyof T]; | ||
max: number; | ||
predicate: (value: string) => boolean; | ||
predicateErrMsg: string; | ||
}> | undefined) => StringType; | ||
@@ -132,3 +147,8 @@ export declare const boolean: () => BooleanType; | ||
export declare const object: <T extends object>(shape: T, opts?: ObjectOptions | undefined) => ObjectType<T>; | ||
export declare const array: <T extends AnyType>(type: T) => ArrayType<T>; | ||
export declare const array: <T extends AnyType>(type: T, opts?: Partial<{ | ||
length: number; | ||
min: number; | ||
max: number; | ||
unique: boolean; | ||
}> | undefined) => ArrayType<T>; | ||
export declare const union: <T extends AnyType[]>(schemas: T, opts?: UnionOptions | undefined) => UnionType<T>; | ||
@@ -147,2 +167,4 @@ export declare const intersection: <T extends AnyType, K extends AnyType>(l: T, r: K) => IntersectionType<T, K>; | ||
max: number; | ||
predicate: (value: string) => boolean; | ||
predicateErrMsg: string; | ||
}> | undefined) => StringType; | ||
@@ -157,3 +179,8 @@ boolean: () => BooleanType; | ||
object: <T_1 extends object>(shape: T_1, opts?: ObjectOptions | undefined) => ObjectType<T_1>; | ||
array: <T_2 extends AnyType>(type: T_2) => ArrayType<T_2>; | ||
array: <T_2 extends AnyType>(type: T_2, opts?: Partial<{ | ||
length: number; | ||
min: number; | ||
max: number; | ||
unique: boolean; | ||
}> | undefined) => ArrayType<T_2>; | ||
union: <T_3 extends AnyType[]>(schemas: T_3, opts?: UnionOptions | undefined) => UnionType<T_3>; | ||
@@ -163,3 +190,4 @@ intersection: <T_4 extends AnyType, K extends AnyType>(l: T_4, r: K) => IntersectionType<T_4, K>; | ||
null: () => NullType; | ||
enum: <T_5>(e: T_5) => EnumType<T_5>; | ||
}; | ||
export default _default; |
@@ -49,3 +49,2 @@ "use strict"; | ||
} | ||
// Primitives | ||
class StringType extends Type { | ||
@@ -69,2 +68,15 @@ constructor(opts = {}) { | ||
} | ||
if (this.opts.predicate) { | ||
try { | ||
if (this.opts.predicate(value) === false) { | ||
throw new ValidationError(this.opts.predicateErrMsg || 'expected string to pass predicate function'); | ||
} | ||
} | ||
catch (err) { | ||
if (err instanceof ValidationError) { | ||
throw err; | ||
} | ||
throw new ValidationError(err.message); | ||
} | ||
} | ||
return value; | ||
@@ -84,2 +96,7 @@ } | ||
} | ||
predicate(fn, errMsg) { | ||
this.opts.predicate = fn; | ||
this.opts.predicateErrMsg = errMsg; | ||
return this; | ||
} | ||
} | ||
@@ -204,5 +221,2 @@ class BooleanType extends Type { | ||
} | ||
getShapeKeys() { | ||
return Object.keys(this.objectShape); | ||
} | ||
} | ||
@@ -241,10 +255,32 @@ class RecordType extends Type { | ||
class ArrayType extends Type { | ||
constructor(schema) { | ||
constructor(schema, opts = {}) { | ||
super(); | ||
this.schema = schema; | ||
this.opts = opts; | ||
} | ||
parse(value, opts) { | ||
parse(value, parseOptions) { | ||
if (!Array.isArray(value)) { | ||
throw new ValidationError('expected an array but got ' + typeOf(value)); | ||
} | ||
if (typeof this.opts.length === 'number' && this.opts.length >= 0 && value.length !== this.opts.length) { | ||
throw new ValidationError(`expected array to have length ${this.opts.length} but got ${value.length}`); | ||
} | ||
if (typeof this.opts.min === 'number' && value.length < this.opts.min) { | ||
throw new ValidationError(`expected array to have length greater than or equal to ${this.opts.min} but got ${value.length}`); | ||
} | ||
if (typeof this.opts.max === 'number' && value.length > this.opts.max) { | ||
throw new ValidationError(`expected array to have length less than or equal to ${this.opts.max} but got ${value.length}`); | ||
} | ||
if (this.opts.unique === true && new Set(value).size !== value.length) { | ||
const seenMap = new Map(); | ||
value.forEach((elem, idx) => { | ||
const seenAt = seenMap.get(elem); | ||
if (!seenAt) { | ||
seenMap.set(elem, [idx]); | ||
} | ||
else { | ||
throw new ValidationError(`expected array to be unique but found same element at indexes ${seenAt[0]} and ${idx}`); | ||
} | ||
}); | ||
} | ||
value.forEach((elem, idx) => { | ||
@@ -261,3 +297,4 @@ try { | ||
const path = err.path ? [idx, ...err.path] : [idx]; | ||
const msg = (opts === null || opts === void 0 ? void 0 : opts.suppressPathErrMsg) ? err.message : `error at ${prettyPrintPath(path)} - ${err.message}`; | ||
const msg = (parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.suppressPathErrMsg) ? err.message | ||
: `error at ${prettyPrintPath(path)} - ${err.message}`; | ||
throw new ValidationError(msg, path); | ||
@@ -268,2 +305,18 @@ } | ||
} | ||
length(value) { | ||
this.opts.length = value; | ||
return this; | ||
} | ||
min(value) { | ||
this.opts.min = value; | ||
return this; | ||
} | ||
max(value) { | ||
this.opts.max = value; | ||
return this; | ||
} | ||
unique(value = true) { | ||
this.opts.unique = value; | ||
return this; | ||
} | ||
} | ||
@@ -358,3 +411,3 @@ class UnionType extends Type { | ||
exports.object = (shape, opts) => new ObjectType(shape, opts); | ||
exports.array = (type) => new ArrayType(type); | ||
exports.array = (type, opts) => new ArrayType(type, opts); | ||
exports.union = (schemas, opts) => new UnionType(schemas, opts); | ||
@@ -383,3 +436,3 @@ exports.intersection = (l, r) => new IntersectionType(l, r); | ||
null: nullValue, | ||
enum: enumValue, | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "myzod", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./libs/index.js", |
@@ -12,3 +12,3 @@ { | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"sourceMap": false, | ||
"outDir": "libs", | ||
@@ -15,0 +15,0 @@ "noUnusedLocals": true, |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
33186
6
632
0
362