Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions express-zod-api/src/common-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ export const getMessageFromError = (error: Error): string => {
export const pullExampleProps = <T extends z.ZodObject>(subject: T) =>
Object.entries(subject.shape).reduce<Partial<z.input<T>>[]>(
(acc, [key, schema]) => {
const examples =
(schema as z.ZodType).meta()?.[metaSymbol]?.examples || [];
const { examples = [] } = globalRegistry.get(schema)?.[metaSymbol] || {};
return combinations(acc, examples.map(R.objOf(key)), ([left, right]) => ({
...left,
...right,
Expand Down
4 changes: 3 additions & 1 deletion express-zod-api/src/documentation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ export const depictRequestParams = ({
name,
in: location,
deprecated: globalRegistry.get(paramSchema)?.deprecated,
required: !(paramSchema as z.ZodType).isOptional(),
required: !(
paramSchema instanceof z.ZodType && paramSchema.isOptional()
),
description: depicted.description || description,
schema: result,
examples: depictParamExamples(objectSchema, name),
Expand Down
18 changes: 10 additions & 8 deletions express-zod-api/src/zts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import type {
$ZodDefault,
$ZodDiscriminatedUnion,
$ZodEnum,
$ZodInterface,
$ZodIntersection,
$ZodLazy,
$ZodLiteral,
$ZodNullable,
$ZodObject,
$ZodOptional,
$ZodPipe,
$ZodReadonly,
Expand Down Expand Up @@ -66,7 +68,7 @@ const onLiteral: Producer = ({ _zod: { def } }: $ZodLiteral) => {
return values.length === 1 ? values[0] : f.createUnionTypeNode(values);
};

const onInterface: Producer = (int: z.ZodInterface, { next, makeAlias }) =>
const onInterface: Producer = (int: $ZodInterface, { next, makeAlias }) =>
makeAlias(int, () => {
const members = Object.entries(int._zod.def.shape).map<ts.TypeElement>(
([key, value]) => {
Expand All @@ -84,16 +86,16 @@ const onInterface: Producer = (int: z.ZodInterface, { next, makeAlias }) =>
});

const onObject: Producer = (
{ _zod: { def } }: z.ZodObject,
{ _zod: { def } }: $ZodObject,
{ isResponse, next },
) => {
const members = Object.entries(def.shape).map<ts.TypeElement>(
([key, value]) => {
const isOptional = isResponse
? value instanceof z.ZodOptional
: value instanceof z.ZodPromise
? false
: (value as z.ZodType).isOptional();
? value._zod.def.type === "optional"
: value._zod.def.type !== "promise" &&
value instanceof z.ZodType &&
value.isOptional();
const { description: comment, deprecated: isDeprecated } =
globalRegistry.get(value) || {};
return makeInterfaceProp(key, next(value), {
Expand Down Expand Up @@ -211,9 +213,9 @@ const onFile: Producer = (schema: FileSchema) => {
const stringType = ensureTypeNode(ts.SyntaxKind.StringKeyword);
const bufferType = ensureTypeNode("Buffer");
const unionType = f.createUnionTypeNode([stringType, bufferType]);
return schema instanceof z.ZodString
return schema._zod.def.type === "string"
? stringType
: schema instanceof z.ZodUnion
: schema._zod.def.type === "union"
? unionType
: bufferType;
};
Expand Down