openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
190 lines (189 loc) • 7.34 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { a as mapJsonSchema, i as jsonSchemaValuesEqual, l as prepareJsonSchemaFormats, n as decodeLocalSchemaRefFragment, o as normalizeJsonSchemaForTypeBox } from "./json-schema-DKEhiksH.mjs";
import { t as createJsonSchemaValidator } from "./json-schema-validator-C3BucEbO.mjs";
import { n as findJsonSchemaShapeError, r as splitResourceRef } from "./json-schema-defaults-CjJJ5H5Y.mjs";
import { Meta } from "typebox/schema";
import fastUri from "fast-uri";
//#region src/shared/json-schema-resources.ts
function stripFragment(e) {
return splitResourceRef(e).resource;
}
/** Normalize an ID while preserving draft-07 fragment anchors. */
function resolveJsonSchemaResourceId(e, t) {
let n = e;
if (t) try {
n = fastUri.resolve(t, e);
} catch {}
return n.endsWith("#") ? n.slice(0, -1) : n;
}
/** Index only schema resources; IDs embedded in defaults, enums, or examples are data. */
function collectJsonSchemaResources(e, t = {}) {
const n = /* @__PURE__ */ new Map();
const r = /* @__PURE__ */ new WeakMap();
mapJsonSchema(e, (e, i) => {
const a = i.at(-1);
const o = a ? r.get(a) : void 0;
const s = typeof e.$id === "string" ? resolveJsonSchemaResourceId(e.$id, o) : void 0;
const c = s === void 0 ? o : stripFragment(s);
r.set(e, c);
const register = (r) => {
if (t.rejectDuplicateIds && n.has(r) && !(i.length === 0 && n.get(r) === e)) throw new Error(`JSON Schema reference resolves to more than one schema: ${r}`);
n.set(r, e);
};
if (s !== void 0) register(s);
for (const t of [e.$anchor, e.$dynamicAnchor]) if (typeof t === "string") register(`${c ?? ""}#${t}`);
return e;
});
return n;
}
const validationResourceBase = "https://schemas.openclaw.invalid/";
/** TypeBox requires a hierarchical base for relative resources; this URI is never fetched. */
function resolveJsonSchemaValidationId(e) {
return resolveJsonSchemaResourceId(e, validationResourceBase);
}
/** Keep source schemas intact while sharing one reference/format preparation policy. */
function prepareJsonSchemaWithResources(e, t) {
const n = normalizeJsonSchemaForTypeBox(prepareJsonSchemaFormats(e, t));
const r = mapJsonSchema(n, (e) => {
if (typeof e.pattern === "string") new RegExp(e.pattern, "u");
if (isRecord(e.patternProperties)) for (const t of Object.keys(e.patternProperties)) new RegExp(t, "u");
if (typeof e.$id === "string" && e.$id.startsWith("#")) {
const t = decodeLocalSchemaRefFragment(e.$id);
if (t !== void 0 && !t.startsWith("/")) {
const { $id: n, ...r } = e;
return t ? {
...r,
$anchor: t
} : r;
}
}
return e;
});
if (!isRecord(r)) return {
schema: r,
resources: /* @__PURE__ */ new Map()
};
const i = resolveJsonSchemaValidationId(isRecord(e) && typeof e.$id === "string" ? e.$id : "");
const { resource: a, fragment: o } = splitResourceRef(i);
const s = o ? decodeLocalSchemaRefFragment(o) : void 0;
const c = {
...r,
$id: a,
...s && !s.startsWith("/") ? { $anchor: s } : {}
};
const l = collectJsonSchemaResources(c);
l.set(i, c);
return {
schema: c,
resources: l
};
}
//#endregion
//#region src/shared/json-schema-validator.ts
const DRAFT_2020_12_SCHEMA = "https://json-schema.org/draft/2020-12/schema";
const DRAFT_7_SCHEMA = "http://json-schema.org/draft-07/schema#";
const draft7IgnoredKeywords = /* @__PURE__ */ new Set([
"$dynamicRef",
"$recursiveAnchor",
"$recursiveRef",
"$vocabulary",
"dependentRequired",
"dependentSchemas",
"enumIncludesNull",
"maxContains",
"minContains",
"prefixItems",
"unevaluatedItems",
"unevaluatedProperties"
]);
function createRegistry(e, t) {
const n = e ? Meta[DRAFT_2020_12_SCHEMA] : Meta[DRAFT_7_SCHEMA];
const r = collectJsonSchemaResources(n);
const { schema: i, resources: a } = prepareJsonSchemaWithResources(n, t);
if (!e) {
r.set("http://json-schema.org/schema", n);
a.set("http://json-schema.org/schema", i);
}
return {
source: r,
prepared: a,
byId: /* @__PURE__ */ new Map(),
bySchema: /* @__PURE__ */ new WeakMap(),
formats: t
};
}
function validatorContext(e) {
return Object.fromEntries([...e].flatMap(([e, t]) => e.includes("#") ? [[e, t]] : [[e, t], [`${e}#`, t]]));
}
function acquireValidator(e, t, n, r) {
const i = e.bySchema.get(t);
if (i) return i;
const a = typeof t.$id === "string" ? resolveJsonSchemaResourceId(t.$id, void 0) : void 0;
const o = a === void 0 ? void 0 : resolveJsonSchemaValidationId(a);
const s = o === void 0 ? void 0 : e.prepared.get(o);
if (a !== void 0 && s !== void 0 && r === "reject") throw new Error(`schema with key or id "${a}" already exists`);
if (!n && a !== void 0 && s !== void 0) {
let n = e.byId.get(a);
if (!n) {
n = createJsonSchemaValidator({ $ref: resolveJsonSchemaValidationId(a) }, { context: validatorContext(e.prepared) });
e.byId.set(a, n);
}
e.bySchema.set(t, n);
return n;
}
const c = n ? t : mapJsonSchema(t, (e, t) => {
const n = Object.fromEntries(Object.entries(e).filter(([e]) => !draft7IgnoredKeywords.has(e)));
if (typeof e.$id === "string" && typeof e.$ref === "string") {
let r;
for (const n of [...t, e]) if (typeof n.$id === "string") r = resolveJsonSchemaResourceId(n.$id, r);
const i = resolveJsonSchemaResourceId(e.$ref, r);
if (URL.canParse(i)) n.$ref = i;
}
return n;
});
const l = collectJsonSchemaResources(c, { rejectDuplicateIds: !n });
const u = findJsonSchemaShapeError(c, {
resources: new Map([...e.source, ...l]),
resolveId: resolveJsonSchemaResourceId
});
if (u) throw new Error(u);
for (const [t, r] of l) {
const i = e.source.get(t);
if (!n && e.byId.has(t) && i !== void 0 && !jsonSchemaValuesEqual(i, r)) throw new Error(`JSON Schema resource already registered: ${t}`);
}
const { schema: d, resources: f } = prepareJsonSchemaWithResources(c, e.formats);
const p = new Map([...e.prepared, ...f]);
const m = createJsonSchemaValidator(d, { context: validatorContext(p) });
for (const [t, n] of l) e.source.set(t, n);
e.prepared = p;
e.bySchema.set(t, m);
if (a !== void 0) e.byId.set(a, m);
return m;
}
/** Owns dialect preparation, reference registration, and cached codegen-free checks. */
function createJsonSchemaValidatorProvider(e) {
const t = e.dialect === "2020-12";
const n = createRegistry(t, e.formats);
const r = /* @__PURE__ */ new Map();
const i = t ? DRAFT_2020_12_SCHEMA : DRAFT_7_SCHEMA;
if (e.validateSchema) r.set(i, createJsonSchemaValidator({ $ref: i }, { context: validatorContext(n.prepared) }));
return { getValidator(a) {
if (e.validateSchema && typeof a !== "boolean" && !n.bySchema.has(a)) {
const e = a.$schema;
const t = typeof e === "string" ? e : i;
let o = r.get(t);
if (!o) {
if (!n.prepared.has(resolveJsonSchemaValidationId(t))) throw new Error(`no schema with key or ref "${t}"`);
o = createJsonSchemaValidator({ $ref: t }, { context: validatorContext(n.prepared) });
r.set(t, o);
}
if (!o.check(a)) {
const e = o.errors(a)[0];
throw new Error(`schema is invalid: data${e?.instancePath ?? ""} ${e?.message ?? "does not match its metaschema"}`);
}
}
return typeof a === "boolean" ? createJsonSchemaValidator(a) : acquireValidator(n, a, t, e.duplicateIds ?? "reuse");
} };
}
//#endregion
export { createJsonSchemaValidatorProvider as t };