json-schema-to-zod
Version:
Converts JSON schema objects or files into Zod schemas
25 lines (24 loc) • 748 B
JavaScript
import { withMessage } from "../utils/withMessage.js";
import { parseSchema } from "./parseSchema.js";
export const parseArray = (schema, refs) => {
if (Array.isArray(schema.items)) {
return `z.tuple([${schema.items.map((v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] }))}])`;
}
let r = !schema.items
? "z.array(z.any())"
: `z.array(${parseSchema(schema.items, {
...refs,
path: [...refs.path, "items"],
})})`;
r += withMessage(schema, "minItems", ({ json }) => [
`.min(${json}`,
", ",
")",
]);
r += withMessage(schema, "maxItems", ({ json }) => [
`.max(${json}`,
", ",
")",
]);
return r;
};