Skip to content

Commit

Permalink
Handle Loose References to Zod Objects (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored Apr 24, 2023
1 parent e59af64 commit 297f72b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/rules/require-openapi/rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ruleTester.run(ruleName, rule, {
test('object-shape'),
test('literal-no-openapi'),
test('optional-no-openapi'),
test('reference'),
],
invalid: [
{
Expand Down
6 changes: 5 additions & 1 deletion src/rules/require-openapi/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export const rule = createRule({
}
},
Property(node) {
if (node.value.type === 'Identifier') {
return;
}

const type = getType(node, context);

if (!type?.isZodType || type.name === 'ZodLiteral') {
Expand All @@ -44,7 +48,7 @@ export const rule = createRule({

const openApiCallExpression = findOpenApiCallExpression(node);

if (!openApiCallExpression && !getInferredComment(node, context)) {
if (!openApiCallExpression) {
return context.report({
messageId: 'open-api-required',
node: node.key,
Expand Down
33 changes: 33 additions & 0 deletions src/rules/require-openapi/tests/reference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { z } from 'zod';
import { ZodOpenApiOperationObject, extendZodWithOpenApi } from 'zod-openapi';
extendZodWithOpenApi(z);

const QuerySchema = z
.object({
a: z.string().openapi({ description: 'a', example: 'a' }),
})
.openapi({ description: 'Query schema' });

const GetJobResponseSchema = z
.object({
a: z.string().openapi({ description: 'a', example: 'a' }),
})
.openapi({ description: 'Response schema' });

export const getJobOperation: ZodOpenApiOperationObject = {
operationId: 'getJob',
summary: 'Get Job',
requestParams: {
query: QuerySchema,
},
responses: {
'200': {
description: 'Successful operation',
content: {
'application/json': {
schema: GetJobResponseSchema,
},
},
},
},
};

0 comments on commit 297f72b

Please sign in to comment.