Comparing version 1.2.3 to 1.2.4
"use strict"; | ||
// import { Primitive } from './primitive'; | ||
// type AnyObject = { [k: string]: any }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// type test = any[] extends Record<string, any> ? true : false; | ||
// type test1 = maskUtil.Params<{ name: string }>; | ||
// type test2 = maskUtil.Params<{ name: string }[]>; | ||
// type test3 = maskUtil.Params<{ name: string } | undefined>; | ||
// type test4 = maskUtil.Params<undefined>; | ||
// type test5 = maskUtil.Params<null>; | ||
// type test6 = maskUtil.Params<string>; | ||
// type test7 = maskUtil.Params<string | boolean | undefined>; | ||
// type test8 = maskUtil.Params<{ properties: { name: string; number: number } }>; | ||
// type test9 = IsObjectArray<[]>; | ||
// type test10 = IsObject<[]>; | ||
// type test11 = true extends IsObject<{ firstName: string; lastName: string }[]> ? true : false; | ||
// type test11b = true extends IsObjectArray<{ firstName: string; lastName: string }[]> ? true : false; | ||
// type test12 = maskUtil.Pick<{ firstName: string }, true>; | ||
// type test13 = maskUtil.Pick<{ firstName: string }, {}>; | ||
// type test14 = maskUtil.PickTest<{ firstName: string; lastName: string }, {}>; | ||
// type test15 = maskUtil.Pick< | ||
// { firstName: string | undefined; lastName: string | undefined | null }[] | undefined, | ||
// { firstName: true } | ||
// >; | ||
// type test16 = maskUtil.Pick<{ username: string; points: number }, { points: true }>; | ||
// type test16 = {} extends true ? true : false; | ||
// export namespace maskUtil { | ||
@@ -5,0 +26,0 @@ // export type Params<T> = { |
@@ -37,5 +37,5 @@ import { ZodRawShape } from '../types/base'; | ||
}; | ||
type ObjectType<T extends ZodRawShape> = NoNever<FlattenObject<ObjectIntersection<T>>>; | ||
type ObjectType<T extends ZodRawShape> = FlattenObject<ObjectIntersection<NoNever<T>>>; | ||
const mergeShapes: <U extends ZodRawShape, T extends ZodRawShape>(first: U, second: T) => T & U; | ||
const mergeObjects: <FirstShape extends ZodRawShape, FirstParams extends ZodObjectParams>(first: ZodObject<FirstShape, FirstParams>) => <SecondShape extends ZodRawShape, SecondParams extends ZodObjectParams>(second: ZodObject<SecondShape, SecondParams>) => ZodObject<FirstShape & SecondShape, MergeObjectParams<FirstParams, SecondParams>>; | ||
} |
@@ -34,3 +34,6 @@ "use strict"; | ||
t: base_1.ZodTypes.object, | ||
strict: first._def.strict && second._def.strict, | ||
// strict: first.params.strict && second.params.strict, | ||
params: { | ||
strict: first.params.strict && second.params.strict, | ||
}, | ||
shape: mergedShape, | ||
@@ -37,0 +40,0 @@ }); |
"use strict"; | ||
// import { ZodRawShape } from '../types/base'; | ||
// import { ZodIntersection } from '../types/intersection'; | ||
// import { ZodObject } from '../types/object'; | ||
// export type Merge<U extends object, V extends object> = { | ||
// [k in Exclude<keyof U, keyof V>]: U[k]; | ||
// } & | ||
// V; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var util; | ||
(function (util) { | ||
function assertNever(_x) { | ||
throw new Error(); | ||
} | ||
util.assertNever = assertNever; | ||
})(util = exports.util || (exports.util = {})); | ||
//# sourceMappingURL=util.js.map |
@@ -68,2 +68,3 @@ "use strict"; | ||
exports.lazy = lazyType; | ||
// const lazyobjectType = ZodLazyObject.create; | ||
// const recursionType = ZodObject.recursion; | ||
@@ -70,0 +71,0 @@ var literalType = literal_1.ZodLiteral.create; |
import * as z from './types/base'; | ||
export declare const applyMask: (schema: z.ZodType<any, z.ZodTypeDef>, mask: any, mode: "omit" | "pick") => any; | ||
export declare const Masker: (schema: z.ZodType<any, z.ZodTypeDef>, params: any) => z.ZodType<any, z.ZodTypeDef>; |
@@ -11,61 +11,133 @@ "use strict"; | ||
var z = __importStar(require("./types/base")); | ||
var array_1 = require("./types/array"); | ||
var object_1 = require("./types/object"); | ||
exports.applyMask = function (schema, mask, mode) { | ||
var _def = schema._def; | ||
var def = _def; | ||
if (mask === true) { | ||
return schema; | ||
} | ||
else if (typeof mask === 'object' && !Array.isArray(mask)) { | ||
if (def.t === z.ZodTypes.array) { | ||
if (def.type._def.t === z.ZodTypes.object) { | ||
return new array_1.ZodArray({ | ||
t: z.ZodTypes.array, | ||
nonempty: def.nonempty, | ||
type: exports.applyMask(def.type, mask, mode), | ||
}); | ||
var _1 = require("."); | ||
// import { util } from './helpers/util';; | ||
// import { ZodArray } from './types/array'; | ||
// import { ZodObject } from './types/object'; | ||
// import { ZodUnion } from './types/union'; | ||
// import { ZodIntersection } from './types/intersection'; | ||
// import { ZodTuple } from './types/tuple'; | ||
// import { ZodRecord } from './types/record'; | ||
// import { ZodLazy } from './types/lazy'; | ||
// import { ZodError } from './ZodError'; | ||
var sampleParamVisitor_1 = require("./sampleParamVisitor"); | ||
// import { ZodObject } from './types/object'; | ||
// type Params = any; //{ [k: string]: boolean | Params } | boolean; | ||
exports.Masker = sampleParamVisitor_1.ParamVisitor(function (schema, params) { | ||
var def = schema._def; | ||
console.log("visiting " + def.t); | ||
console.log("params: " + JSON.stringify(params, null, 2), null, 2); | ||
if (def.t === z.ZodTypes.object) { | ||
console.log(schema); | ||
// console.log(JSON.stringify(params, null, 2)); | ||
var visitedShape = {}; | ||
for (var key in def.shape) { | ||
if (params[key]) { | ||
visitedShape[key] = def.shape[key]; | ||
} | ||
else { | ||
throw new Error("You can only " + mode + " arrays of objects."); | ||
} | ||
} | ||
else if (def.t === z.ZodTypes.object) { | ||
var modShape = {}; | ||
var shape = def.shape; | ||
if (mode === 'pick') { | ||
if (mask === true) | ||
return shape; | ||
for (var key in mask) { | ||
if (!Object.keys(shape).includes(key)) | ||
throw new Error("Unknown key in pick: " + key); | ||
modShape[key] = exports.applyMask(shape[key], mask[key], mode); | ||
} | ||
} | ||
if (mode === 'omit') { | ||
for (var maskKey in mask) { | ||
if (!Object.keys(shape).includes(maskKey)) | ||
throw new Error("Unknown key in omit: " + maskKey); | ||
} | ||
for (var key in shape) { | ||
if (mask[key] === true) { | ||
continue; | ||
} | ||
else if (typeof mask[key] === 'object') { | ||
modShape[key] = exports.applyMask(shape[key], mask[key], mode); | ||
} | ||
else { | ||
modShape[key] = shape[key]; | ||
} | ||
} | ||
} | ||
return new object_1.ZodObject({ | ||
t: z.ZodTypes.object, | ||
strict: def.strict, | ||
shape: modShape, | ||
}); | ||
} | ||
return _1.ZodObject.create(visitedShape); | ||
} | ||
throw new Error("Invalid mask!\n\n" + JSON.stringify(mask, null, 2)); | ||
}; | ||
else { | ||
return schema; | ||
} | ||
}); | ||
// export const Masker = (visit: (_schema: z.ZodAny, params: Params) => z.ZodAny) => ( | ||
// schema: z.ZodAny, | ||
// params: Params, | ||
// ) => { | ||
// const def: ZodDef = schema._def as any; | ||
// if (params === true) return schema; | ||
// if (typeof params !== 'object') throw new Error('Invalid params'); | ||
// if (Array.isArray(params)) throw new Error('Invalid params'); | ||
// switch (def.t) { | ||
// case z.ZodTypes.string: | ||
// return visit(schema, params); | ||
// case z.ZodTypes.number: | ||
// return visit(schema, params); | ||
// case z.ZodTypes.boolean: | ||
// return visit(schema, params); | ||
// case z.ZodTypes.date: | ||
// return visit(schema, params); | ||
// case z.ZodTypes.undefined: | ||
// return visit(schema, params); | ||
// case z.ZodTypes.null: | ||
// return visit(schema, params); | ||
// case z.ZodTypes.array: | ||
// return visit( | ||
// new ZodArray({ | ||
// ...def, | ||
// type: visit(def.type, params), | ||
// }), | ||
// params, | ||
// ); | ||
// case z.ZodTypes.object: | ||
// const visitedShape: any = {}; | ||
// for (const key in def.shape) { | ||
// if (params[key]) { | ||
// visitedShape[key] = visit(def.shape[key], params[key]); | ||
// // if(params[key] === true){ | ||
// // visitedShape[key] = def.shape[key] | ||
// // }else if(typeof params === "object"){ | ||
// // visitedShape[key] = visit(def.shape[key], params[key]); | ||
// // } | ||
// } | ||
// } | ||
// return visit( | ||
// new ZodObject({ | ||
// ...def, | ||
// shape: visitedShape, | ||
// }), | ||
// params, | ||
// ); | ||
// case z.ZodTypes.union: | ||
// return visit( | ||
// new ZodUnion({ | ||
// ...def, | ||
// options: def.options.map(option => visit(option, params)) as any, | ||
// }), | ||
// params, | ||
// ); | ||
// case z.ZodTypes.intersection: | ||
// return visit( | ||
// new ZodIntersection({ | ||
// ...def, | ||
// left: visit(def.left, params), | ||
// right: visit(def.left, params), | ||
// }), | ||
// params, | ||
// ); | ||
// case z.ZodTypes.tuple: | ||
// return visit( | ||
// new ZodTuple({ | ||
// ...def, | ||
// items: def.items.map(item => visit(item, params)) as any, | ||
// }), | ||
// params, | ||
// ); | ||
// case z.ZodTypes.record: | ||
// return visit( | ||
// new ZodRecord({ | ||
// ...def, | ||
// valueType: visit(def.valueType, params), | ||
// }), | ||
// params, | ||
// ); | ||
// case z.ZodTypes.lazy: | ||
// return visit( | ||
// new ZodLazy({ | ||
// ...def, | ||
// getter: () => visit(def.getter(), params), | ||
// }), | ||
// params, | ||
// ); | ||
// case z.ZodTypes.literal: | ||
// return visit(schema, params); | ||
// case z.ZodTypes.enum: | ||
// return visit(schema, params); | ||
// default: | ||
// util.assertNever(def); | ||
// break; | ||
// } | ||
// throw ZodError.fromString(`Unknown schema type.`); | ||
// }; | ||
//# sourceMappingURL=masker.js.map |
@@ -12,7 +12,3 @@ "use strict"; | ||
var ZodError_1 = require("./ZodError"); | ||
function assertNever(x) { | ||
throw ZodError_1.ZodError.fromString('Unexpected object: ' + x); | ||
} | ||
// const seen: any[] = []; | ||
// export const ZodParser = (schemaDef: z.ZodTypeDef) => (obj: any) => ZodParserFactory(schemaDef)(obj, { seen: [] }); | ||
var util_1 = require("./helpers/util"); | ||
exports.ZodParser = function (schemaDef) { return function (obj, params) { | ||
@@ -90,3 +86,3 @@ if (params === void 0) { params = { seen: [] }; } | ||
var shape = def.shape; | ||
if (def.strict) { | ||
if (def.params.strict) { | ||
var shapeKeys_1 = Object.keys(def.shape); | ||
@@ -218,3 +214,3 @@ var objKeys = Object.keys(obj); | ||
// return obj; | ||
assertNever(def); | ||
util_1.util.assertNever(def); | ||
// break; | ||
@@ -221,0 +217,0 @@ } |
"use strict"; | ||
// import * as z from '.'; | ||
// const Animal = z | ||
// .object({ | ||
// species: z.string(), | ||
// }) | ||
// .augment({ | ||
// population: z.number(), | ||
// }); | ||
// // overwrites `species` | ||
// const ModifiedAnimal = Animal.augment({ | ||
// species: z.array(z.string()), | ||
// }); | ||
// ModifiedAnimal.parse({ | ||
// species: ['asd'], | ||
// population: 1324, | ||
// }); | ||
// ({ | ||
// population:11324, | ||
// }) | ||
// import { util } from './types/utils'; | ||
// const fish = z.object({ | ||
// name: z.string(), | ||
// props: z.object({ | ||
// color: z.string(), | ||
// numScales: z.number(), | ||
// }), | ||
// }).augment({ | ||
// name:z.number() | ||
// }); | ||
// type fish = z.infer<typeof fish> | ||
// interface A { | ||
// val: number; | ||
// b: B; | ||
// } | ||
// interface B { | ||
// val: number; | ||
// a: A; | ||
// } | ||
// type TypeToShape<T> = { | ||
// string: z.ZodString; | ||
// number: z.ZodNumber; | ||
// boolean: z.ZodBoolean; | ||
// undefined: z.ZodUndefined; | ||
// null: z.ZodNull; | ||
// array: T extends (infer U)[] ? z.ZodArray<TypeToShape<U>> : never; | ||
// object: z.ZodObject; | ||
// union: z.ZodUnion; | ||
// intersection: z.ZodIntersection; | ||
// tuple: z.ZodTuple; | ||
// record: z.ZodRecord; | ||
// literal: T extends infer U ? z.ZodLiteral<U> : never; | ||
// optional: z.ZodUnion<[z.ZodUndefined, TypeToShape<NoUndef<T>>]>; | ||
// nullable: z.ZodUnion<[z.ZodNull, TypeToShape<NoNull<T>>]>; | ||
// // nullable: z.ZodNull; | ||
// // ZodFunction, | ||
// // ZodLazy, | ||
// // ZodEnum, | ||
// // ZodType, | ||
// // ZodAny, | ||
// // ZodDef, | ||
// // ZodError, | ||
// }[ | ||
// // undefined extends T ? | ||
// util.AssertEqual<T, string> extends true ? 'string' : | ||
// util.AssertEqual<T, number> extends true ? 'number' : | ||
// util.AssertEqual<T, boolean> extends true ? 'boolean' : | ||
// util.AssertEqual<T, null> extends true ? 'null' : | ||
// util.AssertEqual<T, undefined> extends true ? 'undefined' : | ||
// never | ||
// ] | ||
// type StripUndefined<T> = T extends (undefined | infer U) ? U : false | ||
// type NoUndef<T> = T extends undefined ? never : T; | ||
// type NoNull<T> = T extends null ? never : T; | ||
// // type adsf = NoUndef<string | undefined> | ||
// // type lk = keyof number; | ||
// // const fishList = z.array(fish); | ||
// // const numList = z.array(z.string()); | ||
// // const modnumList = numList.pick(true); | ||
// // const modFishList = fishList.pick({ | ||
// // // properties: true, | ||
// // name:true, | ||
// // properties: { | ||
// // color:true | ||
// // } | ||
// // }); | ||
// // const modFish = fish.omit({ | ||
// // name: true, | ||
// // }); | ||
// // const modFishList = fishList.omit({ | ||
// // name: true, | ||
// // properties: { | ||
// // numScales: true, | ||
// // }, | ||
// // }); | ||
// // type nonameFish = z.infer<typeof nonameFish>; | ||
//# sourceMappingURL=playground.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var parser_1 = require("../parser"); | ||
var masker_1 = require("../masker"); | ||
// import { ZodString } from './string'; | ||
// import { maskUtil } from '../helpers/maskUtil'; | ||
@@ -15,3 +17,3 @@ var ZodTypes; | ||
ZodTypes["object"] = "object"; | ||
ZodTypes["interface"] = "interface"; | ||
// interface = 'interface', | ||
ZodTypes["union"] = "union"; | ||
@@ -23,2 +25,3 @@ ZodTypes["intersection"] = "intersection"; | ||
ZodTypes["lazy"] = "lazy"; | ||
ZodTypes["lazyobject"] = "lazyobject"; | ||
ZodTypes["literal"] = "literal"; | ||
@@ -35,19 +38,10 @@ ZodTypes["enum"] = "enum"; | ||
// }; | ||
// assert: zodAssertion<Type> = (value: unknown) => zodAssert(this, value); | ||
// (u: unknown) => asserts u is Type = u => { | ||
// try { | ||
// this.parse(u); | ||
// } catch (err) { | ||
// throw new Error(err.message); | ||
// } | ||
// }; | ||
function ZodType(def) { | ||
var _this = this; | ||
this.mask = function (_params) { | ||
return masker_1.Masker(_this, _params); | ||
}; | ||
this.parse = parser_1.ZodParser(def); | ||
this._def = def; | ||
// this._type = null as any as Type; | ||
} | ||
// mask: (params: MaskParams, params?: ParseParams) => Type; | ||
// mask = <P extends MaskParams<Type>>(params: P): MaskedType<Type, P> => { | ||
// return params as any; | ||
// }; | ||
ZodType.prototype.is = function (u) { | ||
@@ -54,0 +48,0 @@ try { |
@@ -6,6 +6,6 @@ import * as z from './base'; | ||
import { objectUtil } from '../helpers/objectUtil'; | ||
export interface ZodObjectDef<T extends z.ZodRawShape = z.ZodRawShape> extends z.ZodTypeDef { | ||
export interface ZodObjectDef<T extends z.ZodRawShape = z.ZodRawShape, Params extends ZodObjectParams = ZodObjectParams> extends z.ZodTypeDef { | ||
t: z.ZodTypes.object; | ||
shape: T; | ||
strict: boolean; | ||
params: Params; | ||
} | ||
@@ -21,5 +21,7 @@ interface ZodObjectParams { | ||
}> extends z.ZodType<ZodObjectType<T, Params>, // { [k in keyof T]: T[k]['_type'] }, | ||
ZodObjectDef<T>> { | ||
ZodObjectDef<T, Params>> { | ||
readonly _shape: T; | ||
readonly _params: Params; | ||
private readonly _params; | ||
readonly shape: T; | ||
readonly params: Params; | ||
toJSON: () => { | ||
@@ -26,0 +28,0 @@ t: z.ZodTypes.object; |
@@ -34,3 +34,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var z = __importStar(require("./base")); | ||
var z = __importStar(require("./base")); // change | ||
var undefined_1 = require("./undefined"); | ||
@@ -40,2 +40,11 @@ var null_1 = require("./null"); | ||
var objectUtil_1 = require("../helpers/objectUtil"); | ||
// import { ZodString } from './string'; | ||
// import { maskUtil } from '../helpers/maskUtil'; | ||
// import { zodmaskUtil } from '../helpers/zodmaskUtil'; | ||
// import { applyMask } from '../masker'; | ||
var AugmentFactory = function (def) { return function (augmentation) { | ||
return new ZodObject(__assign({}, def, { shape: __assign({}, def.shape, augmentation) })); | ||
}; }; | ||
// const infer = <T extends ZodObjectDef<any>>(def: T): T => def; | ||
// const qewr = infer({ t: z.ZodTypes.object, shape: { asfd: ZodString.create() }, params: { strict: true } }); | ||
var objectDefToJson = function (def) { return ({ | ||
@@ -58,4 +67,5 @@ t: def.t, | ||
shape: _this._def.shape, | ||
strict: false, | ||
// strict: false, | ||
t: z.ZodTypes.object, | ||
params: __assign({}, _this._params, { strict: false }), | ||
}); | ||
@@ -65,5 +75,18 @@ }; | ||
_this.nullable = function () { return union_1.ZodUnion.create([_this, null_1.ZodNull.create()]); }; | ||
_this.augment = function (augmentation) { | ||
return new ZodObject(__assign({}, _this._def, { shape: __assign({}, _this._def.shape, augmentation) })); | ||
}; | ||
// | ||
_this.augment = AugmentFactory(_this._def); | ||
// augment = <Augmentation extends z.ZodRawShape>( | ||
// augmentation: Augmentation, | ||
// ): ZodObject< | ||
// { [k in Exclude<keyof T, keyof Augmentation>]: T[k] } & { [k in keyof Augmentation]: Augmentation[k] }, | ||
// Params | ||
// > => { | ||
// return new ZodObject({ | ||
// ...this._def, | ||
// shape: { | ||
// ...this._def.shape, | ||
// ...augmentation, | ||
// }, | ||
// }) as any; | ||
// }; | ||
/** | ||
@@ -94,2 +117,26 @@ * Prior to zod@1.0.12 there was a bug in the | ||
} | ||
Object.defineProperty(ZodObject.prototype, "shape", { | ||
get: function () { | ||
return this._def.shape; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ZodObject.prototype, "params", { | ||
get: function () { | ||
return this._def.params; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
// partial = <P extends {deep:boolean} | undefined>(params:P):ZodObject<{[k in keyof T]?: T[k]}, Params> => { | ||
// const newShape:any = {}; | ||
// for (const key in this.shape){ | ||
// newShape[key] = this.shape[key].optional(); | ||
// } | ||
// return new ZodObject({ | ||
// ...this._def, | ||
// shape | ||
// }) | ||
// } | ||
// pick = <Mask extends zodmaskUtil.Params<ZodObject<T>>>( | ||
@@ -133,4 +180,7 @@ // mask: Mask, | ||
t: z.ZodTypes.object, | ||
strict: true, | ||
// strict: true, | ||
shape: shape, | ||
params: { | ||
strict: true, | ||
}, | ||
}); | ||
@@ -137,0 +187,0 @@ }; |
import * as z from './base'; | ||
import { ZodUndefined } from './undefined'; | ||
import { ZodNull } from './null'; | ||
export interface ZodUnionDef<T extends [z.ZodAny, ...z.ZodAny[]] = [z.ZodAny, ...z.ZodAny[]]> extends z.ZodTypeDef { | ||
export interface ZodUnionDef<T extends [z.ZodAny, z.ZodAny, ...z.ZodAny[]] = [z.ZodAny, z.ZodAny, ...z.ZodAny[]]> extends z.ZodTypeDef { | ||
t: z.ZodTypes.union; | ||
@@ -6,0 +6,0 @@ options: T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "zod", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "TypeScript-first schema declaration and validation library with static type inference", | ||
@@ -5,0 +5,0 @@ "main": "./lib/src/index.js", |
@@ -69,3 +69,3 @@ <p align="center"> | ||
Zod 1.0.x is compatible with TypeScript 3.2+. Earlier versions contain bugs that will interfere there were known type inference bugs in TypeScript that will cause errors. | ||
Zod 1.0.x is compatible with TypeScript 3.2+. | ||
@@ -95,3 +95,3 @@ # Usage | ||
const booleanSchema = z.boolean(); // => ZodType<boolean> | ||
const dateSchema = z.Date(); // => ZodType<Date> | ||
const dateSchema = z.date(); // => ZodType<Date> | ||
const undefinedSchema = z.undefined(); // => ZodType<undefined> | ||
@@ -98,0 +98,0 @@ const nullTypeSchema = z.null(); // => ZodType<null> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
261706
112
3537