interface-forge
Version:
A TypeScript library for creating strongly typed mock data factories using Faker.js for test data generation
151 lines (150 loc) • 5.97 kB
JavaScript
import { E as e, O as t, T as n, f as r, i, j as a, k as o, m as s, n as c, p as l, r as u, t as d, w as f, x as p } from "./src-B0CxBMVJ.mjs";
import m from "ajv";
import h from "ajv-formats";
//#region src/json-schema.ts
function g(e, t) {
return a(e) && a(t) ? {
...e,
...t
} : t === void 0 || a(t) && Object.keys(t).length === 0 ? e : t;
}
var _ = /* @__PURE__ */ new WeakMap(), v = /* @__PURE__ */ new WeakMap(), y = /* @__PURE__ */ new WeakSet(), b = class {
constructor(t, n) {
e(this, y), s(this, _, void 0), s(this, v, void 0), l(_, this, t), l(v, this, n);
}
generate(e = r(v, this), t = 0) {
if (t >= (r(_, this).options?.maxDepth ?? 5)) return null;
if (e === !0) return {};
if (e === !1) throw new p("Cannot generate data for boolean false JSON Schema");
let i = n(y, this, A).call(this, e);
if ("const" in i) return i.const;
if (i.enum && i.enum.length > 0) return r(_, this).helpers.arrayElement(i.enum);
let a = i.oneOf?.[0] ?? i.anyOf?.[0];
if (a) return this.generate(a, t + 1);
switch (n(y, this, k).call(this, i)) {
case "array": return n(y, this, x).call(this, i, t);
case "boolean": return r(_, this).datatype.boolean();
case "integer": return n(y, this, S).call(this, i, !0);
case "null": return null;
case "number": return n(y, this, S).call(this, i, !1);
case "object": return n(y, this, C).call(this, i, t);
case "string": return n(y, this, w).call(this, i);
default: return {};
}
}
};
function x(e, t) {
let n = e.minItems ?? c.DEFAULT_ARRAY_MIN, i = e.maxItems ?? Math.max(n, c.DEFAULT_ARRAY_MAX), a = r(_, this).number.int({
max: i,
min: n
});
if (Array.isArray(e.items)) return e.items.map((e) => this.generate(e, t + 1)).slice(0, a);
let o = e.items ?? {};
return Array.from({ length: a }, () => this.generate(o, t + 1));
}
function S(e, t) {
let n = e.minimum ?? (t ? u.DEFAULT_INT_MIN : u.DEFAULT_MIN), i = e.maximum ?? (t ? u.DEFAULT_INT_MAX : u.DEFAULT_MAX);
if (typeof e.exclusiveMinimum == "number" ? n = t ? Math.floor(e.exclusiveMinimum) + 1 : e.exclusiveMinimum + u.PRECISION_OFFSET : e.exclusiveMinimum === !0 && (n = t ? n + 1 : n + u.PRECISION_OFFSET), typeof e.exclusiveMaximum == "number" ? i = t ? Math.ceil(e.exclusiveMaximum) - 1 : e.exclusiveMaximum - u.PRECISION_OFFSET : e.exclusiveMaximum === !0 && (i = t ? i - 1 : i - u.PRECISION_OFFSET), e.multipleOf) {
let t = Math.ceil(n / e.multipleOf), a = Math.floor(i / e.multipleOf);
return r(_, this).number.int({
max: a,
min: t
}) * e.multipleOf;
}
return t ? r(_, this).number.int({
max: Math.floor(i),
min: Math.ceil(n)
}) : r(_, this).number.float({
max: i,
min: n
});
}
function C(e, t) {
let n = {};
for (let [r, i] of Object.entries(e.properties ?? {})) n[r] = this.generate(i, t + 1);
return n;
}
function w(e) {
let t = e.minLength ?? 1, a = e.maxLength ?? Math.max(t, i.DEFAULT), o = n(y, this, T).call(this, e.format);
if (o && o.length >= t && (e.maxLength === void 0 || o.length <= e.maxLength)) return o;
if (e.pattern) {
let r = n(y, this, E).call(this, e.pattern, t, a);
if (r) return r;
}
return r(_, this).string.alpha({ length: {
max: a,
min: t
} });
}
function T(e) {
switch (e) {
case "date": return "2026-01-01";
case "date-time": return "2026-01-01T00:00:00Z";
case "email": return "user@example.com";
case "time": return "00:00:00Z";
case "uri": return "https://example.com";
case "uuid": return "00000000-0000-4000-8000-000000000000";
default: return;
}
}
function E(e, t, r) {
let i = new RegExp(e);
return [
n(y, this, O).call(this, e),
n(y, this, D).call(this, e),
"mock-value",
"aaaaa",
"0000"
].filter((e) => typeof e == "string").find((e) => e.length >= t && e.length <= r && i.test(e));
}
function D(e) {
let t = /^\^?([\w .-]*)\[0-9\]\{(\d+)\}\$?$/.exec(e);
if (t) return `${t[1]}${"0".repeat(Number(t[2]))}`;
}
function O(e) {
let t = /^\^\[([A-Za-z0-9-]+)\]\{(\d+)\}\$$/.exec(e);
if (!t) return;
let [, n, r] = t, i = Number(r);
return n.includes("0-9") ? "0".repeat(i) : n.includes("A-Z") ? "A".repeat(i) : "a".repeat(i);
}
function k(e) {
let t = Array.isArray(e.type) ? e.type.find((e) => e !== "null") : e.type;
if (t) return t;
if (e.properties) return "object";
if (e.items) return "array";
}
function A(e) {
if (!e.$ref) return e;
let t = /^#\/(\$defs|definitions)\/(.+)$/.exec(e.$ref);
if (!t || typeof r(v, this) == "boolean") throw new p(`Unsupported JSON Schema reference: ${e.$ref}`);
let n = (t[1] === "$defs" ? r(v, this).$defs : r(v, this).definitions)?.[decodeURIComponent(t[2])];
if (!n || typeof n == "boolean") throw new p(`Unable to resolve JSON Schema reference: ${e.$ref}`);
return n;
}
var j = /* @__PURE__ */ new WeakMap(), M = /* @__PURE__ */ new WeakMap(), N = /* @__PURE__ */ new WeakSet(), P = class extends d {
constructor(r, i, a) {
let c, u = t(i) ? i : (() => ({})), d = o(a) ? a : o(i) && !t(i) ? i : {};
super(((e, t, n) => {
let i = c.generate(r), a = u(e, t, n);
return a instanceof Promise ? a.then((e) => g(i, e)) : g(i, a);
}), d), e(this, N), s(this, j, void 0), s(this, M, void 0), l(j, this, r), c = new b(this, r);
let f = new m({
allErrors: !0,
strict: !1,
...d.ajv
});
h(f), l(M, this, f.compile(r));
let p = this.batch.bind(this), _ = this.batchAsync.bind(this), v = this.build.bind(this), y = this.buildAsync.bind(this);
this.batch = (e, t) => p(e, t).map((e) => n(N, this, F).call(this, e)), this.batchAsync = async (e, t) => (await _(e, t)).map((e) => n(N, this, F).call(this, e)), this.build = (e, t) => n(N, this, F).call(this, v(e, t)), this.buildAsync = async (e, t) => n(N, this, F).call(this, await y(e, t));
}
get schema() {
return r(j, this);
}
};
function F(e) {
if (!r(M, this).call(this, e)) throw new f(`Generated value does not match JSON Schema: ${JSON.stringify(r(M, this).errors)}`);
return e;
}
//#endregion
export { P as JsonSchemaFactory };
//# sourceMappingURL=json-schema.mjs.map