avancement planning
This commit is contained in:
+92
-31
@@ -42,6 +42,7 @@ exports._xid = _xid;
|
||||
exports._ksuid = _ksuid;
|
||||
exports._ipv4 = _ipv4;
|
||||
exports._ipv6 = _ipv6;
|
||||
exports._mac = _mac;
|
||||
exports._cidrv4 = _cidrv4;
|
||||
exports._cidrv6 = _cidrv6;
|
||||
exports._base64 = _base64;
|
||||
@@ -109,6 +110,7 @@ exports._normalize = _normalize;
|
||||
exports._trim = _trim;
|
||||
exports._toLowerCase = _toLowerCase;
|
||||
exports._toUpperCase = _toUpperCase;
|
||||
exports._slugify = _slugify;
|
||||
exports._array = _array;
|
||||
exports._union = _union;
|
||||
exports._discriminatedUnion = _discriminatedUnion;
|
||||
@@ -135,9 +137,14 @@ exports._lazy = _lazy;
|
||||
exports._promise = _promise;
|
||||
exports._custom = _custom;
|
||||
exports._refine = _refine;
|
||||
exports._superRefine = _superRefine;
|
||||
exports._check = _check;
|
||||
exports.describe = describe;
|
||||
exports.meta = meta;
|
||||
exports._stringbool = _stringbool;
|
||||
exports._stringFormat = _stringFormat;
|
||||
const checks = __importStar(require("./checks.cjs"));
|
||||
const registries = __importStar(require("./registries.cjs"));
|
||||
const schemas = __importStar(require("./schemas.cjs"));
|
||||
const util = __importStar(require("./util.cjs"));
|
||||
function _string(Class, params) {
|
||||
@@ -300,6 +307,15 @@ function _ipv6(Class, params) {
|
||||
...util.normalizeParams(params),
|
||||
});
|
||||
}
|
||||
function _mac(Class, params) {
|
||||
return new Class({
|
||||
type: "string",
|
||||
format: "mac",
|
||||
check: "string_format",
|
||||
abort: false,
|
||||
...util.normalizeParams(params),
|
||||
});
|
||||
}
|
||||
function _cidrv4(Class, params) {
|
||||
return new Class({
|
||||
type: "string",
|
||||
@@ -740,6 +756,10 @@ function _toLowerCase() {
|
||||
function _toUpperCase() {
|
||||
return _overwrite((input) => input.toUpperCase());
|
||||
}
|
||||
// slugify
|
||||
function _slugify() {
|
||||
return _overwrite((input) => util.slugify(input));
|
||||
}
|
||||
function _array(Class, element, params) {
|
||||
return new Class({
|
||||
type: "array",
|
||||
@@ -880,7 +900,7 @@ function _default(Class, innerType, defaultValue) {
|
||||
type: "default",
|
||||
innerType,
|
||||
get defaultValue() {
|
||||
return typeof defaultValue === "function" ? defaultValue() : defaultValue;
|
||||
return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -947,13 +967,6 @@ function _custom(Class, fn, _params) {
|
||||
});
|
||||
return schema;
|
||||
}
|
||||
// export function _refine<T>(
|
||||
// Class: util.SchemaClass<schemas.$ZodCustom>,
|
||||
// fn: (arg: NoInfer<T>) => util.MaybeAsync<unknown>,
|
||||
// _params: string | $ZodCustomParams = {}
|
||||
// ): checks.$ZodCheck<T> {
|
||||
// return _custom(Class, fn, _params);
|
||||
// }
|
||||
// same as _custom but defaults to abort:false
|
||||
function _refine(Class, fn, _params) {
|
||||
const schema = new Class({
|
||||
@@ -964,6 +977,58 @@ function _refine(Class, fn, _params) {
|
||||
});
|
||||
return schema;
|
||||
}
|
||||
function _superRefine(fn) {
|
||||
const ch = _check((payload) => {
|
||||
payload.addIssue = (issue) => {
|
||||
if (typeof issue === "string") {
|
||||
payload.issues.push(util.issue(issue, payload.value, ch._zod.def));
|
||||
}
|
||||
else {
|
||||
// for Zod 3 backwards compatibility
|
||||
const _issue = issue;
|
||||
if (_issue.fatal)
|
||||
_issue.continue = false;
|
||||
_issue.code ?? (_issue.code = "custom");
|
||||
_issue.input ?? (_issue.input = payload.value);
|
||||
_issue.inst ?? (_issue.inst = ch);
|
||||
_issue.continue ?? (_issue.continue = !ch._zod.def.abort); // abort is always undefined, so this is always true...
|
||||
payload.issues.push(util.issue(_issue));
|
||||
}
|
||||
};
|
||||
return fn(payload.value, payload);
|
||||
});
|
||||
return ch;
|
||||
}
|
||||
function _check(fn, params) {
|
||||
const ch = new checks.$ZodCheck({
|
||||
check: "custom",
|
||||
...util.normalizeParams(params),
|
||||
});
|
||||
ch._zod.check = fn;
|
||||
return ch;
|
||||
}
|
||||
function describe(description) {
|
||||
const ch = new checks.$ZodCheck({ check: "describe" });
|
||||
ch._zod.onattach = [
|
||||
(inst) => {
|
||||
const existing = registries.globalRegistry.get(inst) ?? {};
|
||||
registries.globalRegistry.add(inst, { ...existing, description });
|
||||
},
|
||||
];
|
||||
ch._zod.check = () => { }; // no-op check
|
||||
return ch;
|
||||
}
|
||||
function meta(metadata) {
|
||||
const ch = new checks.$ZodCheck({ check: "meta" });
|
||||
ch._zod.onattach = [
|
||||
(inst) => {
|
||||
const existing = registries.globalRegistry.get(inst) ?? {};
|
||||
registries.globalRegistry.add(inst, { ...existing, ...metadata });
|
||||
},
|
||||
];
|
||||
ch._zod.check = () => { }; // no-op check
|
||||
return ch;
|
||||
}
|
||||
function _stringbool(Classes, _params) {
|
||||
const params = util.normalizeParams(_params);
|
||||
let truthyArray = params.truthy ?? ["true", "1", "yes", "on", "y", "enabled"];
|
||||
@@ -974,13 +1039,16 @@ function _stringbool(Classes, _params) {
|
||||
}
|
||||
const truthySet = new Set(truthyArray);
|
||||
const falsySet = new Set(falsyArray);
|
||||
const _Pipe = Classes.Pipe ?? schemas.$ZodPipe;
|
||||
const _Codec = Classes.Codec ?? schemas.$ZodCodec;
|
||||
const _Boolean = Classes.Boolean ?? schemas.$ZodBoolean;
|
||||
const _String = Classes.String ?? schemas.$ZodString;
|
||||
const _Transform = Classes.Transform ?? schemas.$ZodTransform;
|
||||
const tx = new _Transform({
|
||||
type: "transform",
|
||||
transform: (input, payload) => {
|
||||
const stringSchema = new _String({ type: "string", error: params.error });
|
||||
const booleanSchema = new _Boolean({ type: "boolean", error: params.error });
|
||||
const codec = new _Codec({
|
||||
type: "pipe",
|
||||
in: stringSchema,
|
||||
out: booleanSchema,
|
||||
transform: ((input, payload) => {
|
||||
let data = input;
|
||||
if (params.case !== "sensitive")
|
||||
data = data.toLowerCase();
|
||||
@@ -996,30 +1064,23 @@ function _stringbool(Classes, _params) {
|
||||
expected: "stringbool",
|
||||
values: [...truthySet, ...falsySet],
|
||||
input: payload.value,
|
||||
inst: tx,
|
||||
inst: codec,
|
||||
continue: false,
|
||||
});
|
||||
return {};
|
||||
}
|
||||
},
|
||||
error: params.error,
|
||||
});
|
||||
// params.error;
|
||||
const innerPipe = new _Pipe({
|
||||
type: "pipe",
|
||||
in: new _String({ type: "string", error: params.error }),
|
||||
out: tx,
|
||||
error: params.error,
|
||||
});
|
||||
const outerPipe = new _Pipe({
|
||||
type: "pipe",
|
||||
in: innerPipe,
|
||||
out: new _Boolean({
|
||||
type: "boolean",
|
||||
error: params.error,
|
||||
}),
|
||||
reverseTransform: ((input, _payload) => {
|
||||
if (input === true) {
|
||||
return truthyArray[0] || "true";
|
||||
}
|
||||
else {
|
||||
return falsyArray[0] || "false";
|
||||
}
|
||||
}),
|
||||
error: params.error,
|
||||
});
|
||||
return outerPipe;
|
||||
return codec;
|
||||
}
|
||||
function _stringFormat(Class, format, fnOrRegex, _params = {}) {
|
||||
const params = util.normalizeParams(_params);
|
||||
|
||||
Reference in New Issue
Block a user