zod#ZodTypeAny TypeScript Examples
The following examples show how to use
zod#ZodTypeAny.
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: record.ts From zod-to-json-schema with ISC License | 6 votes |
export function parseRecordDef(
def: ZodRecordDef<ZodTypeAny, ZodTypeAny>,
refs: References
): JsonSchema7RecordType {
const schema: JsonSchema7RecordType = {
type: "object",
additionalProperties:
parseDef(def.valueType._def, refs.addToPath("additionalProperties")) ||
{},
}
if (
def.keyType?._def.typeName === ZodFirstPartyTypeKind.ZodString &&
def.keyType._def.checks?.length
) {
const keyType: JsonSchema7RecordPropertyNamesType = Object.entries(
parseStringDef(def.keyType._def)
).reduce(
(acc, [key, value]) => (key === "type" ? acc : { ...acc, [key]: value }),
{}
);
return {
...schema,
propertyNames: keyType,
};
} else if (def.keyType?._def.typeName === ZodFirstPartyTypeKind.ZodEnum) {
return {
...schema,
propertyNames: {
enum: def.keyType._def.values
}
}
}
return schema
}
Example #2
Source File: tuple.ts From zod-to-json-schema with ISC License | 6 votes |
export function parseTupleDef(
def: ZodTupleDef<ZodTupleItems | [], ZodTypeAny | null>,
refs: References
): JsonSchema7TupleType {
if (def.rest) {
return {
type: "array",
minItems: def.items.length,
items: def.items
.map((x, i) => parseDef(x._def, refs.addToPath("items", i.toString())))
.reduce(
(acc: JsonSchema7Type[], x) => (x === undefined ? acc : [...acc, x]),
[]
),
additionalItems: parseDef(
def.rest._def,
refs.addToPath("additionalItems")
),
};
} else {
return {
type: "array",
minItems: def.items.length,
maxItems: def.items.length,
items: def.items
.map((x, i) => parseDef(x._def, refs.addToPath("items", i.toString())))
.reduce(
(acc: JsonSchema7Type[], x) => (x === undefined ? acc : [...acc, x]),
[]
),
};
}
}
Example #3
Source File: union.ts From zod-to-json-schema with ISC License | 5 votes |
export function parseUnionDef(
def: ZodUnionDef | ZodDiscriminatedUnionDef<any, any, any>,
refs: References
): JsonSchema7PrimitiveUnionType | JsonSchema7AnyOfType | undefined {
if (refs.target === "openApi3") return asAnyOf(def, refs);
const options: readonly ZodTypeAny[] =
def.options instanceof Map ? Array.from(def.options.values()) : def.options;
// This blocks tries to look ahead a bit to produce nicer looking schemas with type array instead of anyOf.
if (
options.every(
(x) =>
x._def.typeName in primitiveMappings &&
(!x._def.checks || !x._def.checks.length)
)
) {
// all types in union are primitive and lack checks, so might as well squash into {type: [...]}
const types = options.reduce((types: JsonSchema7Primitive[], x) => {
const type = primitiveMappings[x._def.typeName as ZodPrimitive]; //Can be safely casted due to row 43
return type && !types.includes(type) ? [...types, type] : types;
}, []);
return {
type: types.length > 1 ? types : types[0],
};
} else if (options.every((x) => x._def.typeName === "ZodLiteral")) {
// all options literals
const types = options.reduce(
(acc: JsonSchema7Primitive[], x: { _def: ZodLiteralDef }) => {
const type = typeof x._def.value;
switch (type) {
case "string":
case "number":
case "boolean":
return [...acc, type];
case "bigint":
return [...acc, "integer" as const];
case "object":
if (x._def.value === null) return [...acc, "null" as const];
case "symbol":
case "undefined":
case "function":
default:
return acc;
}
},
[]
);
if (types.length === options.length) {
// all the literals are primitive, as far as null can be considered primitive
const uniqueTypes = types.filter((x, i, a) => a.indexOf(x) === i);
return {
type: uniqueTypes.length > 1 ? uniqueTypes : uniqueTypes[0],
enum: options.reduce((acc, x) => {
return acc.includes(x._def.value) ? acc : [...acc, x._def.value];
}, [] as (string | number | bigint | boolean | null)[]),
};
}
} else if (options.every((x) => x._def.typeName === "ZodEnum")) {
return {
type: "string",
enum: options.reduce(
(acc: string[], x) => [
...acc,
...x._def.values.filter((x: string) => !acc.includes(x)),
],
[]
),
};
}
return asAnyOf(def, refs);
}