Comparing version 2.0.0-beta.2 to 2.0.0-beta.3
@@ -66,4 +66,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").ZodCustomError, "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").ZodCustomError, "path" | "message" | "params">> | undefined) => ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>; | ||
export declare const custom: <T>(check: (data: unknown) => any, params?: string | Partial<Pick<import("./ZodError").ZodCustomError, "path" | "message" | "params">> | ((arg: any) => Partial<Pick<import("./ZodError").ZodCustomError, "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").ZodCustomError, "path" | "message" | "params">> | ((arg: any) => Partial<Pick<import("./ZodError").ZodCustomError, "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, 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, }; | ||
@@ -70,0 +70,0 @@ export declare const late: { |
"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 dynamicRefine; | ||
return __generator(this, function (_a) { | ||
dynamicRefine = z.string().refine(function (val) { return val === val.toUpperCase(); }, function (val) { return ({ params: { val: val } }); }); | ||
console.log(dynamicRefine.safeParse('asdf')); | ||
return [2 /*return*/]; | ||
}); | ||
}); }; | ||
run(); | ||
//# sourceMappingURL=playground.js.map |
@@ -75,6 +75,41 @@ import { ParseParams, MakeErrorData } from '../parser'; | ||
}>; | ||
spa: (x: unknown, params?: ParseParams | undefined) => Promise<{ | ||
success: true; | ||
data: Output; | ||
} | { | ||
success: false; | ||
error: ZodError; | ||
}>; | ||
is(u: Input): u is Input; | ||
check(u: unknown): u is Input; | ||
refine: <Func extends (arg: Output) => any>(check: Func, message?: string | Partial<Pick<ZodCustomError, "path" | "message" | "params">>) => this; | ||
refinement: (check: (arg: Output) => any, refinementData: MakeErrorData) => this; | ||
refine: <Func extends (arg: Output) => any>(check: Func, message?: string | Partial<Pick<ZodCustomError, "path" | "message" | "params">> | ((arg: Output) => Partial<Pick<ZodCustomError, "path" | "message" | "params">>)) => this; | ||
refinement: (check: (arg: Output) => any, refinementData: (Pick<import("..").ZodInvalidTypeError, "code" | "expected" | "received" | "message"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodNonEmptyArrayIsEmptyError, "code" | "message"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodUnrecognizedKeysError, "keys" | "code" | "message"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodInvalidUnionError, "code" | "message" | "unionErrors"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodInvalidLiteralValueError, "code" | "expected" | "message"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodInvalidEnumValueError, "code" | "message" | "options"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodInvalidArgumentsError, "code" | "message" | "argumentsError"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodInvalidReturnTypeError, "code" | "message" | "returnTypeError"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodInvalidDateError, "code" | "message"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodInvalidStringError, "code" | "message" | "validation"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodTooSmallError, "code" | "message" | "minimum" | "inclusive" | "type"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodTooBigError, "code" | "message" | "inclusive" | "type" | "maximum"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<import("..").ZodInvalidIntersectionTypesError, "code" | "message"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | (Pick<ZodCustomError, "code" | "message" | "params"> & { | ||
path?: (string | number)[] | undefined; | ||
}) | ((arg: Output) => MakeErrorData)) => this; | ||
protected _refinement: (refinement: InternalCheck<Output>['check']) => this; | ||
@@ -81,0 +116,0 @@ constructor(def: Def); |
@@ -147,2 +147,3 @@ "use strict"; | ||
}); }; | ||
this.spa = this.safeParseAsync; | ||
this.refine = function (check, message) { | ||
@@ -171,2 +172,20 @@ if (message === void 0) { message = 'Invalid value.'; } | ||
} | ||
if (typeof message === 'function') { | ||
return _this._refinement(function (val, ctx) { | ||
var result = check(val); | ||
var setError = function () { | ||
return ctx.makeError(__assign({ code: index_1.ZodErrorCode.custom_error }, message(val))); | ||
}; | ||
if (result instanceof Promise) { | ||
return result.then(function (data) { | ||
if (!data) | ||
setError(); | ||
}); | ||
} | ||
if (!result) { | ||
setError(); | ||
return result; | ||
} | ||
}); | ||
} | ||
return _this._refinement(function (val, ctx) { | ||
@@ -192,3 +211,5 @@ var result = check(val); | ||
if (!check(val)) { | ||
ctx.makeError(refinementData); | ||
ctx.makeError(typeof refinementData === 'function' | ||
? refinementData(val) | ||
: refinementData); | ||
} | ||
@@ -195,0 +216,0 @@ }); |
{ | ||
"name": "zod", | ||
"version": "2.0.0-beta.2", | ||
"version": "2.0.0-beta.3", | ||
"description": "TypeScript-first schema declaration and validation library with static type inference", | ||
@@ -59,2 +59,2 @@ "main": "./lib/src/index.js", | ||
} | ||
} | ||
} |
@@ -119,3 +119,3 @@ <p align="center"> | ||
- [.partial/.deepPartial](#partials) | ||
- [.strip](#strip-unknown-keys) | ||
- [.passthrough](#pass-through-unknown-keys) | ||
- [.strict](#disallow-unknown-keys) | ||
@@ -122,0 +122,0 @@ - [.primitives/.nonprimitives](#primitives-and-nonprimitives) |
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
368023
5067