Skip to content

Commit 9cad0ec

Browse files
Merge pull request #194 from BEARlogin/fix-required
Fix Handling of Optional Zod Fields with NestJS Swagger
2 parents 633adcf + ab4c5c6 commit 9cad0ec

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

packages/zod-nestjs/src/lib/create-zod-dto.spec.ts

+61-4
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ describe('zod-nesjs create-zod-dto', () => {
126126
const metadataFactory = getMetadataFactory(schema);
127127

128128
const generatedSchema = metadataFactory();
129-
const personName = generatedSchema?.person.properties?.name as SchemaObject30
130-
const tags = generatedSchema?.person.properties?.tags as SchemaObject30
131-
const tagsItems = tags.items as SchemaObject30
132-
const tagName = tagsItems.properties?.name as SchemaObject30
129+
const personName = generatedSchema?.person.properties
130+
?.name as SchemaObject30;
131+
const tags = generatedSchema?.person.properties?.tags as SchemaObject30;
132+
const tagsItems = tags.items as SchemaObject30;
133+
const tagName = tagsItems.properties?.name as SchemaObject30;
133134

134135
expect(generatedSchema).toBeDefined();
135136
expect(personName.type).toEqual('string');
@@ -150,6 +151,62 @@ describe('zod-nesjs create-zod-dto', () => {
150151
expect(generatedSchema?.name.type).toEqual('string');
151152
expect(generatedSchema?.name.nullable).toBe(true);
152153
});
154+
155+
it('should correct work with optional fields and make required fields false', () => {
156+
const schema = z.object({
157+
pagination: z
158+
.object({
159+
limit: z.number(),
160+
offset: z.number(),
161+
})
162+
.optional(),
163+
filter: z
164+
.object({
165+
category: z.string().uuid(),
166+
userId: z.string().uuid(),
167+
})
168+
.optional(),
169+
sort: z
170+
.object({
171+
field: z.string(),
172+
order: z.string(),
173+
})
174+
.optional(),
175+
});
176+
const metadataFactory = getMetadataFactory(schema);
177+
178+
const generatedSchema = metadataFactory();
179+
expect(generatedSchema?.pagination.required).toEqual(false);
180+
expect(generatedSchema?.sort.required).toEqual(false);
181+
expect(generatedSchema?.filter.required).toEqual(false);
182+
});
183+
184+
it('should correct work with optional fields and make required field true and optional field false', () => {
185+
const schema = z.object({
186+
pagination: z
187+
.object({
188+
limit: z.number(),
189+
offset: z.number(),
190+
})
191+
.optional(),
192+
filter: z
193+
.object({
194+
category: z.string().uuid(),
195+
userId: z.string().uuid(),
196+
})
197+
.optional(),
198+
sort: z.object({
199+
field: z.string(),
200+
order: z.string(),
201+
}),
202+
});
203+
const metadataFactory = getMetadataFactory(schema);
204+
205+
const generatedSchema = metadataFactory();
206+
expect(generatedSchema?.pagination.required).toEqual(false);
207+
expect(generatedSchema?.sort.required).toEqual(true);
208+
expect(generatedSchema?.filter.required).toEqual(false);
209+
});
153210
});
154211

155212
function getMetadataFactory(zodRef: OpenApiZodAny) {

packages/zod-nestjs/src/lib/create-zod-dto.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export const createZodDto = <T extends OpenApiZodAny>(
117117
)) {
118118
SchemaHolderClass.convertSchemaObject(
119119
subSchemaObject,
120-
schemaObject.required?.includes(key)
120+
schemaObject.required?.includes(key) ?? false
121121
);
122122
}
123123

@@ -141,9 +141,9 @@ export const createZodDto = <T extends OpenApiZodAny>(
141141
if (Array.isArray(convertedSchemaObject.type)) {
142142
convertedSchemaObject.nullable =
143143
convertedSchemaObject.type.includes('null') || undefined;
144-
convertedSchemaObject.type = convertedSchemaObject.type.find(
145-
(item) => item !== 'null'
146-
) || 'string';
144+
convertedSchemaObject.type =
145+
convertedSchemaObject.type.find((item) => item !== 'null') ||
146+
'string';
147147
} else if (convertedSchemaObject.type === 'null') {
148148
convertedSchemaObject.type = 'string'; // There ist no explicit null value in OpenAPI 3.0
149149
convertedSchemaObject.nullable = true;

0 commit comments

Comments
 (0)