@freeword/meta
Version:
Meta package for Freeword: exports all core types, constants, and utilities from the src/ directory.
115 lines • 4.05 kB
JavaScript
import _ /**/ from 'lodash';
import * as ZImp from 'zod';
//
import { repairError, customErrorMap } from "./ZodReporting.js";
import { isChecker } from "./ZodTypeguards.js";
import * as _ZMP from "./ZodMonkeypunch.js";
import { decorate } from "../utils/BaseUtils.js";
export { ZodIssueCode, NEVER as FailedTransform, NEVER as FailedRefine, } from 'zod';
function report(subj, tmi = {}) {
const result = this.safeParse(subj);
if (result.success) {
const { data, success, ...rest } = result;
return decorate({ tmi, act: this.checkname ?? this.constructor.name, ...rest, ok: true, val: data }, { checker: this, success });
}
const err = result.error;
err._repair?.(subj, this, tmi);
Error.captureStackTrace?.(err, report);
return err;
}
function check(subj, _story, params) {
const result = this.safeParse(subj, params);
return result.success;
}
function cast(subj, story, _params) {
const result = this.report(subj, story);
if (result.ok) {
return result.val;
}
Error.captureStackTrace?.(result, cast);
throw result;
}
export function demand(zod, obj, opts = {}) {
try {
return zod.parse(obj, opts);
}
catch (zerr) {
console.error(zerr, obj);
Error.captureStackTrace?.(zerr, demand);
throw zerr;
}
}
export function ensureDescribed(zcheck, descr) {
if (!zcheck.describe) {
return zcheck;
}
const existing = zcheck._def?.description;
if (existing) {
return zcheck.describe(`${descr} (${existing})`);
}
return zcheck?.describe(descr);
}
export function zShape(checker, depth = 5) {
if (depth <= 0) {
return checker;
}
const shapebag = { ...(_.isFunction(checker?._def?.shape) ? checker._def.shape() : checker._def) };
// console.warn('shapebag', shapebag)
if (shapebag.type) {
return { ...shapebag, type: zShape(shapebag.type, depth - 1) };
}
if (_.isObject(shapebag)) {
return _.mapValues(shapebag, (subcheck, _nn) => (isChecker(subcheck) ? zShape(subcheck, depth - 1) : subcheck));
}
return shapebag;
}
function mergeObjectSyncNoUndef(status, pairs) {
const finalObject = {};
for (const pair of pairs) {
const { key, value } = pair;
if (key.status === "aborted")
return ZImp.INVALID;
if (value.status === "aborted")
return ZImp.INVALID;
if (key.status === "dirty")
status.dirty();
if (value.status === "dirty")
status.dirty();
if (key.value !== "__proto__" &&
(typeof value.value !== "undefined")
// (typeof value.value !== "undefined" || pair.alwaysSet) // don't set undefined values
) {
finalObject[key.value] = value.value;
}
}
return { status: status.value, value: finalObject };
}
ZImp.ParseStatus.mergeObjectSync = mergeObjectSyncNoUndef;
function _repairError(subj, checker, story = {}) {
return repairError(this, subj, checker, story);
}
export function checknameFor(checker) {
return checker._def?.description || String(checker._def?.typeName || checker.constructor.name).replace(/^Zod/, '');
}
function monkeypatchZod() {
const monkey = ZImp;
/* eslint-disable no-param-reassign */
// monkey.setErrorMap(customErrorMap as any)
//
const ZcheckerClass = monkey.ZodSchema.prototype;
ZcheckerClass.check = check;
ZcheckerClass.report = report;
ZcheckerClass.cast = cast;
Object.defineProperties(monkey.ZodSchema.prototype, {
checkname: { get() { return checknameFor(this); }, configurable: true },
hasDescription: { get() { return Boolean(this._def?.description); }, configurable: true },
});
ZImp.ZodError.prototype._repair = _repairError;
ZImp.setErrorMap(customErrorMap);
//
// ZImp.setErrorMap(customErrorMap as any)
return ZImp.z;
} /* eslint-enable no-param-reassign */
export const Z = monkeypatchZod();
export const ZLib = ZImp;
//# sourceMappingURL=ZodInternal.js.map