Comparing version 2.0.0-beta.16 to 2.0.0-beta.17
@@ -68,4 +68,4 @@ import { ZodString, ZodStringDef } from './types/string'; | ||
declare const codegen: () => ZodCodeGenerator; | ||
export declare const custom: <T>(check: (data: unknown) => any, params?: string | Partial<Pick<import("./ZodError").ZodCustomIssue, "path" | "message" | "params">> | ((arg: any) => Partial<Pick<import("./ZodError").ZodCustomIssue, "path" | "message" | "params">>) | undefined) => ZodType<T, ZodTypeDef, T>; | ||
declare const instanceOfType: <T extends new (...args: any[]) => any>(cls: T, params?: string | Partial<Pick<import("./ZodError").ZodCustomIssue, "path" | "message" | "params">> | ((arg: any) => Partial<Pick<import("./ZodError").ZodCustomIssue, "path" | "message" | "params">>) | undefined) => ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>; | ||
export declare const custom: <T>(check?: ((data: unknown) => any) | undefined, params?: string | Partial<Pick<import("./ZodError").ZodCustomIssue, "path" | "message" | "params">> | ((arg: unknown) => Partial<Pick<import("./ZodError").ZodCustomIssue, "path" | "message" | "params">>) | undefined) => ZodType<T, ZodTypeDef, T>; | ||
declare const instanceOfType: <T extends new (...args: any[]) => any>(cls: T, params?: string | Partial<Pick<import("./ZodError").ZodCustomIssue, "path" | "message" | "params">> | ((arg: unknown) => Partial<Pick<import("./ZodError").ZodCustomIssue, "path" | "message" | "params">>) | undefined) => ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>; | ||
export { stringType as string, numberType as number, bigIntType as bigint, booleanType as boolean, dateType as date, undefinedType as undefined, nullType as null, anyType as any, unknownType as unknown, neverType as never, voidType as void, arrayType as array, objectType as object, unionType as union, intersectionType as intersection, tupleType as tuple, recordType as record, mapType as map, functionType as function, lazyType as lazy, literalType as literal, enumType as enum, nativeEnumType as nativeEnum, promiseType as promise, instanceOfType as instanceof, transformerType as transformer, optionalType as optional, nullableType as nullable, ostring, onumber, oboolean, codegen, }; | ||
@@ -72,0 +72,0 @@ export declare const late: { |
@@ -132,3 +132,7 @@ "use strict"; | ||
exports.codegen = codegen; | ||
exports.custom = function (check, params) { return anyType().refine(check, params); }; | ||
exports.custom = function (check, params) { | ||
if (check) | ||
return anyType().refine(check, params); | ||
return anyType(); | ||
}; | ||
var instanceOfType = function (cls, params) { | ||
@@ -135,0 +139,0 @@ if (params === void 0) { params = { |
@@ -207,5 +207,17 @@ "use strict"; | ||
}; | ||
var HANDLE = function (err) { | ||
if (err instanceof ZodError_1.ZodError) { | ||
ERROR.addIssues(err.issues); | ||
} | ||
throw err; | ||
}; | ||
// const defaultPROMISE = Symbol('return_value'); | ||
// let returnValue: PseudoPromise<any>; // = defaultReturnValue; | ||
var parsedType = exports.getParsedType(data); | ||
// console.log( | ||
// `\n============\nPARSING ${def.t.toUpperCase()} at ${params.path.join( | ||
// '.', | ||
// )}`, | ||
// ); | ||
// console.log(JSON.stringify(data, null, 2)); | ||
switch (def.t) { | ||
@@ -1016,14 +1028,9 @@ case z.ZodTypes.string: | ||
.then(function () { | ||
try { | ||
return def.input.parse(data, params); | ||
} | ||
catch (err) { | ||
if (err instanceof ZodError_1.ZodError) { | ||
ERROR.addIssues(err.issues); | ||
} | ||
throw err; | ||
} | ||
return def.input.parse(data, params); | ||
}) | ||
.catch(HANDLE) | ||
.then(function (inputParseResult) { | ||
// try { | ||
// console.log(`TRANSFORMING`); | ||
// console.log(JSON.stringify(inputParseResult, null, 2)); | ||
var transformed = def.transformer(inputParseResult); | ||
@@ -1035,2 +1042,4 @@ if (transformed instanceof Promise && params.async === false) { | ||
} | ||
// console.log(`RESULT`); | ||
// console.log(JSON.stringify(transformed, null, 2)); | ||
return transformed; | ||
@@ -1046,19 +1055,13 @@ // } catch (err) { | ||
.then(function (transformedResult) { | ||
try { | ||
return def.output.parse(transformedResult, params); | ||
} | ||
catch (err) { | ||
if (err instanceof ZodError_1.ZodError) { | ||
ERROR.addIssues(err.issues); | ||
return util_1.INVALID; | ||
} | ||
throw err; | ||
} | ||
// try { | ||
return def.output.parse(transformedResult, params); | ||
// } catch (err) { | ||
// if (err instanceof ZodError) { | ||
// ERROR.addIssues(err.issues); | ||
// return INVALID; | ||
// } | ||
// throw err; | ||
// } | ||
}) | ||
.catch(function (err) { | ||
if (!(err instanceof ZodError_1.ZodError)) { | ||
throw err; | ||
} | ||
ERROR.addIssues(err.issues); | ||
}); | ||
.catch(HANDLE); | ||
break; | ||
@@ -1065,0 +1068,0 @@ default: |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var z = __importStar(require(".")); | ||
var run = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
data = z | ||
.string() | ||
.default(function (schema) { return "" + schema._def.t; }) | ||
.parse(undefined); | ||
console.log(data); | ||
return [2 /*return*/]; | ||
}); | ||
}); }; | ||
run(); | ||
// import * as z from '.'; | ||
// const XmlJsonArray = <Key extends string, Schema extends z.ZodTypeAny>( | ||
// element: Key, | ||
// schema: Schema, | ||
// ) => { | ||
// return z.object({}).setKey(element, z.union([schema, z.array(schema)])); | ||
// }; | ||
// const test = XmlJsonArray('asdf', z.string()); | ||
// // both work | ||
// test.parse({ asdf: 'hello' }); | ||
// test.parse({ asdf: ['hello'] }); | ||
// type test = z.infer<typeof test>; | ||
//# sourceMappingURL=playground.js.map |
@@ -35,3 +35,3 @@ import { ZodArray, ZodTransformer, ZodError } from '../index'; | ||
} | ||
export declare type ZodTypeAny = ZodType<any, any>; | ||
export declare type ZodTypeAny = ZodType<unknown, any, unknown>; | ||
export declare type ZodRawShape = { | ||
@@ -126,3 +126,3 @@ [k: string]: ZodTypeAny; | ||
transform<This extends this, U extends ZodType<any>, Tx extends (arg: This['_output']) => U['_input'] | Promise<U['_input']>>(input: U, transformer: Tx): ZodTransformer<This, U>; | ||
transform<This extends this, Tx extends (arg: This['_output']) => This['_input'] | Promise<This['_input']>>(transformer: Tx): ZodTransformer<This, This>; | ||
transform<This extends this, Tx extends (arg: This['_output']) => This['_output'] | Promise<This['_output']>>(transformer: Tx): ZodTransformer<This, This>; | ||
default<T extends Output = Output, Opt extends ReturnType<this['optional']> = ReturnType<this['optional']>>(def: T): ZodTransformer<Opt, this>; | ||
@@ -129,0 +129,0 @@ default<T extends (arg: this) => Output, Opt extends ReturnType<this['optional']> = ReturnType<this['optional']>>(def: T): ZodTransformer<Opt, this>; |
@@ -282,3 +282,3 @@ "use strict"; | ||
} | ||
return index_1.ZodTransformer.create(this, this, input); | ||
return index_1.ZodTransformer.create(this, exports.outputSchema(this), input); | ||
}; | ||
@@ -285,0 +285,0 @@ ZodType.prototype.default = function (def) { |
@@ -32,5 +32,3 @@ import * as z from './base'; | ||
t: z.ZodTypes.object; | ||
shape: { | ||
[x: string]: any; | ||
}[]; | ||
shape: any; | ||
}; | ||
@@ -43,2 +41,3 @@ strict: () => ZodObject<T, "strict", Catchall, objectOutputType<T, Catchall>, objectInputType<T, Catchall>>; | ||
extend: <Augmentation extends z.ZodRawShape>(augmentation: Augmentation) => ZodObject<{ [k in Exclude<keyof T, keyof Augmentation>]: T[k]; } & { [k_1 in keyof Augmentation]: Augmentation[k_1]; }, UnknownKeys, Catchall, objectOutputType<{ [k in Exclude<keyof T, keyof Augmentation>]: T[k]; } & { [k_1 in keyof Augmentation]: Augmentation[k_1]; }, Catchall>, objectInputType<{ [k in Exclude<keyof T, keyof Augmentation>]: T[k]; } & { [k_1 in keyof Augmentation]: Augmentation[k_1]; }, Catchall>>; | ||
setKey: <Key extends string, Schema extends z.ZodTypeAny>(key: Key, schema: Schema) => ZodObject<T & { [k in Key]: Schema; }, UnknownKeys, Catchall, objectOutputType<T & { [k in Key]: Schema; }, Catchall>, objectInputType<T & { [k in Key]: Schema; }, Catchall>>; | ||
/** | ||
@@ -45,0 +44,0 @@ * Prior to zod@1.0.12 there was a bug in the |
@@ -26,2 +26,22 @@ "use strict"; | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
@@ -47,3 +67,3 @@ if (mod && mod.__esModule) return mod; | ||
t: def.t, | ||
shape: Object.assign({}, Object.keys(def.shape()).map(function (k) { | ||
shape: Object.assign.apply(Object, __spread([{}], Object.keys(def.shape()).map(function (k) { | ||
var _a; | ||
@@ -53,3 +73,3 @@ return (_a = {}, | ||
_a); | ||
})), | ||
}))), | ||
}); }; | ||
@@ -86,2 +106,6 @@ // export type AnyZodObject = ZodObject< | ||
_this.extend = AugmentFactory(_this._def); | ||
_this.setKey = function (key, schema) { | ||
var _a; | ||
return _this.augment((_a = {}, _a[key] = schema, _a)); | ||
}; | ||
/** | ||
@@ -88,0 +112,0 @@ * Prior to zod@1.0.12 there was a bug in the |
@@ -14,4 +14,5 @@ import * as z from './base'; | ||
}; | ||
get output(): U; | ||
static create: <I extends z.ZodTypeAny, O extends z.ZodTypeAny>(input: I, output: O, transformer: (arg: I["_output"]) => O["_input"] | Promise<O["_input"]>) => ZodTransformer<I, O>; | ||
static fromSchema: <I extends z.ZodTypeAny>(input: I) => ZodTransformer<I, I>; | ||
} |
@@ -57,8 +57,15 @@ "use strict"; | ||
} | ||
// transformTo: <Out extends z.ZodTypeAny>( | ||
// output: Out, | ||
// transformer: (arg: U['_type']) => Out['_type'], | ||
// ) => ZodTransformer<this, Out> = (output, transformer) => { | ||
// return ZodTransformer.create(this as any, output, transformer) as any; | ||
// }; | ||
Object.defineProperty(ZodTransformer.prototype, "output", { | ||
// transformTo: <Out extends z.ZodTypeAny>( | ||
// output: Out, | ||
// transformer: (arg: U['_type']) => Out['_type'], | ||
// ) => ZodTransformer<this, Out> = (output, transformer) => { | ||
// return ZodTransformer.create(this as any, output, transformer) as any; | ||
// }; | ||
get: function () { | ||
return this._def.output; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
ZodTransformer.create = function (input, output, transformer) { | ||
@@ -65,0 +72,0 @@ return new ZodTransformer({ |
@@ -75,2 +75,4 @@ "use strict"; | ||
_this.addIssue = function (sub) { | ||
// console.log(`adding issue...`); | ||
// console.log(JSON.stringify(sub, null, 2)); | ||
_this.issues = __spread(_this.issues, [sub]); | ||
@@ -80,2 +82,4 @@ }; | ||
if (subs === void 0) { subs = []; } | ||
// console.log(`adding issues...`); | ||
// console.log(JSON.stringify(subs, null, 2)); | ||
_this.issues = __spread(_this.issues, subs); | ||
@@ -82,0 +86,0 @@ }; |
{ | ||
"name": "zod", | ||
"version": "2.0.0-beta.16", | ||
"version": "2.0.0-beta.17", | ||
"description": "TypeScript-first schema declaration and validation library with static type inference", | ||
@@ -5,0 +5,0 @@ "main": "./lib/src/index.js", |
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
417014
5949