avancement planning

This commit is contained in:
2026-05-26 11:58:39 +02:00
parent 619a2b240a
commit 150b97cd2e
4892 changed files with 99214 additions and 429382 deletions
+86 -31
View File
@@ -1,4 +1,5 @@
import * as checks from "./checks.js";
import * as registries from "./registries.js";
import * as schemas from "./schemas.js";
import * as util from "./util.js";
export function _string(Class, params) {
@@ -161,6 +162,15 @@ export function _ipv6(Class, params) {
...util.normalizeParams(params),
});
}
export function _mac(Class, params) {
return new Class({
type: "string",
format: "mac",
check: "string_format",
abort: false,
...util.normalizeParams(params),
});
}
export function _cidrv4(Class, params) {
return new Class({
type: "string",
@@ -607,6 +617,10 @@ export function _toLowerCase() {
export function _toUpperCase() {
return _overwrite((input) => input.toUpperCase());
}
// slugify
export function _slugify() {
return _overwrite((input) => util.slugify(input));
}
export function _array(Class, element, params) {
return new Class({
type: "array",
@@ -747,7 +761,7 @@ export function _default(Class, innerType, defaultValue) {
type: "default",
innerType,
get defaultValue() {
return typeof defaultValue === "function" ? defaultValue() : defaultValue;
return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
},
});
}
@@ -814,13 +828,6 @@ export 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
export function _refine(Class, fn, _params) {
const schema = new Class({
@@ -831,6 +838,58 @@ export function _refine(Class, fn, _params) {
});
return schema;
}
export 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;
}
export function _check(fn, params) {
const ch = new checks.$ZodCheck({
check: "custom",
...util.normalizeParams(params),
});
ch._zod.check = fn;
return ch;
}
export 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;
}
export 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;
}
export function _stringbool(Classes, _params) {
const params = util.normalizeParams(_params);
let truthyArray = params.truthy ?? ["true", "1", "yes", "on", "y", "enabled"];
@@ -841,13 +900,16 @@ export 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();
@@ -863,30 +925,23 @@ export 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;
}
export function _stringFormat(Class, format, fnOrRegex, _params = {}) {
const params = util.normalizeParams(_params);