UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

66 lines (65 loc) 3.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodePointerSegment = decodePointerSegment; exports.extractZodSubSchema = extractZodSubSchema; exports.unwrap = unwrap; var _hasOwn = _interopRequireDefault(require("core-js-pure/stable/object/has-own.js")); var z = _interopRequireWildcard(require("zod")); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function extractZodSubSchema(root, pointer) { if (!pointer) return root; const normalized = pointer.startsWith('#') ? pointer.slice(1) : pointer; if (normalized === '/' || normalized === '') return root; const parts = normalized.split('/').slice(1).map(decodePointerSegment); let cur = root; for (const part of parts) { cur = unwrap(cur); if (cur instanceof z.ZodObject) { const shape = cur.shape; if (!(0, _hasOwn.default)(shape, part)) { throw new Error(`Key '${part}' not found in object shape`); } cur = shape[part]; continue; } if (cur instanceof z.ZodArray) { cur = cur.element; continue; } if (cur instanceof z.ZodTuple) { const idx = Number(part); const items = cur.def.items; if (!Array.isArray(items)) { throw new Error('Tuple items are unavailable for traversal'); } if (!Number.isInteger(idx) || idx < 0 || idx >= items.length) { throw new Error(`Tuple index out of range: ${part}`); } cur = items[idx]; continue; } if (cur instanceof z.ZodRecord) { cur = cur.valueType; continue; } if (cur instanceof z.ZodUnion || cur instanceof z.ZodDiscriminatedUnion) { throw new Error('Pointer into union is ambiguous. Choose a branch explicitly.'); } if (cur instanceof z.ZodIntersection) { throw new Error('Pointer into intersection is ambiguous. Decide how to traverse.'); } const typeName = cur.type || cur.constructor.name; throw new Error(`Unsupported traversal at '${part}' for schema type '${typeName}'`); } return cur; } function unwrap(t) { return t instanceof z.ZodOptional || t instanceof z.ZodNullable || t instanceof z.ZodDefault || t instanceof z.ZodCatch || t instanceof z.ZodNonOptional || t instanceof z.ZodPrefault || t instanceof z.ZodSuccess || t instanceof z.ZodReadonly || t instanceof z.ZodLazy || t instanceof z.ZodPromise ? unwrap(t.unwrap()) : t instanceof z.ZodPipe ? unwrap(t.out) : t; } function decodePointerSegment(seg) { return seg.replace(/~1/g, '/').replace(/~0/g, '~'); } //# sourceMappingURL=extractZodSubSchema.js.map