zod-schema-faker
Version:
Generates mock data from zod schema. Powered by @faker-js/faker and randexp.js
1,102 lines (1,101 loc) • 37.6 kB
JavaScript
import { z as n, ZodString as U } from "zod/v3";
import { fakerEN as K, SimpleFaker as E } from "@faker-js/faker";
import j from "randexp";
class S extends Error {
}
const x = /* @__PURE__ */ new Map(), w = /* @__PURE__ */ new Map(), l = (r) => {
const e = r._def.typeName, s = w.get(r) ?? x.get(e);
if (s === void 0)
throw new S(`Unsupported schema type: ${e}.
- If this is a custom schema, you may not have installed a corresponding faker. If you need help with installation, please refer to the documentation for more information.
- If this is a built-in schema, it may not be implemented yet. Please file an issue to let us know: https://github.com/soc221b/zod-schema-faker/issues`);
return new s(r).fake();
};
let _ = K;
function g(r) {
_ = r;
}
function o() {
return _;
}
const T = new E(), b = (r, e) => {
const s = new j(r, e);
return s.randInt = (t, a) => T.number.int({ min: t, max: a }), s.gen();
}, Ue = (r) => {
_.seed(r), T.seed(r);
}, Ke = g, Ee = (r) => {
const e = r(_);
if (e instanceof Promise)
throw new SyntaxError("InternalError: runFake cannot be used with async functions");
return e;
};
function L(r) {
if (typeof r != "object" || r === null || !("_parse" in r) || typeof r._parse != "function")
throw new S(`Expected a zod schema, but got ${r}`);
}
function O(r, e) {
return r * e / N(r, e);
}
function N(r, e) {
return e === 0n || e === 0 ? r : N(e, r % e);
}
class f {
constructor(e) {
this.schema = e, L(e);
}
}
const D = (() => {
const r = [n.string(), n.number(), n.boolean(), n.null()], e = r.map((t) => n.array(t)), s = r.map(
(t) => n.object({
foo: t
})
);
return [...r, ...e, ...s];
})();
class A extends f {
fake() {
const e = o().helpers.arrayElement(D);
return l(e);
}
}
class R extends f {
fake() {
if (this.schema._def.exactLength !== null) {
if (this.schema._def.minLength !== null && this.schema._def.minLength.value !== this.schema._def.exactLength.value)
throw new RangeError();
if (this.schema._def.maxLength !== null && this.schema._def.maxLength.value !== this.schema._def.exactLength.value)
throw new RangeError();
}
const e = this.schema._def.exactLength?.value ?? this.schema._def.minLength?.value ?? 0, s = this.schema._def.exactLength?.value ?? this.schema._def.maxLength?.value ?? o().number.int({ min: e, max: e + 2 });
if (e > s)
throw new RangeError();
return o().helpers.multiple(() => l(this.schema._def.type), { count: { min: e, max: s } });
}
}
class z extends f {
fake() {
let e, s, t = 1n;
for (const c of this.schema._def.checks)
switch (c.kind) {
case "min": {
const i = c.value + (c.inclusive ? 0n : 1n);
e = e !== void 0 && e > i ? e : i;
break;
}
case "max": {
const i = c.value - (c.inclusive ? 0n : 1n);
s = s !== void 0 && s < i ? s : i;
break;
}
case "multipleOf": {
const i = c.value < 0n ? -c.value : c.value;
t = O(t, i);
break;
}
}
const a = t * 1000n;
if (e !== void 0 && s !== void 0) {
if (e > s)
throw new RangeError();
} else e !== void 0 ? s = e + a : s !== void 0 ? e = s - a : (e = -a, s = a);
return o().number.bigInt({ min: e, max: s, multipleOf: t });
}
}
class M extends f {
fake() {
return o().datatype.boolean();
}
}
class B extends f {
fake() {
return l(this.schema._def.type);
}
}
class P extends f {
fake() {
throw Error();
}
}
class C extends f {
fake() {
return o().datatype.boolean() ? this.schema.parse(new P(n.never())) : l(this.schema._def.innerType);
}
}
const V = -864e13, $ = 864e13;
class F extends f {
fake() {
let e, s;
for (const t of this.schema._def.checks)
switch (t.kind) {
case "min": {
const a = t.value;
e = e !== void 0 ? Math.max(e, a) : a;
break;
}
case "max": {
const a = t.value;
s = s !== void 0 ? Math.min(s, a) : a;
break;
}
}
return e === void 0 && (o().datatype.boolean({ probability: 0.2 }) ? e = V : e = (s ?? (/* @__PURE__ */ new Date("2025-01-01T00:00:00.000Z")).getTime()) - 31536e6), s === void 0 && (o().datatype.boolean({ probability: 0.2 }) ? s = $ : s = e + 31536e6), o().date.between({ from: e, to: s });
}
}
class W extends f {
fake() {
return o().datatype.boolean() ? this.schema._def.defaultValue() : l(this.schema._def.innerType);
}
}
class G extends f {
fake() {
const e = o().helpers.arrayElement(this.schema.options);
return l(e);
}
}
class H extends f {
fake() {
let e;
switch (this.schema._def.effect.type) {
case "preprocess":
e = l(this.schema._def.schema);
break;
case "refinement":
e = l(this.schema._def.schema);
break;
case "transform":
e = this.schema._def.effect.transform(l(this.schema._def.schema), {
/* v8 ignore next 1 */
addIssue: () => {
},
path: []
});
break;
/* v8 ignore next 3 */
default:
this.schema._def.effect;
}
return e;
}
}
class J extends f {
fake() {
return o().helpers.arrayElement(this.schema._def.values);
}
}
class X extends f {
fake() {
return (...e) => l(this.schema._def.returns);
}
}
class Y extends f {
fake() {
const e = this.findIntersectedSchema(this.schema._def.left, this.schema._def.right);
if (e.success)
return l(e.schema);
throw new TypeError("ZodIntersectionFaker: unable to fake the given schema");
}
findIntersectedSchema = (e, s) => {
const t = [
this.findIntersectedSchemaForUndefined,
this.findIntersectedSchemaForOptional,
this.findIntersectedSchemaForDefault,
this.findIntersectedSchemaForCatch,
this.findIntersectedSchemaForNull,
this.findIntersectedSchemaForNullable,
this.findIntersectedSchemaForIntersection,
this.findIntersectedSchemaForLazy,
this.findIntersectedSchemaForReadonly,
this.findIntersectedSchemaForPipe,
this.findIntersectedSchemaForBrand,
this.findIntersectedSchemaForUnknown,
this.findIntersectedSchemaForAny,
this.findIntersectedSchemaForArray,
this.findIntersectedSchemaForBigint,
this.findIntersectedSchemaForBoolean,
this.findIntersectedSchemaForDate,
this.findIntersectedSchemaForDiscriminatedUnion,
this.findIntersectedSchemaForDiscriminatedUnionAndObject,
this.findIntersectedSchemaForDiscriminatedUnionAndRecord,
this.findIntersectedSchemaForEnum,
this.findIntersectedSchemaForLiteral,
this.findIntersectedSchemaForNativeEnum,
this.findIntersectedSchemaForNumber,
this.findIntersectedSchemaForObject,
this.findIntersectedSchemaForObjectAndRecord,
this.findIntersectedSchemaForRecordWithKeyTypeString,
this.findIntersectedSchemaForString,
this.findIntersectedSchemaForSymbol,
this.findIntersectedSchemaForTuple,
this.findIntersectedSchemaForTupleAndArray,
this.findIntersectedSchemaForUnion,
this.findIntersectedSchemaForUnionAndNonUnion,
this.findIntersectedSchemaForVoid
];
for (const a of t) {
const c = a(e, s);
if (c.success)
return c;
}
return { success: !1 };
};
findIntersectedSchemaForUndefined = (e, s) => {
if (s instanceof n.ZodUndefined) {
const t = this.findIntersectedSchema(n.never().optional(), e);
if (t.success)
return t;
}
return e instanceof n.ZodUndefined ? this.findIntersectedSchemaForUndefined(s, e) : { success: !1 };
};
findIntersectedSchemaForOptional = (e, s) => {
if (e instanceof n.ZodOptional && s instanceof n.ZodOptional && o().datatype.boolean({ probability: 0.2 }))
return { success: !0, schema: n.undefined() };
if (s instanceof n.ZodOptional) {
const t = this.findIntersectedSchema(e, s._def.innerType);
if (t.success)
return t;
}
return e instanceof n.ZodOptional ? this.findIntersectedSchemaForOptional(s, e) : { success: !1 };
};
findIntersectedSchemaForDefault = (e, s) => s instanceof n.ZodDefault ? {
success: !0,
schema: n.union([
n.lazy(() => n.literal(s._def.defaultValue())),
n.intersection(e, s._def.innerType)
])
} : e instanceof n.ZodDefault ? this.findIntersectedSchemaForDefault(s, e) : { success: !1 };
findIntersectedSchemaForCatch = (e, s) => s instanceof n.ZodCatch ? {
success: !0,
schema: n.union([
n.lazy(
() => n.literal(
s._def.catchValue({
error: new n.ZodError([]),
input: void 0
})
)
),
n.intersection(e, s._def.innerType)
])
} : e instanceof n.ZodCatch ? this.findIntersectedSchemaForCatch(s, e) : { success: !1 };
findIntersectedSchemaForNull = (e, s) => {
if (s instanceof n.ZodNull) {
const t = this.findIntersectedSchema(e, n.never().nullable());
if (t.success)
return t;
}
return e instanceof n.ZodNull ? this.findIntersectedSchemaForNull(s, e) : { success: !1 };
};
findIntersectedSchemaForNullable = (e, s) => {
if (e instanceof n.ZodNullable && s instanceof n.ZodNullable && o().datatype.boolean({ probability: 0.2 }))
return { success: !0, schema: n.null() };
if (s instanceof n.ZodNullable) {
const t = this.findIntersectedSchema(e, s._def.innerType);
if (t.success)
return t;
}
return e instanceof n.ZodNullable ? this.findIntersectedSchemaForNullable(s, e) : { success: !1 };
};
findIntersectedSchemaForIntersection = (e, s) => {
if (s instanceof n.ZodIntersection) {
const t = this.findIntersectedSchema(s._def.left, s._def.right);
if (t.success) {
const a = this.findIntersectedSchema(e, t.schema);
if (a.success)
return { success: !0, schema: a.schema };
}
return { success: !1 };
}
return e instanceof n.ZodIntersection ? this.findIntersectedSchemaForIntersection(s, e) : { success: !1 };
};
findIntersectedSchemaForLazy = (e, s) => {
if (s instanceof n.ZodLazy) {
const t = this.findIntersectedSchema(e, s._def.getter());
return t.success ? t : { success: !1 };
}
return e instanceof n.ZodLazy ? this.findIntersectedSchemaForLazy(s, e) : { success: !1 };
};
findIntersectedSchemaForReadonly = (e, s) => {
if (s instanceof n.ZodReadonly) {
const t = this.findIntersectedSchema(e, s._def.innerType);
return t.success ? t : { success: !1 };
}
return e instanceof n.ZodReadonly ? this.findIntersectedSchemaForReadonly(s, e) : { success: !1 };
};
findIntersectedSchemaForPipe = (e, s) => {
if (s instanceof n.ZodPipeline) {
const t = this.findIntersectedSchema(e, s._def.out);
if (t.success)
return t;
}
return e instanceof n.ZodPipeline ? this.findIntersectedSchemaForPipe(s, e) : { success: !1 };
};
findIntersectedSchemaForBrand = (e, s) => {
if (s instanceof n.ZodBranded) {
const t = this.findIntersectedSchema(e, s._def.type);
if (t.success)
return t;
}
return e instanceof n.ZodBranded ? this.findIntersectedSchemaForBrand(s, e) : { success: !1 };
};
findIntersectedSchemaForUnknown = (e, s) => s instanceof n.ZodUnknown ? { success: !0, schema: e } : e instanceof n.ZodUnknown ? this.findIntersectedSchemaForUnknown(s, e) : { success: !1 };
findIntersectedSchemaForAny = (e, s) => s instanceof n.ZodAny ? { success: !0, schema: e } : e instanceof n.ZodAny ? this.findIntersectedSchemaForAny(s, e) : { success: !1 };
findIntersectedSchemaForArray = (e, s) => {
if (!(e instanceof n.ZodArray) || !(s instanceof n.ZodArray))
return { success: !1 };
const t = this.findIntersectedSchema(e._def.type, s._def.type);
if (t.success === !1)
return { success: !1 };
let a = e._def.minLength?.value ?? null;
s._def.minLength !== null && (a = Math.max(a ?? 0, s._def.minLength.value));
let c = e._def.maxLength?.value ?? null;
s._def.maxLength !== null && (c = Math.min(c ?? 1 / 0, s._def.maxLength.value));
let i = e._def.exactLength?.value ?? null;
if (s._def.exactLength !== null) {
if (i !== null && i !== s._def.exactLength.value)
return { success: !1 };
i = s._def.exactLength.value;
}
if (a !== null && i !== null && a > i)
return { success: !1 };
if (c !== null && i !== null && c < i)
return { success: !1 };
if (a !== null && c !== null && a > c)
return { success: !1 };
let d = n.array(t.schema);
return a !== null && (d = d.min(a)), c !== null && (d = d.max(c)), i !== null && (d = d.length(i)), { success: !0, schema: d };
};
findIntersectedSchemaForBigint = (e, s) => {
if (!(e instanceof n.ZodBigInt) || !(s instanceof n.ZodBigInt))
return { success: !1 };
let t = n.bigint();
for (const a of e._def.checks)
switch (a.kind) {
case "min":
t = t.min(a.value);
break;
case "max":
t = t.max(a.value);
break;
case "multipleOf":
t = t.multipleOf(a.value);
break;
}
for (const a of s._def.checks)
switch (a.kind) {
case "min":
t = t.min(a.value);
break;
case "max":
t = t.max(a.value);
break;
case "multipleOf":
t = t.multipleOf(a.value);
break;
}
return { success: !0, schema: t };
};
findIntersectedSchemaForBoolean = (e, s) => !(e instanceof n.ZodBoolean) || !(s instanceof n.ZodBoolean) ? { success: !1 } : { success: !0, schema: e };
findIntersectedSchemaForDate = (e, s) => {
if (!(e instanceof n.ZodDate) || !(s instanceof n.ZodDate))
return { success: !1 };
let t = n.date();
for (const a of e._def.checks)
switch (a.kind) {
case "min":
t = t.min(new Date(a.value));
break;
case "max":
t = t.max(new Date(a.value));
break;
}
for (const a of s._def.checks)
switch (a.kind) {
case "min":
t = t.min(new Date(a.value));
break;
case "max":
t = t.max(new Date(a.value));
break;
}
return { success: !0, schema: t };
};
findIntersectedSchemaForDiscriminatedUnion = (e, s) => {
if (!(e instanceof n.ZodDiscriminatedUnion) || !(s instanceof n.ZodDiscriminatedUnion))
return { success: !1 };
if (e._def.discriminator !== s._def.discriminator)
return { success: !1 };
const t = e._def.options, a = s._def.options, c = [];
for (const i of t) {
const d = i.shape[e._def.discriminator];
if (d instanceof n.ZodLiteral)
for (const p of a) {
const y = p.shape[s._def.discriminator];
if (y instanceof n.ZodLiteral && d._def.value === y._def.value) {
const Z = this.findIntersectedSchema(i, p);
Z.success && Z.schema instanceof n.ZodObject && c.push(Z.schema);
}
}
}
return c.length === 0 ? { success: !1 } : { success: !0, schema: n.discriminatedUnion(e._def.discriminator, c) };
};
findIntersectedSchemaForDiscriminatedUnionAndObject = (e, s) => {
if (e instanceof n.ZodObject && s instanceof n.ZodDiscriminatedUnion) {
const t = [];
for (const a of s._def.options)
e.shape[s._def.discriminator].safeParse(l(a.shape[s._def.discriminator])).success && t.push(
e.merge(
n.object({
[s._def.discriminator]: a.shape[s._def.discriminator]
})
)
);
if (t.length) {
const a = n.discriminatedUnion(s._def.discriminator, t), c = this.findIntersectedSchema(a, s);
if (c.success)
return { success: !0, schema: c.schema };
}
}
return e instanceof n.ZodDiscriminatedUnion && s instanceof n.ZodObject ? this.findIntersectedSchemaForDiscriminatedUnionAndObject(s, e) : { success: !1 };
};
findIntersectedSchemaForDiscriminatedUnionAndRecord = (e, s) => {
if (e instanceof n.ZodRecord && s instanceof n.ZodDiscriminatedUnion) {
const t = [];
for (const a of s._def.options) {
const c = this.findIntersectedSchema(e, a);
c.success && c.schema instanceof n.ZodObject && t.push(c.schema);
}
if (t.length) {
const a = n.discriminatedUnion(s._def.discriminator, t), c = this.findIntersectedSchema(a, s);
if (c.success)
return { success: !0, schema: c.schema };
}
}
return e instanceof n.ZodDiscriminatedUnion && s instanceof n.ZodRecord ? this.findIntersectedSchemaForDiscriminatedUnionAndRecord(s, e) : { success: !1 };
};
findIntersectedSchemaForEnum = (e, s) => {
if (s instanceof n.ZodEnum) {
const t = [];
for (const a of s._def.values)
e.safeParse(a).success && t.push(a);
return typeof t.at(0) == "string" ? { success: !0, schema: n.enum(t) } : { success: !1 };
}
return e instanceof n.ZodEnum ? this.findIntersectedSchemaForEnum(s, e) : { success: !1 };
};
findIntersectedSchemaForLiteral = (e, s) => s instanceof n.ZodLiteral ? e.safeParse(s._def.value).success ? { success: !0, schema: s } : { success: !1 } : e instanceof n.ZodLiteral ? this.findIntersectedSchemaForLiteral(s, e) : { success: !1 };
findIntersectedSchemaForNativeEnum = (e, s) => !(e instanceof n.ZodNativeEnum) || !(s instanceof n.ZodNativeEnum) || e.enum !== s.enum ? { success: !1 } : { success: !0, schema: e };
findIntersectedSchemaForNumber = (e, s) => {
if (!(e instanceof n.ZodNumber) || !(s instanceof n.ZodNumber))
return { success: !1 };
let t = n.number();
for (const a of [...e._def.checks, ...s._def.checks])
switch (a.kind) {
case "min":
t = t.min(a.value);
break;
case "max":
t = t.max(a.value);
break;
case "int":
t = t.int();
break;
case "finite":
t = t.finite();
break;
case "multipleOf":
t = t.multipleOf(a.value);
break;
}
return { success: !0, schema: t };
};
findIntersectedSchemaForObject = (e, s) => {
if (!(e instanceof n.ZodObject) || !(s instanceof n.ZodObject))
return { success: !1 };
let t = n.object({});
const a = e._def.unknownKeys, c = s._def.unknownKeys, i = a === "strict" || c === "strict" ? "strict" : a === "strip" || c === "strip" ? "strip" : "passthrough", d = e._def.catchall, p = s._def.catchall, y = d instanceof n.ZodNever && c === "strict" || p instanceof n.ZodNever && a === "strict" || a === "strict" || c === "strict" ? n.never() : d instanceof n.ZodNever ? p : d, Z = /* @__PURE__ */ new Set([...Object.keys(e.shape), ...Object.keys(s.shape)]);
for (const u of Z) {
const m = e.shape[u], h = s.shape[u];
if (m === void 0)
if (d instanceof n.ZodNever)
switch (a) {
case "strict":
break;
default:
t = t.merge(n.object({ [u]: h }));
}
else {
const k = this.findIntersectedSchema(e._def.catchall, h);
if (k.success)
t = t.merge(n.object({ [u]: k.schema }));
else
return { success: !1 };
}
else if (h === void 0)
if (p instanceof n.ZodNever)
switch (c) {
case "strict":
break;
default:
t = t.merge(n.object({ [u]: m }));
}
else {
const k = this.findIntersectedSchema(m, s._def.catchall);
if (k.success)
t = t.merge(n.object({ [u]: k.schema }));
else
return { success: !1 };
}
else {
const k = this.findIntersectedSchema(m, h);
if (k.success)
t = t.merge(n.object({ [u]: k.schema }));
else
return { success: !1 };
}
}
if (y instanceof n.ZodNever)
switch (i) {
case "strict":
t = t.strict();
break;
case "strip":
t = t.strip();
break;
case "passthrough":
t = t.passthrough().catchall(y);
break;
}
else
t = t.catchall(y);
return { success: !0, schema: t };
};
findIntersectedSchemaForObjectAndRecord = (e, s) => {
if (e instanceof n.ZodRecord && s instanceof n.ZodObject) {
const t = {};
for (const i in s.shape)
t[i] = e._def.valueType;
const a = n.object(t), c = this.findIntersectedSchema(a, s);
if (c.success && c.schema instanceof n.ZodObject)
if (s._def.catchall instanceof n.ZodNever)
switch (s._def.unknownKeys) {
case "strict":
return { success: !0, schema: c.schema.strict() };
case "strip":
return { success: !0, schema: c.schema.strip() };
case "passthrough":
return { success: !0, schema: c.schema.catchall(e._def.valueType) };
}
else {
const i = this.findIntersectedSchema(e._def.valueType, s._def.catchall);
if (i.success)
return { success: !0, schema: c.schema.catchall(i.schema) };
}
}
return e instanceof n.ZodObject && s instanceof n.ZodRecord ? this.findIntersectedSchemaForObjectAndRecord(s, e) : { success: !1 };
};
findIntersectedSchemaForRecordWithKeyTypeString = (e, s) => {
if (!(e instanceof n.ZodRecord) || !(s instanceof n.ZodRecord))
return { success: !1 };
if (!(e._def.keyType instanceof n.ZodString) || !(s._def.keyType instanceof n.ZodString))
return { success: !1 };
const t = this.findIntersectedSchema(e._def.valueType, s._def.valueType);
return t.success === !1 ? { success: !1 } : { success: !0, schema: n.record(t.schema) };
};
findIntersectedSchemaForString = (e, s) => {
if (!(e instanceof n.ZodString) || !(s instanceof n.ZodString))
return { success: !1 };
const t = [
"email",
"url",
"uuid",
"nanoid",
"cuid",
"cuid2",
"ulid",
"regex",
"jwt",
"datetime",
"date",
"time",
"duration",
"ip",
"cidr",
"base64",
"base64url"
], a = e._def.checks.map((i) => i.kind).filter((i) => t.some((d) => i === d)), c = s._def.checks.map((i) => i.kind).filter((i) => t.some((d) => i === d));
return a.length && a.some(
(i) => c.length === 0 || c.includes(i)
) === !1 ? { success: !1 } : {
success: !0,
schema: new U({
coerce: e._def.coerce || s._def.coerce,
checks: [...e._def.checks, ...s._def.checks],
typeName: e._def.typeName
})
};
};
findIntersectedSchemaForSymbol = (e, s) => !(e instanceof n.ZodSymbol) || !(s instanceof n.ZodSymbol) ? { success: !1 } : { success: !0, schema: e };
findIntersectedSchemaForTuple = (e, s) => {
if (!(e instanceof n.ZodTuple) || !(s instanceof n.ZodTuple))
return { success: !1 };
const t = [];
let a;
for (let i = 0; i < Math.min(e._def.items.length, s._def.items.length); ++i) {
const d = this.findIntersectedSchema(e._def.items[i], s._def.items[i]);
d.success && t.push(d.schema);
}
for (let i = e._def.items.length; i < s._def.items.length; ++i) {
const d = this.findIntersectedSchema(e._def.rest, s._def.items[i]);
d.success && t.push(d.schema);
}
for (let i = s._def.items.length; i < e._def.items.length; ++i) {
const d = this.findIntersectedSchema(e._def.items[i], s._def.rest);
d.success && t.push(d.schema);
}
if (e._def.rest !== void 0 && s._def.rest !== void 0) {
const i = this.findIntersectedSchema(e._def.rest, s._def.rest);
i.success && (a = i.schema);
}
let c = n.tuple(t);
return a !== void 0 && (c = c.rest(a)), { success: !0, schema: c };
};
findIntersectedSchemaForTupleAndArray = (e, s) => {
if (e instanceof n.ZodArray && s instanceof n.ZodTuple) {
const t = [];
for (const d of s._def.items) {
const p = this.findIntersectedSchema(e._def.type, d);
p.success && t.push(p.schema);
}
let a;
if (s._def.rest !== void 0) {
const d = this.findIntersectedSchema(e._def.type, s._def.rest);
d.success && (a = d.schema);
}
let c = n.tuple(t);
if (a !== void 0 && (c = c.rest(a)), this.findIntersectedSchema(c, s).success)
return { success: !0, schema: c };
}
return e instanceof n.ZodTuple && s instanceof n.ZodArray ? this.findIntersectedSchemaForTupleAndArray(s, e) : { success: !1 };
};
findIntersectedSchemaForUnion = (e, s) => {
if (!(e instanceof n.ZodUnion) || !(s instanceof n.ZodUnion))
return { success: !1 };
let t;
for (const a of e._def.options)
for (const c of s._def.options) {
const i = this.findIntersectedSchema(a, c);
i.success && (t = t === void 0 ? i.schema : n.union([t, i.schema]));
}
return t === void 0 ? { success: !1 } : { success: !0, schema: t };
};
findIntersectedSchemaForUnionAndNonUnion = (e, s) => s instanceof n.ZodUnion && !(e instanceof n.ZodUnion) ? this.findIntersectedSchema(n.union([e, n.never()]), s) : e instanceof n.ZodUnion && !(s instanceof n.ZodUnion) ? this.findIntersectedSchemaForUnionAndNonUnion(s, e) : { success: !1 };
findIntersectedSchemaForVoid = (e, s) => !(e instanceof n.ZodVoid) || !(s instanceof n.ZodVoid) ? { success: !1 } : { success: !0, schema: e };
}
class q extends f {
fake() {
return l(this.schema._def.getter());
}
}
class Q extends f {
fake() {
return this.schema._def.value;
}
}
class ee extends f {
fake() {
return new Map(
o().helpers.multiple(() => [l(this.schema._def.keyType), l(this.schema._def.valueType)], {
count: { min: 1, max: 10 }
})
);
}
}
class se extends f {
fake() {
return NaN;
}
}
class ne extends f {
fake() {
return o().helpers.enumValue(this.schema._def.values);
}
}
class te extends f {
fake() {
return null;
}
}
class ae extends f {
fake() {
return o().datatype.boolean() ? null : l(this.schema._def.innerType);
}
}
class ce extends f {
fake() {
let e, s, t, a = !1, c = !1;
for (const d of this.schema._def.checks)
switch (d.kind) {
case "min": {
const p = d.value + (d.inclusive ? 0 : 1e-15);
e = e !== void 0 ? Math.max(e, p) : p;
break;
}
case "max": {
const p = d.value - (d.inclusive ? 0 : 1e-15);
s = s !== void 0 ? Math.min(s, p) : p;
break;
}
case "multipleOf": {
const p = d.value;
t = t !== void 0 ? O(t, p) : p;
break;
}
case "int":
a = !0;
break;
case "finite":
c = !0;
break;
}
if (c === !1 && a === !1 && t === void 0) {
if (e === void 0 && o().datatype.boolean({ probability: 0.2 }))
return -1 / 0;
if (s === void 0 && o().datatype.boolean({ probability: 0.2 }))
return 1 / 0;
}
if (e ??= Number.MIN_SAFE_INTEGER, s ??= Number.MAX_SAFE_INTEGER, a = a || t !== void 0 && t % 1 === 0, e > s)
throw new RangeError();
const i = a ? "int" : "float";
return o().number[i]({ min: e, max: s, multipleOf: t });
}
}
class re extends f {
fake() {
const e = {}, s = this.schema._def.catchall, t = this.schema._def.unknownKeys;
if (s instanceof n.ZodNever)
switch (t) {
case "passthrough": {
Object.assign(
e,
Object.fromEntries(
o().helpers.multiple(() => [l(n.string().regex(/^extra_[a-z]{5}$/)), l(n.any())], {
count: { min: 0, max: 5 }
})
)
);
break;
}
}
else
Object.assign(
e,
Object.fromEntries(
o().helpers.multiple(() => [l(n.string().regex(/^extra_[a-z]{5}$/)), l(s)], {
count: { min: 0, max: 5 }
})
)
);
const a = this.schema._def.shape(), c = Object.keys(a);
for (const i of c) {
const d = l(a[i]);
e[i] = d;
}
return e;
}
}
class ie extends f {
fake() {
if (!o().datatype.boolean())
return l(this.schema._def.innerType);
}
}
class oe extends f {
fake() {
return l(this.schema._def.out);
}
}
class de extends f {
fake() {
const e = l(this.schema._def.type);
return o().datatype.boolean() ? Promise.resolve(e) : new Promise((s) => {
setTimeout(() => s(e));
});
}
}
class fe extends f {
fake() {
return Object.freeze(l(this.schema._def.innerType));
}
}
class ue extends f {
fake() {
if (!(this.schema._def.keyType instanceof n.ZodString))
throw new S("Invalid record key type: In runtime JavaScript, all keys are strings.");
return Object.fromEntries(
o().helpers.multiple(() => [l(this.schema._def.keyType), l(this.schema._def.valueType)], {
count: { min: 0, max: 10 }
})
);
}
}
class le extends f {
fake() {
const e = this.schema._def.minSize?.value ?? 0, s = this.schema._def.maxSize?.value ?? o().number.int({ min: e, max: e + 10 });
if (e > s)
throw new RangeError();
const t = /* @__PURE__ */ new Set();
for (; t.size < e; )
t.add(l(this.schema._def.valueType));
for (; t.size < s && o().datatype.boolean(); )
t.add(l(this.schema._def.valueType));
return t;
}
}
const I = 5, me = I * 15, he = I * 200, pe = /^c[^\s-]{8,}$/i, ye = /^[0-9a-z]+$/, Ze = ["☘", "⚜"], ke = ["😳", "😀", "😁", "🤣", "😃", "😆", "😉", "😊", "😋", "😎", "😍", "😘", "🥰", "😗", "😈", "👿"], be = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/, Fe = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/, v = n.date().min(/* @__PURE__ */ new Date("0000-01-01T00:00:00.000Z")).max(/* @__PURE__ */ new Date("9999-12-31T23:59:59.999Z"));
class _e extends f {
fake() {
let e, s, t, a, c, i, d = !1, p = !1, y = !1, Z = !1;
for (const m of this.schema._def.checks)
switch (m.kind) {
case "base64":
return b(be);
case "base64url":
return b(Fe);
case "cidr": {
let h = "";
switch (m.version) {
case "v4":
h = o().internet.ipv4() + "/" + o().number.int({ min: 0, max: 32 });
break;
case "v6":
h = o().internet.ipv6() + "/" + o().number.int({ min: 0, max: 128 });
break;
case void 0:
h = o().datatype.boolean() ? o().internet.ipv4() + "/" + o().number.int({ min: 0, max: 32 }) : o().internet.ipv6() + "/" + o().number.int({ min: 0, max: 128 });
break;
/* v8 ignore next 3 */
default:
m.version;
}
return h;
}
case "cuid":
return b(pe);
case "cuid2":
return b(ye);
case "date":
return new F(v).fake().toISOString().slice(0, 10);
case "datetime":
return new F(v).fake().toISOString();
case "duration":
return [
"P",
o().datatype.boolean() ? o().number.int({ min: 0, max: 10 * 2e3 }) + "Y" : "",
o().datatype.boolean() ? o().number.int({ min: 0, max: 120 }) + "M" : "",
o().datatype.boolean() ? o().number.int({ min: 0, max: 310 }) + "D" : "",
"T",
o().datatype.boolean() ? o().number.int({ min: 0, max: 240 }) + "H" : "",
o().datatype.boolean() ? o().number.int({ min: 0, max: 600 }) + "M" : "",
o().datatype.boolean() ? o().number.int({ min: 0, max: 600 }) + "S" : ""
].join("").replace(/T$/, "").replace(/^P$/, "P" + o().number.int({ min: 0, max: 100 }) + "W");
case "email":
return o().internet.email();
case "emoji": {
Z = !0;
break;
}
case "endsWith":
a = m.value;
break;
case "includes":
c = m.value;
break;
case "ip": {
let h = "";
switch (m.version) {
case "v4":
h = o().internet.ipv4();
break;
case "v6":
h = o().internet.ipv6();
break;
case void 0:
h = o().internet.ip();
break;
/* v8 ignore next 3 */
default:
m.version;
}
return h;
}
case "jwt":
return o().internet.jwt();
case "length": {
const h = m.value;
if (t !== void 0 && t !== h)
throw new RangeError();
t = h;
break;
}
case "max": {
const h = m.value;
s = s === void 0 ? h : Math.min(s, h);
break;
}
case "min": {
const h = m.value;
e = e === void 0 ? h : Math.max(e, h);
break;
}
case "nanoid":
return o().string.nanoid();
case "regex":
return b(m.regex);
case "startsWith":
i = m.value;
break;
case "time":
return new F(v).fake().toISOString().slice(11, -5);
case "toLowerCase":
d = !0;
break;
case "toUpperCase":
p = !0;
break;
case "trim":
y = !0;
break;
case "ulid":
return o().string.ulid();
case "url":
return o().internet.url();
case "uuid":
return o().string.uuid();
}
if (t !== void 0) {
if (e !== void 0) {
if (t < e)
throw new RangeError();
e = t;
}
if (s !== void 0) {
if (s < t)
throw new RangeError();
s = t;
}
}
if (e !== void 0) {
if (s !== void 0 && e > s)
throw new RangeError();
} else
e = 0;
s === void 0 && (s = t ?? e + (o().datatype.boolean() ? I : o().datatype.boolean() ? me : he));
let u = "";
for (; Number.isFinite(e) && u.length < e; )
u += o().lorem.word() + " ";
for (; Number.isFinite(s) && u.length < s; )
u += o().lorem.word() + " ";
if (u = u.slice(0, s), y && (u = u.replace(/\s$/, "a")), d ? u = u.toLowerCase() : u = u.replace(/./g, (m) => o().datatype.boolean() ? m.toUpperCase() : m), p && (u = u.toUpperCase()), c) {
const m = o().number.int({ min: 0, max: Math.max(0, u.length - c.length) });
u = u.slice(0, m) + c + u.slice(m);
}
if (i && (u = i + u.slice(i.length)), a && (u = u.slice(-1 * a.length) + a), Z) {
const m = u.length % 2;
u = Array((u.length - m) / 2).fill(null).map(() => o().helpers.arrayElement(ke)).join(""), m && (u += o().helpers.arrayElement(Ze));
}
return u;
}
}
class ve extends f {
fake() {
return /* @__PURE__ */ Symbol();
}
}
class Se extends f {
fake() {
return [
...this.schema._def.items.map((e) => l(e)),
...this.schema._def.rest ? [l(this.schema._def.rest)] : []
];
}
}
class Ie extends f {
fake() {
}
}
class xe extends f {
fake() {
return l(o().helpers.arrayElement(this.schema._def.options));
}
}
class we extends f {
fake() {
return l(n.any());
}
}
class Te extends f {
fake() {
}
}
function je() {
const r = {
[n.ZodFirstPartyTypeKind.ZodAny]: A,
[n.ZodFirstPartyTypeKind.ZodArray]: R,
[n.ZodFirstPartyTypeKind.ZodBigInt]: z,
[n.ZodFirstPartyTypeKind.ZodBoolean]: M,
[n.ZodFirstPartyTypeKind.ZodBranded]: B,
[n.ZodFirstPartyTypeKind.ZodCatch]: C,
[n.ZodFirstPartyTypeKind.ZodDate]: F,
[n.ZodFirstPartyTypeKind.ZodDefault]: W,
[n.ZodFirstPartyTypeKind.ZodDiscriminatedUnion]: G,
[n.ZodFirstPartyTypeKind.ZodEffects]: H,
[n.ZodFirstPartyTypeKind.ZodEnum]: J,
[n.ZodFirstPartyTypeKind.ZodFunction]: X,
[n.ZodFirstPartyTypeKind.ZodIntersection]: Y,
[n.ZodFirstPartyTypeKind.ZodLazy]: q,
[n.ZodFirstPartyTypeKind.ZodLiteral]: Q,
[n.ZodFirstPartyTypeKind.ZodMap]: ee,
[n.ZodFirstPartyTypeKind.ZodNaN]: se,
[n.ZodFirstPartyTypeKind.ZodNativeEnum]: ne,
[n.ZodFirstPartyTypeKind.ZodNever]: P,
[n.ZodFirstPartyTypeKind.ZodNull]: te,
[n.ZodFirstPartyTypeKind.ZodNullable]: ae,
[n.ZodFirstPartyTypeKind.ZodNumber]: ce,
[n.ZodFirstPartyTypeKind.ZodObject]: re,
[n.ZodFirstPartyTypeKind.ZodOptional]: ie,
[n.ZodFirstPartyTypeKind.ZodPipeline]: oe,
[n.ZodFirstPartyTypeKind.ZodPromise]: de,
[n.ZodFirstPartyTypeKind.ZodReadonly]: fe,
[n.ZodFirstPartyTypeKind.ZodRecord]: ue,
[n.ZodFirstPartyTypeKind.ZodSet]: le,
[n.ZodFirstPartyTypeKind.ZodString]: _e,
[n.ZodFirstPartyTypeKind.ZodSymbol]: ve,
[n.ZodFirstPartyTypeKind.ZodTuple]: Se,
[n.ZodFirstPartyTypeKind.ZodUndefined]: Ie,
[n.ZodFirstPartyTypeKind.ZodUnion]: xe,
[n.ZodFirstPartyTypeKind.ZodUnknown]: we,
[n.ZodFirstPartyTypeKind.ZodVoid]: Te
};
for (const e of Object.keys(
r
))
x.set(
e,
r[e]
);
}
function ge(r, e) {
w.set(r, e);
}
export {
S as ZodSchemaFakerError,
f as ZodTypeFaker,
l as fake,
o as getFaker,
je as install,
ge as installCustom,
Ke as installFaker,
b as randexp,
Ee as runFake,
Ue as seed,
g as setFaker
};