zod#ZodObjectDef TypeScript Examples
The following examples show how to use
zod#ZodObjectDef.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: object.ts From zod-to-json-schema with ISC License | 6 votes |
export function parseObjectDef(def: ZodObjectDef, refs: References) {
const result: JsonSchema7ObjectType = {
type: "object",
...Object.entries(def.shape()).reduce(
(
acc: {
properties: Record<string, JsonSchema7Type>;
required: string[];
},
[propName, propDef]
) => {
if (propDef === undefined || propDef._def === undefined) return acc;
const parsedDef = parseDef(
propDef._def,
refs.addToPathAsProperty("properties", propName)
);
if (parsedDef === undefined) return acc;
return {
properties: { ...acc.properties, [propName]: parsedDef },
required: propDef.isOptional()
? acc.required
: [...acc.required, propName],
};
},
{ properties: {}, required: [] }
),
additionalProperties:
def.catchall._def.typeName === "ZodNever"
? def.unknownKeys === "passthrough"
: parseDef(def.catchall._def, refs.addToPath("additionalProperties")) ??
true,
};
if (!result.required!.length) delete result.required;
return result;
}