Skip to content

Commit

Permalink
feat: set additionalProperties to false if strict
Browse files Browse the repository at this point in the history
  • Loading branch information
trs committed Jan 10, 2023
1 parent e21f365 commit 80c7012
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,31 @@ describe('zodOpenapi', () => {
});
});

it('should support `strict` on an object schema', () => {
const zodSchema = extendApi(
z
.object({
aString: z.string(),
aNumber: z.number(),
})
.strict(),
{
description: "Super strict",
}
);
const apiSchema = generateSchema(zodSchema);
expect(apiSchema).toEqual({
type: 'object',
required: ['aString', 'aNumber'],
properties: {
aString: { type: 'string' },
aNumber: { type: 'number' },
},
additionalProperties: false,
description: "Super strict",
});
});

it('Testing large mixed schema', () => {
enum Fruits {
Apple,
Expand Down
4 changes: 3 additions & 1 deletion packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ function parseObject({
additionalProperties = generateSchema(zodRef._def.catchall, useOutput);
else if (zodRef._def.unknownKeys === 'passthrough')
additionalProperties = true;
else if (zodRef._def.unknownKeys === 'strict')
additionalProperties = false;

// So that `undefined` values don't end up in the schema and be weird
additionalProperties = additionalProperties ? { additionalProperties } : {};
additionalProperties = additionalProperties != null ? { additionalProperties } : {};

return merge(
{
Expand Down

0 comments on commit 80c7012

Please sign in to comment.