eslint-plugin-zod-to-openapi
Advanced tools
Comparing version 0.0.21 to 0.0.22
@@ -70,4 +70,3 @@ "use strict"; | ||
meta: { | ||
fixable: 'code', | ||
type: 'problem', | ||
type: 'suggestion', | ||
messages: { | ||
@@ -74,0 +73,0 @@ requires: '.openapi() should be declared at the end of a zod chain', |
@@ -66,4 +66,3 @@ "use strict"; | ||
meta: { | ||
fixable: 'code', | ||
type: 'problem', | ||
type: 'suggestion', | ||
messages: { | ||
@@ -70,0 +69,0 @@ prefer: 'use .default() instead of .openapi() default', |
@@ -170,2 +170,53 @@ "use strict"; | ||
}, | ||
TSTypeReference(node) { | ||
// only check z.infer, z.input, z.output | ||
if (node.typeName.type !== 'TSQualifiedName' || | ||
node.typeName.left.type !== 'Identifier' || | ||
node.typeName.left.name !== 'z' || | ||
node.typeName.right.type !== 'Identifier' || | ||
(node.typeName.right.name !== 'infer' && | ||
node.typeName.right.name !== 'input' && | ||
node.typeName.right.name !== 'output')) { | ||
return; | ||
} | ||
if (node.typeParameters?.type !== 'TSTypeParameterInstantiation') { | ||
return; | ||
} | ||
const param = node.typeParameters.params[0]; | ||
if (param?.type !== 'TSTypeQuery' || | ||
param.exprName.type !== 'Identifier') { | ||
return; | ||
} | ||
const declaration = node.parent; | ||
if (declaration?.type !== 'TSTypeAliasDeclaration') { | ||
return; | ||
} | ||
const expectedCommentValue = (0, type_1.getInferredComment)(param.exprName, context); | ||
if (!expectedCommentValue) { | ||
return; | ||
} | ||
const commentNode = (0, exports.getCommentNode)(declaration); | ||
const comment = (0, exports.getComment)(commentNode, context); | ||
if (!comment) { | ||
return context.report({ | ||
messageId: 'comment', | ||
node: declaration.id, | ||
fix: (fixer) => fixer.insertTextBefore(commentNode, createFormattedComment(expectedCommentValue, commentNode.loc)), | ||
}); | ||
} | ||
const commentValue = commentRegex.exec(comment.value)?.[1]; | ||
if (!commentValue || expectedCommentValue !== commentValue) { | ||
return context.report({ | ||
messageId: 'comment', | ||
node: comment, | ||
fix: (fixer) => [ | ||
fixer.removeRange([ | ||
comment.range[0] - (comment.loc.start.column + 1), | ||
comment.range[1], | ||
]), | ||
fixer.insertTextBefore(commentNode, createFormattedComment(expectedCommentValue, commentNode.loc)), | ||
], | ||
}); | ||
} | ||
}, | ||
}; | ||
@@ -176,3 +227,3 @@ }, | ||
fixable: 'code', | ||
type: 'problem', | ||
type: 'suggestion', | ||
messages: { | ||
@@ -179,0 +230,0 @@ required: '.openapi() description is required on Zod Schema', |
@@ -72,4 +72,3 @@ "use strict"; | ||
meta: { | ||
fixable: 'code', | ||
type: 'problem', | ||
type: 'suggestion', | ||
messages: { | ||
@@ -76,0 +75,0 @@ required: '.openapi() example is required for Zod primatives', |
@@ -77,4 +77,3 @@ "use strict"; | ||
meta: { | ||
fixable: 'code', | ||
type: 'problem', | ||
type: 'suggestion', | ||
messages: { | ||
@@ -81,0 +80,0 @@ 'open-api-required': '.openapi() is required on Zod Schema', |
@@ -5,5 +5,6 @@ import { TSESLint, TSESTree } from '@typescript-eslint/utils'; | ||
export declare const isNewLineRequired: (node: TSESTree.Property) => boolean; | ||
export declare const rule: TSESLint.RuleModule<"required" | "comment", never[], { | ||
export declare const rule: TSESLint.RuleModule<"comment" | "required", never[], { | ||
VariableDeclaration(node: TSESTree.VariableDeclaration): void; | ||
Property(node: TSESTree.Property): void; | ||
TSTypeReference(node: TSESTree.TSTypeReference): void; | ||
}>; |
{ | ||
"name": "eslint-plugin-zod-to-openapi", | ||
"version": "0.0.21", | ||
"version": "0.0.22", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "Eslint rules for zod-to-openapi", |
@@ -9,3 +9,3 @@ # eslint-plugin-zod-to-openapi | ||
This is a set of Eslint rules created for use with [@asteasolutions/zod-to-openapi]. As a contributor and major user of the library, there are some learnings and difficulties with using the library by itself and this repository hopes to address those issues. | ||
This is a set of Eslint rules created for use with [@asteasolutions/zod-to-openapi]. As a contributor and major user of the library, there are some learnings with using the library by itself and this package hopes to address those issues as well as optimise the overall developer experience with a sprinkle of magic â¨. | ||
@@ -96,10 +96,15 @@ ## Table of Contents | ||
const NameSchema2 = z.string().openapi({ description: "A user's name" }); | ||
const IdSchema = z.string().uuid(); | ||
const ObjectSchema = z.object({ | ||
/** | ||
* A user's name | ||
**/ | ||
name: z.string().openapi({ description: "A user's name" }); | ||
}) | ||
/** | ||
* Other Schema | ||
**/ | ||
const OtherSchema = z | ||
.object({ | ||
/** | ||
* A user's age | ||
**/ | ||
age: z.number().openapi({ description: "A user's age" }), | ||
}) | ||
.openapi({ description: 'Other Schema' }); | ||
@@ -112,7 +117,7 @@ const PersonSchema = z | ||
name: NameSchema, // â correct | ||
name2: NameSchema2, // â error (no comment) | ||
id: IdSchema, // â error (IdSchema has no comment) | ||
/** | ||
* A user's name | ||
**/ | ||
name3: ObjectSchema.shape.name, // â correct | ||
age: OtherSchema.shape.age, // â correct | ||
}) | ||
@@ -135,4 +140,6 @@ .openapi({ description: 'Person' }); | ||
Requires that all Zod schemas which have an `.openapi()` object have a `description` and matching comment. In order for your IDE to display descriptions in inferred types, it requires JsDoc comments. This rule can generate comments based on your `description` and `deprecated` fields and adds it to your variable declaration. This rule is autofixable. | ||
This rule was rhe inspiration for the entire package. It requires that all Zod schemas which have an `.openapi()` object have a `description` and matching jsDoc comment. | ||
In order for your IDE to display descriptions in inferred types, it requires JsDoc comments. This rule autogenerates comments based on your `description` and `deprecated` fields and adds it to your ZodSchema. These appear in both the inferred and actual ZodSchema. This rule is autofixable. | ||
A simple example | ||
@@ -169,3 +176,3 @@ | ||
/** | ||
* @deprecated A user's name // âšī¸ this comment is copied across from NameSchema | ||
* @deprecated A user's name // âšī¸ this comment is synced with NameSchema | ||
**/ | ||
@@ -183,2 +190,7 @@ name: NameSchema, // âšī¸ This type will be marked as deprecated in your IDE | ||
.openapi({ description: 'Person' }); | ||
/** | ||
* Person | ||
**/ | ||
type Person = z.infer<typeof PersonSchema>; // âšī¸ This comment is synced with PersonSchema. This does not work for indexed access eg. z.infer<typeof PersonSchema>['name']. | ||
``` | ||
@@ -185,0 +197,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
66303
798
314
27