avancement planning
This commit is contained in:
+93
-47
@@ -6,18 +6,16 @@ import * as parse from "./parse.js";
|
||||
export const ZodType = /*@__PURE__*/ core.$constructor("ZodType", (inst, def) => {
|
||||
core.$ZodType.init(inst, def);
|
||||
inst.def = def;
|
||||
inst.type = def.type;
|
||||
Object.defineProperty(inst, "_def", { value: def });
|
||||
// base methods
|
||||
inst.check = (...checks) => {
|
||||
return inst.clone({
|
||||
...def,
|
||||
return inst.clone(util.mergeDefs(def, {
|
||||
checks: [
|
||||
...(def.checks ?? []),
|
||||
...checks.map((ch) => typeof ch === "function" ? { _zod: { check: ch, def: { check: "custom" }, onattach: [] } } : ch),
|
||||
],
|
||||
}
|
||||
// { parent: true }
|
||||
);
|
||||
}));
|
||||
};
|
||||
inst.clone = (def, params) => core.clone(inst, def, params);
|
||||
inst.brand = () => inst;
|
||||
@@ -31,6 +29,15 @@ export const ZodType = /*@__PURE__*/ core.$constructor("ZodType", (inst, def) =>
|
||||
inst.parseAsync = async (data, params) => parse.parseAsync(inst, data, params, { callee: inst.parseAsync });
|
||||
inst.safeParseAsync = async (data, params) => parse.safeParseAsync(inst, data, params);
|
||||
inst.spa = inst.safeParseAsync;
|
||||
// encoding/decoding
|
||||
inst.encode = (data, params) => parse.encode(inst, data, params);
|
||||
inst.decode = (data, params) => parse.decode(inst, data, params);
|
||||
inst.encodeAsync = async (data, params) => parse.encodeAsync(inst, data, params);
|
||||
inst.decodeAsync = async (data, params) => parse.decodeAsync(inst, data, params);
|
||||
inst.safeEncode = (data, params) => parse.safeEncode(inst, data, params);
|
||||
inst.safeDecode = (data, params) => parse.safeDecode(inst, data, params);
|
||||
inst.safeEncodeAsync = async (data, params) => parse.safeEncodeAsync(inst, data, params);
|
||||
inst.safeDecodeAsync = async (data, params) => parse.safeDecodeAsync(inst, data, params);
|
||||
// refinements
|
||||
inst.refine = (check, params) => inst.check(refine(check, params));
|
||||
inst.superRefine = (refinement) => inst.check(superRefine(refinement));
|
||||
@@ -99,6 +106,7 @@ export const _ZodString = /*@__PURE__*/ core.$constructor("_ZodString", (inst, d
|
||||
inst.normalize = (...args) => inst.check(checks.normalize(...args));
|
||||
inst.toLowerCase = () => inst.check(checks.toLowerCase());
|
||||
inst.toUpperCase = () => inst.check(checks.toUpperCase());
|
||||
inst.slugify = () => inst.check(checks.slugify());
|
||||
});
|
||||
export const ZodString = /*@__PURE__*/ core.$constructor("ZodString", (inst, def) => {
|
||||
core.$ZodString.init(inst, def);
|
||||
@@ -182,6 +190,13 @@ export const ZodURL = /*@__PURE__*/ core.$constructor("ZodURL", (inst, def) => {
|
||||
export function url(params) {
|
||||
return core._url(ZodURL, params);
|
||||
}
|
||||
export function httpUrl(params) {
|
||||
return core._url(ZodURL, {
|
||||
protocol: /^https?$/,
|
||||
hostname: core.regexes.domain,
|
||||
...util.normalizeParams(params),
|
||||
});
|
||||
}
|
||||
export const ZodEmoji = /*@__PURE__*/ core.$constructor("ZodEmoji", (inst, def) => {
|
||||
// ZodStringFormat.init(inst, def);
|
||||
core.$ZodEmoji.init(inst, def);
|
||||
@@ -246,6 +261,14 @@ export const ZodIPv4 = /*@__PURE__*/ core.$constructor("ZodIPv4", (inst, def) =>
|
||||
export function ipv4(params) {
|
||||
return core._ipv4(ZodIPv4, params);
|
||||
}
|
||||
export const ZodMAC = /*@__PURE__*/ core.$constructor("ZodMAC", (inst, def) => {
|
||||
// ZodStringFormat.init(inst, def);
|
||||
core.$ZodMAC.init(inst, def);
|
||||
ZodStringFormat.init(inst, def);
|
||||
});
|
||||
export function mac(params) {
|
||||
return core._mac(ZodMAC, params);
|
||||
}
|
||||
export const ZodIPv6 = /*@__PURE__*/ core.$constructor("ZodIPv6", (inst, def) => {
|
||||
// ZodStringFormat.init(inst, def);
|
||||
core.$ZodIPv6.init(inst, def);
|
||||
@@ -308,6 +331,20 @@ export const ZodCustomStringFormat = /*@__PURE__*/ core.$constructor("ZodCustomS
|
||||
export function stringFormat(format, fnOrRegex, _params = {}) {
|
||||
return core._stringFormat(ZodCustomStringFormat, format, fnOrRegex, _params);
|
||||
}
|
||||
export function hostname(_params) {
|
||||
return core._stringFormat(ZodCustomStringFormat, "hostname", core.regexes.hostname, _params);
|
||||
}
|
||||
export function hex(_params) {
|
||||
return core._stringFormat(ZodCustomStringFormat, "hex", core.regexes.hex, _params);
|
||||
}
|
||||
export function hash(alg, params) {
|
||||
const enc = params?.enc ?? "hex";
|
||||
const format = `${alg}_${enc}`;
|
||||
const regex = core.regexes[format];
|
||||
if (!regex)
|
||||
throw new Error(`Unrecognized hash format: ${format}`);
|
||||
return core._stringFormat(ZodCustomStringFormat, format, regex, params);
|
||||
}
|
||||
export const ZodNumber = /*@__PURE__*/ core.$constructor("ZodNumber", (inst, def) => {
|
||||
core.$ZodNumber.init(inst, def);
|
||||
ZodType.init(inst, def);
|
||||
@@ -481,22 +518,26 @@ export function array(element, params) {
|
||||
// .keyof
|
||||
export function keyof(schema) {
|
||||
const shape = schema._zod.def.shape;
|
||||
return literal(Object.keys(shape));
|
||||
return _enum(Object.keys(shape));
|
||||
}
|
||||
export const ZodObject = /*@__PURE__*/ core.$constructor("ZodObject", (inst, def) => {
|
||||
core.$ZodObject.init(inst, def);
|
||||
core.$ZodObjectJIT.init(inst, def);
|
||||
ZodType.init(inst, def);
|
||||
util.defineLazy(inst, "shape", () => def.shape);
|
||||
util.defineLazy(inst, "shape", () => {
|
||||
return def.shape;
|
||||
});
|
||||
inst.keyof = () => _enum(Object.keys(inst._zod.def.shape));
|
||||
inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall: catchall });
|
||||
inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
|
||||
// inst.nonstrict = () => inst.clone({ ...inst._zod.def, catchall: api.unknown() });
|
||||
inst.loose = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
|
||||
inst.strict = () => inst.clone({ ...inst._zod.def, catchall: never() });
|
||||
inst.strip = () => inst.clone({ ...inst._zod.def, catchall: undefined });
|
||||
inst.extend = (incoming) => {
|
||||
return util.extend(inst, incoming);
|
||||
};
|
||||
inst.safeExtend = (incoming) => {
|
||||
return util.safeExtend(inst, incoming);
|
||||
};
|
||||
inst.merge = (other) => util.merge(inst, other);
|
||||
inst.pick = (mask) => util.pick(inst, mask);
|
||||
inst.omit = (mask) => util.omit(inst, mask);
|
||||
@@ -506,10 +547,7 @@ export const ZodObject = /*@__PURE__*/ core.$constructor("ZodObject", (inst, def
|
||||
export function object(shape, params) {
|
||||
const def = {
|
||||
type: "object",
|
||||
get shape() {
|
||||
util.assignProp(this, "shape", { ...shape });
|
||||
return this.shape;
|
||||
},
|
||||
shape: shape ?? {},
|
||||
...util.normalizeParams(params),
|
||||
};
|
||||
return new ZodObject(def);
|
||||
@@ -518,10 +556,7 @@ export function object(shape, params) {
|
||||
export function strictObject(shape, params) {
|
||||
return new ZodObject({
|
||||
type: "object",
|
||||
get shape() {
|
||||
util.assignProp(this, "shape", { ...shape });
|
||||
return this.shape;
|
||||
},
|
||||
shape,
|
||||
catchall: never(),
|
||||
...util.normalizeParams(params),
|
||||
});
|
||||
@@ -530,10 +565,7 @@ export function strictObject(shape, params) {
|
||||
export function looseObject(shape, params) {
|
||||
return new ZodObject({
|
||||
type: "object",
|
||||
get shape() {
|
||||
util.assignProp(this, "shape", { ...shape });
|
||||
return this.shape;
|
||||
},
|
||||
shape,
|
||||
catchall: unknown(),
|
||||
...util.normalizeParams(params),
|
||||
});
|
||||
@@ -609,9 +641,11 @@ export function record(keyType, valueType, params) {
|
||||
}
|
||||
// type alksjf = core.output<core.$ZodRecordKey>;
|
||||
export function partialRecord(keyType, valueType, params) {
|
||||
const k = core.clone(keyType);
|
||||
k._zod.values = undefined;
|
||||
return new ZodRecord({
|
||||
type: "record",
|
||||
keyType: union([keyType, never()]),
|
||||
keyType: k,
|
||||
valueType: valueType,
|
||||
...util.normalizeParams(params),
|
||||
});
|
||||
@@ -741,6 +775,9 @@ export const ZodTransform = /*@__PURE__*/ core.$constructor("ZodTransform", (ins
|
||||
core.$ZodTransform.init(inst, def);
|
||||
ZodType.init(inst, def);
|
||||
inst._zod.parse = (payload, _ctx) => {
|
||||
if (_ctx.direction === "backward") {
|
||||
throw new core.$ZodEncodeError(inst.constructor.name);
|
||||
}
|
||||
payload.addIssue = (issue) => {
|
||||
if (typeof issue === "string") {
|
||||
payload.issues.push(util.issue(issue, payload.value, def));
|
||||
@@ -753,7 +790,7 @@ export const ZodTransform = /*@__PURE__*/ core.$constructor("ZodTransform", (ins
|
||||
_issue.code ?? (_issue.code = "custom");
|
||||
_issue.input ?? (_issue.input = payload.value);
|
||||
_issue.inst ?? (_issue.inst = inst);
|
||||
_issue.continue ?? (_issue.continue = true);
|
||||
// _issue.continue ??= true;
|
||||
payload.issues.push(util.issue(_issue));
|
||||
}
|
||||
};
|
||||
@@ -811,7 +848,7 @@ export function _default(innerType, defaultValue) {
|
||||
type: "default",
|
||||
innerType: innerType,
|
||||
get defaultValue() {
|
||||
return typeof defaultValue === "function" ? defaultValue() : defaultValue;
|
||||
return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -825,7 +862,7 @@ export function prefault(innerType, defaultValue) {
|
||||
type: "prefault",
|
||||
innerType: innerType,
|
||||
get defaultValue() {
|
||||
return typeof defaultValue === "function" ? defaultValue() : defaultValue;
|
||||
return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -887,9 +924,23 @@ export function pipe(in_, out) {
|
||||
// ...util.normalizeParams(params),
|
||||
});
|
||||
}
|
||||
export const ZodCodec = /*@__PURE__*/ core.$constructor("ZodCodec", (inst, def) => {
|
||||
ZodPipe.init(inst, def);
|
||||
core.$ZodCodec.init(inst, def);
|
||||
});
|
||||
export function codec(in_, out, params) {
|
||||
return new ZodCodec({
|
||||
type: "pipe",
|
||||
in: in_,
|
||||
out: out,
|
||||
transform: params.decode,
|
||||
reverseTransform: params.encode,
|
||||
});
|
||||
}
|
||||
export const ZodReadonly = /*@__PURE__*/ core.$constructor("ZodReadonly", (inst, def) => {
|
||||
core.$ZodReadonly.init(inst, def);
|
||||
ZodType.init(inst, def);
|
||||
inst.unwrap = () => inst._zod.def.innerType;
|
||||
});
|
||||
export function readonly(innerType) {
|
||||
return new ZodReadonly({
|
||||
@@ -930,6 +981,18 @@ export function promise(innerType) {
|
||||
innerType: innerType,
|
||||
});
|
||||
}
|
||||
export const ZodFunction = /*@__PURE__*/ core.$constructor("ZodFunction", (inst, def) => {
|
||||
core.$ZodFunction.init(inst, def);
|
||||
ZodType.init(inst, def);
|
||||
});
|
||||
export function _function(params) {
|
||||
return new ZodFunction({
|
||||
type: "function",
|
||||
input: Array.isArray(params?.input) ? tuple(params?.input) : (params?.input ?? array(unknown())),
|
||||
output: params?.output ?? unknown(),
|
||||
});
|
||||
}
|
||||
export { _function as function };
|
||||
export const ZodCustom = /*@__PURE__*/ core.$constructor("ZodCustom", (inst, def) => {
|
||||
core.$ZodCustom.init(inst, def);
|
||||
ZodType.init(inst, def);
|
||||
@@ -951,27 +1014,11 @@ export function refine(fn, _params = {}) {
|
||||
}
|
||||
// superRefine
|
||||
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);
|
||||
payload.issues.push(util.issue(_issue));
|
||||
}
|
||||
};
|
||||
return fn(payload.value, payload);
|
||||
});
|
||||
return ch;
|
||||
return core._superRefine(fn);
|
||||
}
|
||||
// Re-export describe and meta from core
|
||||
export const describe = core.describe;
|
||||
export const meta = core.meta;
|
||||
function _instanceof(cls, params = {
|
||||
error: `Input not instance of ${cls.name}`,
|
||||
}) {
|
||||
@@ -988,10 +1035,9 @@ function _instanceof(cls, params = {
|
||||
export { _instanceof as instanceof };
|
||||
// stringbool
|
||||
export const stringbool = (...args) => core._stringbool({
|
||||
Pipe: ZodPipe,
|
||||
Codec: ZodCodec,
|
||||
Boolean: ZodBoolean,
|
||||
String: ZodString,
|
||||
Transform: ZodTransform,
|
||||
}, ...args);
|
||||
export function json(params) {
|
||||
const jsonSchema = lazy(() => {
|
||||
|
||||
Reference in New Issue
Block a user