zod-prime
Version:
Enhance Zod with real-world schema utilities for production-grade apps.
204 lines (193 loc) • 5.99 kB
JavaScript
// src/deepPartial.ts
import { z } from "zod";
function deepPartial(schema) {
if (schema instanceof z.ZodObject) {
const shape = schema.shape;
const partialShape = {};
for (const key in shape) {
partialShape[key] = deepPartial(shape[key]);
}
return z.object(partialShape).partial();
}
if (schema instanceof z.ZodArray) {
return z.array(deepPartial(schema.element));
}
if (schema instanceof z.ZodTuple) {
return z.tuple(schema.items.map(deepPartial));
}
if (schema instanceof z.ZodRecord) {
return z.record(deepPartial(schema.valueSchema));
}
if (schema instanceof z.ZodMap) {
return z.map(schema.keySchema, deepPartial(schema.valueSchema));
}
return schema.optional();
}
// src/deepRequired.ts
import { z as z2 } from "zod";
function deepRequired(schema) {
if (schema._def?.typeName === z2.ZodFirstPartyTypeKind.ZodOptional || schema._def?.typeName === z2.ZodFirstPartyTypeKind.ZodNullable) {
return deepRequired(schema._def.innerType);
}
if (schema instanceof z2.ZodObject) {
const shape = schema.shape;
const requiredShape = {};
for (const key in shape) {
requiredShape[key] = deepRequired(shape[key]);
}
return z2.object(requiredShape);
}
if (schema instanceof z2.ZodArray) {
return z2.array(deepRequired(schema.element));
}
if (schema instanceof z2.ZodTuple) {
return z2.tuple(schema.items.map(deepRequired));
}
if (schema instanceof z2.ZodRecord) {
return z2.record(deepRequired(schema.valueSchema));
}
if (schema instanceof z2.ZodMap) {
return z2.map(schema.keySchema, deepRequired(schema.valueSchema));
}
return schema;
}
// src/emailPasswordSchema.ts
import { z as z3 } from "zod";
function emailPasswordSchema(options) {
let password = z3.string().min(options?.minPasswordLength || 8);
if (options?.requireSpecialChar) {
password = password.regex(/[^a-zA-Z0-9]/, "Password must contain a special character");
}
return z3.object({
email: z3.string().email(),
password
});
}
// src/smartEnum.ts
import { z as z4 } from "zod";
function smartEnum(values) {
return z4.enum(values);
}
// src/flattenErrors.ts
function flattenErrors(error) {
return error.errors.map((e) => ({
path: e.path.map(String).join("."),
message: e.message
}));
}
// src/refineObject.ts
function refineObject(schema, refineFn, message) {
return schema.superRefine((data, ctx) => {
if (!refineFn(data)) {
ctx.addIssue({ code: "custom", message });
}
});
}
// src/deepOmit.ts
import { z as z5 } from "zod";
function deepOmit(schema, keys) {
if (schema instanceof z5.ZodObject) {
const shape = schema.shape;
const newShape = {};
for (const key in shape) {
if (!keys.includes(key)) {
newShape[key] = deepOmit(shape[key], keys);
}
}
return z5.object(newShape).strict();
}
if (schema instanceof z5.ZodArray) {
return z5.array(deepOmit(schema.element, keys));
}
if (schema instanceof z5.ZodTuple) {
return z5.tuple(schema.items.map((item) => deepOmit(item, keys)));
}
if (schema instanceof z5.ZodRecord) {
return z5.record(deepOmit(schema.valueSchema, keys));
}
if (schema instanceof z5.ZodMap) {
return z5.map(schema.keySchema, deepOmit(schema.valueSchema, keys));
}
return schema;
}
// src/deepPick.ts
import { z as z6 } from "zod";
function deepPick(schema, keys) {
if (schema instanceof z6.ZodObject) {
const shape = schema.shape;
const newShape = {};
for (const key in shape) {
if (keys.includes(key)) {
newShape[key] = deepPick(shape[key], keys);
} else if (shape[key] instanceof z6.ZodObject || shape[key] instanceof z6.ZodArray) {
const nested = deepPick(shape[key], keys);
if (nested instanceof z6.ZodObject && Object.keys(nested.shape ?? {}).length > 0 || nested instanceof z6.ZodArray && (nested.element instanceof z6.ZodObject && Object.keys(nested.element.shape ?? {}).length > 0 || !(nested.element instanceof z6.ZodObject))) {
newShape[key] = nested;
}
}
}
return z6.object(newShape).strict();
}
if (schema instanceof z6.ZodArray) {
const pickedElement = deepPick(schema.element, keys);
if (pickedElement instanceof z6.ZodObject && Object.keys(pickedElement.shape ?? {}).length === 0) {
return z6.array(z6.never());
}
return z6.array(pickedElement);
}
if (schema instanceof z6.ZodTuple) {
return z6.tuple(schema.items.map((item) => deepPick(item, keys)));
}
if (schema instanceof z6.ZodRecord) {
return z6.record(deepPick(schema.valueSchema, keys));
}
if (schema instanceof z6.ZodMap) {
return z6.map(schema.keySchema, deepPick(schema.valueSchema, keys));
}
return schema;
}
// src/mergeSchemas.ts
function mergeSchemas(a, b) {
return a.merge(b);
}
// src/zodDefault.ts
function zodDefault(schema, defaultValue) {
return schema.default(defaultValue);
}
// src/zodToJsonSchema.ts
import { z as z7 } from "zod";
function zodToJsonSchema(schema) {
if (schema instanceof z7.ZodObject) {
const shape = schema.shape;
const properties = {};
for (const key in shape) {
properties[key] = zodToJsonSchema(shape[key]);
}
return { type: "object", properties };
}
if (schema instanceof z7.ZodString)
return { type: "string" };
if (schema instanceof z7.ZodNumber)
return { type: "number" };
if (schema instanceof z7.ZodBoolean)
return { type: "boolean" };
if (schema instanceof z7.ZodArray)
return { type: "array", items: zodToJsonSchema(schema.element) };
if (schema._def?.typeName === z7.ZodFirstPartyTypeKind.ZodOptional || schema._def?.typeName === z7.ZodFirstPartyTypeKind.ZodNullable) {
return zodToJsonSchema(schema._def.innerType);
}
return {};
}
export {
deepOmit,
deepPartial,
deepPick,
deepRequired,
emailPasswordSchema,
flattenErrors,
mergeSchemas,
refineObject,
smartEnum,
zodDefault,
zodToJsonSchema
};