484 lines
13 KiB
JavaScript
484 lines
13 KiB
JavaScript
// Metro module 12141 (f12142).
|
|
import { globalRegistry } from "./$ZodRegistry";
|
|
let process, extractDefs, finalize, isTransforming;
|
|
const initializeContext = function initializeContext(a0) {
|
|
let target = a0?.target;
|
|
if (target == null) {
|
|
target = "draft-2020-12";
|
|
}
|
|
if (target === "draft-4") {
|
|
target = "draft-04";
|
|
}
|
|
if (target === "draft-7") {
|
|
target = "draft-07";
|
|
}
|
|
let processors3 = a0.processors;
|
|
if (processors3 == null) {
|
|
processors3 = {};
|
|
}
|
|
let metadataRegistry = a0?.metadata;
|
|
if (metadataRegistry == null) {
|
|
metadataRegistry = globalRegistry;
|
|
}
|
|
let unrepresentable = a0?.unrepresentable;
|
|
if (unrepresentable == null) {
|
|
unrepresentable = "throw";
|
|
}
|
|
let override = a0?.override;
|
|
if (override == null) {
|
|
override = () => {};
|
|
}
|
|
let io2 = a0?.io;
|
|
if (io2 == null) {
|
|
io2 = "output";
|
|
}
|
|
const r12 = {
|
|
processors: processors3,
|
|
metadataRegistry,
|
|
target,
|
|
unrepresentable,
|
|
override,
|
|
io: io2,
|
|
counter: 0,
|
|
seen: new Map(),
|
|
cycles: null,
|
|
reused: null,
|
|
external: null
|
|
};
|
|
let cycles = a0?.cycles;
|
|
if (cycles == null) {
|
|
cycles = "ref";
|
|
}
|
|
r12.cycles = cycles;
|
|
let reused = a0?.reused;
|
|
if (reused == null) {
|
|
reused = "inline";
|
|
}
|
|
r12.reused = reused;
|
|
let external = a0?.external;
|
|
if (external == null) {
|
|
external = void 0;
|
|
}
|
|
r12.external = external;
|
|
return r12;
|
|
};
|
|
process = function process(a0, a1, r5 = {
|
|
path: [],
|
|
schemaPath: []
|
|
}) {
|
|
const def = a0._zod.def;
|
|
const r27 = a1.seen.get(a0);
|
|
if (r27) {
|
|
r27.count = r27.count + 1;
|
|
if (r5.schemaPath.includes(a0)) {
|
|
r27.cycle = r5.path;
|
|
}
|
|
return r27.schema;
|
|
}
|
|
const r28 = {
|
|
schema: {},
|
|
count: 1,
|
|
cycle: void 0,
|
|
path: r5.path
|
|
};
|
|
a1.seen.set(a0, r28);
|
|
const _zod = a0._zod;
|
|
const toJSONSchema = _zod.toJSONSchema;
|
|
const schema3 = toJSONSchema == null ? void 0 : toJSONSchema.call(_zod);
|
|
if (schema3) {
|
|
r28.schema = schema3;
|
|
} else {
|
|
const r12 = {
|
|
...r5,
|
|
schemaPath: [...r5.schemaPath, a0],
|
|
path: r5.path
|
|
};
|
|
if (a0._zod.processJSONSchema) {
|
|
a0._zod.processJSONSchema(a1, r28.schema, r12);
|
|
} else {
|
|
const schema = r28.schema;
|
|
const r43 = a1.processors[def.type];
|
|
if (!r43) {
|
|
throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
|
|
}
|
|
r43(a0, a1, schema, r12);
|
|
}
|
|
const parent = a0._zod.parent;
|
|
if (parent) {
|
|
if (!r28.ref) {
|
|
r28.ref = parent;
|
|
}
|
|
process(parent, a1, r12);
|
|
a1.seen.get(parent).isParent = true;
|
|
}
|
|
}
|
|
const r72 = a1.metadataRegistry.get(a0);
|
|
if (r72) {
|
|
Object.assign(r28.schema, r72);
|
|
}
|
|
if (a1.io === "input" && isTransforming(a0)) {
|
|
delete r28.schema["examples"];
|
|
delete r28.schema["default"];
|
|
}
|
|
if (a1.io === "input" && r28.schema._prefault) {
|
|
const schema2 = r28.schema;
|
|
if (schema2.default == null) {
|
|
schema2.default = r28.schema._prefault;
|
|
}
|
|
}
|
|
delete r28.schema["_prefault"];
|
|
return a1.seen.get(a0).schema;
|
|
};
|
|
extractDefs = function extractDefs(a0, a1) {
|
|
let c19614_0 = a0;
|
|
const r33 = a0.seen.get(a1);
|
|
let c19614_1 = r33;
|
|
if (!r33) {
|
|
throw new Error("Unprocessed schema. This is a bug in Zod.");
|
|
}
|
|
const map = new Map();
|
|
for (const item of a0.seen.entries()) {
|
|
const id = a0.metadataRegistry.get(item[0])?.id;
|
|
if (id) {
|
|
const r58 = map.get(id);
|
|
if (r58 && r58 !== item[0]) {
|
|
throw new Error(`Duplicate schema id "${id}" detected during JSON Schema conversion. Two different schemas cannot share the same id when converted together.`);
|
|
}
|
|
map.set(id, item[0]);
|
|
}
|
|
}
|
|
const extractToDef = (a0) => {
|
|
let r41;
|
|
if (!a0[1].schema.$ref) {
|
|
const r25 = a0[1];
|
|
const r16 = c19614_0.target === "draft-2020-12" ? "$defs" : "definitions";
|
|
if (c19614_0.external) {
|
|
const id2 = c19614_0.external.registry.get(a0[0])?.id;
|
|
let uri = c19614_0.external.uri;
|
|
if (uri == null) {
|
|
uri = (a0) => a0;
|
|
}
|
|
if (id2) {
|
|
r41 = {
|
|
ref: uri(id2)
|
|
};
|
|
} else {
|
|
let defId = a0[1].defId;
|
|
if (defId == null) {
|
|
defId = a0[1].schema.id;
|
|
}
|
|
if (defId == null) {
|
|
const r11 = +c19614_0.counter;
|
|
c19614_0.counter = r11 + 1;
|
|
defId = "schema" + r11;
|
|
}
|
|
a0[1].defId = defId;
|
|
r41 = {
|
|
defId,
|
|
ref: `${uri("__shared")}#/${r16}/${defId}`
|
|
};
|
|
}
|
|
} else if (a0[1] === c19614_1) {
|
|
r41 = {
|
|
ref: "#"
|
|
};
|
|
} else {
|
|
const r34 = `#/${r16}/`;
|
|
let id = a0[1].schema.id;
|
|
if (id == null) {
|
|
const r10 = +c19614_0.counter;
|
|
c19614_0.counter = r10 + 1;
|
|
id = "__schema" + r10;
|
|
}
|
|
r41 = {
|
|
defId: id,
|
|
ref: r34 + id
|
|
};
|
|
}
|
|
const { ref, defId: defId2 } = r41;
|
|
r25.def = {
|
|
...r25.schema
|
|
};
|
|
if (defId2) {
|
|
r25.defId = defId2;
|
|
}
|
|
const schema = r25.schema;
|
|
for (const key in schema) {
|
|
delete schema[key];
|
|
}
|
|
schema.$ref = ref;
|
|
}
|
|
};
|
|
if (a0.cycles === "throw") {
|
|
for (const item2 of a0.seen.entries()) {
|
|
const r92 = item2[1];
|
|
if (r92.cycle) {
|
|
const r95 = Error;
|
|
const cycle = r92.cycle;
|
|
throw new r95(`Cycle detected: #/${cycle == null ? void 0 : cycle.join("/")}/<root>
|
|
|
|
Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);
|
|
}
|
|
}
|
|
}
|
|
for (const item3 of a0.seen.entries()) {
|
|
const r124 = item3[1];
|
|
if (a1 !== item3[0]) {
|
|
if (a0.external) {
|
|
const id2 = a0.external.registry.get(item3[0])?.id;
|
|
if (a1 !== item3[0] && id2) {
|
|
extractToDef(item3);
|
|
continue;
|
|
}
|
|
}
|
|
if (a0.metadataRegistry.get(item3[0])?.id || r124.cycle || r124.count > 1 && a0.reused === "ref") {
|
|
extractToDef(item3);
|
|
}
|
|
} else {
|
|
extractToDef(item3);
|
|
}
|
|
}
|
|
};
|
|
finalize = function finalize(a0, a1) {
|
|
let r114;
|
|
let c19615_0 = a0;
|
|
const r12 = a0.seen.get(a1);
|
|
if (!r12) {
|
|
throw new Error("Unprocessed schema. This is a bug in Zod.");
|
|
}
|
|
const flattenRef2 = (a0) => {
|
|
let r16, schema;
|
|
const r8 = c19615_0.seen.get(a0);
|
|
if (r8.ref !== null) {
|
|
let def = r8.def;
|
|
if (def == null) {
|
|
def = r8.schema;
|
|
}
|
|
const r18 = {
|
|
...def
|
|
};
|
|
const ref = r8.ref;
|
|
r8.ref = null;
|
|
if (ref) {
|
|
L12: {
|
|
L11: {
|
|
flattenRef2(ref);
|
|
r16 = c19615_0.seen.get(ref);
|
|
schema = r16.schema;
|
|
if (schema.$ref) {
|
|
if (c19615_0.target !== "draft-07" && c19615_0.target !== "draft-04" && c19615_0.target !== "openapi-3.0") {
|
|
break L11;
|
|
}
|
|
let allOf = def.allOf;
|
|
if (allOf == null) {
|
|
allOf = [];
|
|
}
|
|
def.allOf = allOf;
|
|
def.allOf.push(schema);
|
|
break L12;
|
|
}
|
|
}
|
|
Object.assign(def, schema);
|
|
}
|
|
Object.assign(def, r18);
|
|
if (a0._zod.parent === ref) {
|
|
for (const key in def) {
|
|
if (key !== "$ref" && key !== "allOf" && !(key in r18)) {
|
|
delete def[key];
|
|
}
|
|
}
|
|
}
|
|
if (schema.$ref && r16.def) {
|
|
for (const key2 in def) {
|
|
if (key2 !== "$ref" && key2 !== "allOf" && key2 in r16.def && JSON.stringify(def[key2]) === JSON.stringify(r16.def[key2])) {
|
|
delete def[key2];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
const parent = a0._zod.parent;
|
|
if (parent && parent !== ref) {
|
|
flattenRef2(parent);
|
|
const r85 = c19615_0.seen.get(parent);
|
|
if (r85 == null ? void 0 : r85.schema.$ref) {
|
|
def.$ref = r85.schema.$ref;
|
|
if (r85.def) {
|
|
for (const key3 in def) {
|
|
if (key3 !== "$ref" && key3 !== "allOf" && key3 in r85.def && JSON.stringify(def[key3]) === JSON.stringify(r85.def[key3])) {
|
|
delete def[key3];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let path = r8.path;
|
|
if (path == null) {
|
|
path = [];
|
|
}
|
|
c19615_0.override({
|
|
zodSchema: a0,
|
|
jsonSchema: def,
|
|
path
|
|
});
|
|
}
|
|
};
|
|
for (const item of [...a0.seen.entries()].reverse()) {
|
|
flattenRef2(item[0]);
|
|
}
|
|
const r48 = {};
|
|
if (a0.target === "draft-2020-12") {
|
|
r48.$schema = "https://json-schema.org/draft/2020-12/schema";
|
|
} else if (a0.target === "draft-07") {
|
|
r48.$schema = "http://json-schema.org/draft-07/schema#";
|
|
} else if (a0.target === "draft-04") {
|
|
r48.$schema = "http://json-schema.org/draft-04/schema#";
|
|
} else {
|
|
a0.target;
|
|
}
|
|
if (a0.external?.uri) {
|
|
const id = a0.external.registry.get(a1)?.id;
|
|
if (!id) {
|
|
throw new Error("Schema is missing an `id` property");
|
|
}
|
|
r48.$id = a0.external.uri(id);
|
|
}
|
|
const r78 = Object;
|
|
let def = r12.def;
|
|
if (def == null) {
|
|
def = r12.schema;
|
|
}
|
|
r78.assign(r48, def);
|
|
let definitions = a0.external?.defs;
|
|
if (definitions == null) {
|
|
definitions = {};
|
|
}
|
|
for (const item2 of a0.seen.entries()) {
|
|
const r94 = item2[1];
|
|
if (r94.def && r94.defId) {
|
|
definitions[r94.defId] = r94.def;
|
|
}
|
|
}
|
|
if (!a0.external && Object.keys(definitions).length > 0) {
|
|
if (a0.target === "draft-2020-12") {
|
|
r48.$defs = definitions;
|
|
} else {
|
|
r48.definitions = definitions;
|
|
}
|
|
}
|
|
try {
|
|
r114 = JSON.parse(JSON.stringify(r48));
|
|
Object.defineProperty(r114, "~standard", {
|
|
value: {
|
|
...a1["~standard"],
|
|
jsonSchema: {
|
|
input: (0, exports.createStandardJSONSchemaMethod)(a1, "input", a0.processors),
|
|
output: (0, exports.createStandardJSONSchemaMethod)(a1, "output", a0.processors)
|
|
}
|
|
},
|
|
enumerable: false,
|
|
writable: false
|
|
});
|
|
} catch ($e) {
|
|
throw new Error("Error converting schema to JSON.");
|
|
}
|
|
return r114;
|
|
};
|
|
isTransforming = function isTransforming(a0, a1) {
|
|
const r3 = a1 == null ? {
|
|
seen: new Set()
|
|
} : a1;
|
|
if (r3.seen.has(a0)) {
|
|
return false;
|
|
}
|
|
r3.seen.add(a0);
|
|
const def = a0._zod.def;
|
|
if (def.type === "transform") {
|
|
return true;
|
|
}
|
|
if (def.type === "array") {
|
|
return isTransforming(def.element, r3);
|
|
}
|
|
if (def.type === "set") {
|
|
return isTransforming(def.valueType, r3);
|
|
}
|
|
if (def.type === "lazy") {
|
|
return isTransforming(def.getter(), r3);
|
|
}
|
|
if (def.type !== "promise" && def.type !== "optional" && def.type !== "nonoptional" && def.type !== "nullable" && def.type !== "readonly" && def.type !== "default" && def.type !== "prefault") {
|
|
if (def.type === "intersection") {
|
|
return isTransforming(def.left, r3) || isTransforming(def.right, r3);
|
|
}
|
|
if (def.type !== "record" && def.type !== "map") {
|
|
if (def.type === "pipe") {
|
|
return isTransforming(def.in, r3) || isTransforming(def.out, r3);
|
|
}
|
|
if (def.type === "object") {
|
|
const shape = def.shape;
|
|
for (const key in shape) {
|
|
if (isTransforming(def.shape[key], r3)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
if (def.type === "union") {
|
|
for (const option of def.options) {
|
|
if (!isTransforming(option, r3)) {
|
|
continue;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
if (def.type !== "tuple") {
|
|
return false;
|
|
}
|
|
for (const item of def.items) {
|
|
if (!isTransforming(item, r3)) {
|
|
continue;
|
|
}
|
|
return true;
|
|
}
|
|
const rest = def.rest;
|
|
return !(rest ? !isTransforming(def.rest, r3) : !rest);
|
|
}
|
|
return isTransforming(def.keyType, r3) || isTransforming(def.valueType, r3);
|
|
}
|
|
return isTransforming(def.innerType, r3);
|
|
};
|
|
export { initializeContext };
|
|
export { process };
|
|
export { extractDefs };
|
|
export { finalize };
|
|
export const createToJSONSchemaMethod = function(c19616_0, r3 = {}) {
|
|
const processors = r3;
|
|
return (a0) => {
|
|
const r10 = initializeContext({
|
|
...a0,
|
|
processors
|
|
});
|
|
process(c19616_0, r10);
|
|
extractDefs(r10, c19616_0);
|
|
return finalize(r10, c19616_0);
|
|
};
|
|
};
|
|
export const createStandardJSONSchemaMethod = function(c19617_0, io, r4 = {}) {
|
|
const processors2 = r4;
|
|
return (a0) => {
|
|
const r6 = a0 == null ? {} : a0;
|
|
let libraryOptions = r6.libraryOptions;
|
|
if (libraryOptions == null) {
|
|
libraryOptions = {};
|
|
}
|
|
const r15 = initializeContext({
|
|
...libraryOptions,
|
|
target: r6.target,
|
|
io,
|
|
processors: processors2
|
|
});
|
|
process(c19617_0, r15);
|
|
extractDefs(r15, c19617_0);
|
|
return finalize(r15, c19617_0);
|
|
};
|
|
};
|