eslint-plugin-class-validator-type-match 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/rules/dto-filename-match.d.ts +48 -0
- package/dist/rules/dto-filename-match.js +157 -0
- package/package.json +1 -1
- package/readme.md +62 -1
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ declare const _default: {
|
|
|
9
9
|
'validate-nested-match': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"nestedArrayMismatch" | "missingValidateNested" | "missingEachOption" | "unnecessaryValidateNested" | "tupleValidationWarning" | "multiTypeUnionWarning" | "mixedComplexityUnionWarning" | "pickOmitWarning", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener>;
|
|
10
10
|
'type-decorator-match': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"typeMismatch" | "missingTypeDecorator", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener>;
|
|
11
11
|
'definite-assignment-match': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"missingDefiniteAssignment", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener>;
|
|
12
|
+
'dto-filename-match': import("@typescript-eslint/utils/dist/ts-eslint").RuleModule<"incorrectDtoClassName", [], unknown, import("@typescript-eslint/utils/dist/ts-eslint").RuleListener>;
|
|
12
13
|
};
|
|
13
14
|
configs: {
|
|
14
15
|
recommended: {
|
|
@@ -19,6 +20,7 @@ declare const _default: {
|
|
|
19
20
|
'class-validator-type-match/validate-nested-match': string;
|
|
20
21
|
'class-validator-type-match/type-decorator-match': string;
|
|
21
22
|
'class-validator-type-match/definite-assignment-match': string;
|
|
23
|
+
'class-validator-type-match/dto-filename-match': string;
|
|
22
24
|
};
|
|
23
25
|
};
|
|
24
26
|
strict: {
|
|
@@ -29,6 +31,7 @@ declare const _default: {
|
|
|
29
31
|
'class-validator-type-match/validate-nested-match': string;
|
|
30
32
|
'class-validator-type-match/type-decorator-match': string;
|
|
31
33
|
'class-validator-type-match/definite-assignment-match': string;
|
|
34
|
+
'class-validator-type-match/dto-filename-match': string;
|
|
32
35
|
};
|
|
33
36
|
};
|
|
34
37
|
basic: {
|
|
@@ -37,6 +40,7 @@ declare const _default: {
|
|
|
37
40
|
'class-validator-type-match/decorator-type-match': string;
|
|
38
41
|
'class-validator-type-match/optional-decorator-match': string;
|
|
39
42
|
'class-validator-type-match/definite-assignment-match': string;
|
|
43
|
+
'class-validator-type-match/dto-filename-match': string;
|
|
40
44
|
};
|
|
41
45
|
};
|
|
42
46
|
};
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const optional_decorator_match_1 = __importDefault(require("./rules/optional-dec
|
|
|
7
7
|
const validate_nested_match_1 = __importDefault(require("./rules/validate-nested-match"));
|
|
8
8
|
const type_decorator_match_1 = __importDefault(require("./rules/type-decorator-match"));
|
|
9
9
|
const definite_assignment_match_1 = __importDefault(require("./rules/definite-assignment-match"));
|
|
10
|
+
const dto_filename_match_1 = __importDefault(require("./rules/dto-filename-match"));
|
|
10
11
|
module.exports = {
|
|
11
12
|
rules: {
|
|
12
13
|
'decorator-type-match': decorator_type_match_1.default,
|
|
@@ -14,6 +15,7 @@ module.exports = {
|
|
|
14
15
|
'validate-nested-match': validate_nested_match_1.default,
|
|
15
16
|
'type-decorator-match': type_decorator_match_1.default,
|
|
16
17
|
'definite-assignment-match': definite_assignment_match_1.default,
|
|
18
|
+
'dto-filename-match': dto_filename_match_1.default,
|
|
17
19
|
},
|
|
18
20
|
configs: {
|
|
19
21
|
recommended: {
|
|
@@ -24,6 +26,7 @@ module.exports = {
|
|
|
24
26
|
'class-validator-type-match/validate-nested-match': 'error',
|
|
25
27
|
'class-validator-type-match/type-decorator-match': 'error',
|
|
26
28
|
'class-validator-type-match/definite-assignment-match': 'error',
|
|
29
|
+
'class-validator-type-match/dto-filename-match': 'error',
|
|
27
30
|
},
|
|
28
31
|
},
|
|
29
32
|
strict: {
|
|
@@ -34,6 +37,7 @@ module.exports = {
|
|
|
34
37
|
'class-validator-type-match/validate-nested-match': 'error',
|
|
35
38
|
'class-validator-type-match/type-decorator-match': 'error',
|
|
36
39
|
'class-validator-type-match/definite-assignment-match': 'error',
|
|
40
|
+
'class-validator-type-match/dto-filename-match': 'error',
|
|
37
41
|
},
|
|
38
42
|
},
|
|
39
43
|
basic: {
|
|
@@ -42,6 +46,7 @@ module.exports = {
|
|
|
42
46
|
'class-validator-type-match/decorator-type-match': 'error',
|
|
43
47
|
'class-validator-type-match/optional-decorator-match': 'error',
|
|
44
48
|
'class-validator-type-match/definite-assignment-match': 'error',
|
|
49
|
+
'class-validator-type-match/dto-filename-match': 'error',
|
|
45
50
|
},
|
|
46
51
|
},
|
|
47
52
|
},
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
/**
|
|
3
|
+
* ESLint rule to ensure DTO class names match their file naming convention.
|
|
4
|
+
*
|
|
5
|
+
* This rule enforces consistent naming between DTO files and their class names:
|
|
6
|
+
* - Files like `.body.dto.ts`, `.query.dto.ts`, `.param.dto.ts` should have classes named `BodyDto`, `QueryDto`, `ParamDto`
|
|
7
|
+
* - Files like `.dto.ts` should have classes named `Dto`
|
|
8
|
+
*
|
|
9
|
+
* The rule extracts the type identifier (body, query, param, etc.) from the filename
|
|
10
|
+
* and validates that the class name follows the pattern `<Type>Dto` where `<Type>` is capitalized.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // ✅ Good - file: create-user.body.dto.ts
|
|
14
|
+
* class CreateUserBodyDto {
|
|
15
|
+
* @IsString()
|
|
16
|
+
* name!: string;
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // ❌ Bad - file: create-user.body.dto.ts
|
|
21
|
+
* class CreateUserDto { // Error: Expected CreateUserBodyDto
|
|
22
|
+
* @IsString()
|
|
23
|
+
* name!: string;
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // ✅ Good - file: user.dto.ts
|
|
28
|
+
* class UserDto {
|
|
29
|
+
* @IsString()
|
|
30
|
+
* name!: string;
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // ❌ Bad - file: user.dto.ts
|
|
35
|
+
* class User { // Error: Expected UserDto
|
|
36
|
+
* @IsString()
|
|
37
|
+
* name!: string;
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* // ✅ Good - file: update-profile.query.dto.ts
|
|
42
|
+
* class UpdateProfileQueryDto {
|
|
43
|
+
* @IsString()
|
|
44
|
+
* filter?: string;
|
|
45
|
+
* }
|
|
46
|
+
*/
|
|
47
|
+
declare const _default: ESLintUtils.RuleModule<"incorrectDtoClassName", [], unknown, ESLintUtils.RuleListener>;
|
|
48
|
+
export default _default;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
37
|
+
const path = __importStar(require("path"));
|
|
38
|
+
/**
|
|
39
|
+
* Creates an ESLint rule with proper documentation URL
|
|
40
|
+
*/
|
|
41
|
+
const createRule = utils_1.ESLintUtils.RuleCreator((name) => `https://github.com/robertlinde/eslint-plugin-class-validator-type-match#${name}`);
|
|
42
|
+
/**
|
|
43
|
+
* Capitalize first letter of a string
|
|
44
|
+
*/
|
|
45
|
+
function capitalize(str) {
|
|
46
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* ESLint rule to ensure DTO class names match their file naming convention.
|
|
50
|
+
*
|
|
51
|
+
* This rule enforces consistent naming between DTO files and their class names:
|
|
52
|
+
* - Files like `.body.dto.ts`, `.query.dto.ts`, `.param.dto.ts` should have classes named `BodyDto`, `QueryDto`, `ParamDto`
|
|
53
|
+
* - Files like `.dto.ts` should have classes named `Dto`
|
|
54
|
+
*
|
|
55
|
+
* The rule extracts the type identifier (body, query, param, etc.) from the filename
|
|
56
|
+
* and validates that the class name follows the pattern `<Type>Dto` where `<Type>` is capitalized.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // ✅ Good - file: create-user.body.dto.ts
|
|
60
|
+
* class CreateUserBodyDto {
|
|
61
|
+
* @IsString()
|
|
62
|
+
* name!: string;
|
|
63
|
+
* }
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* // ❌ Bad - file: create-user.body.dto.ts
|
|
67
|
+
* class CreateUserDto { // Error: Expected CreateUserBodyDto
|
|
68
|
+
* @IsString()
|
|
69
|
+
* name!: string;
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // ✅ Good - file: user.dto.ts
|
|
74
|
+
* class UserDto {
|
|
75
|
+
* @IsString()
|
|
76
|
+
* name!: string;
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* // ❌ Bad - file: user.dto.ts
|
|
81
|
+
* class User { // Error: Expected UserDto
|
|
82
|
+
* @IsString()
|
|
83
|
+
* name!: string;
|
|
84
|
+
* }
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* // ✅ Good - file: update-profile.query.dto.ts
|
|
88
|
+
* class UpdateProfileQueryDto {
|
|
89
|
+
* @IsString()
|
|
90
|
+
* filter?: string;
|
|
91
|
+
* }
|
|
92
|
+
*/
|
|
93
|
+
exports.default = createRule({
|
|
94
|
+
name: 'dto-filename-match',
|
|
95
|
+
meta: {
|
|
96
|
+
type: 'problem',
|
|
97
|
+
docs: {
|
|
98
|
+
description: 'Ensure DTO class names match their file naming convention. Files structured like .<type>.dto.ts should have class names like <Type>Dto',
|
|
99
|
+
},
|
|
100
|
+
messages: {
|
|
101
|
+
incorrectDtoClassName: 'Class name "{{className}}" does not match the DTO file naming convention. Expected class name to end with "{{expectedSuffix}}" based on filename "{{filename}}".',
|
|
102
|
+
},
|
|
103
|
+
schema: [],
|
|
104
|
+
fixable: undefined,
|
|
105
|
+
},
|
|
106
|
+
defaultOptions: [],
|
|
107
|
+
create(context) {
|
|
108
|
+
// Get the filename of the current file being linted
|
|
109
|
+
const filename = context.filename || context.getFilename();
|
|
110
|
+
const basename = path.basename(filename);
|
|
111
|
+
// Pattern to match DTO files: .<type>.dto.ts or .dto.ts
|
|
112
|
+
const dtoPatternWithType = /\.([a-zA-Z]+)\.dto\.ts$/;
|
|
113
|
+
const dtoPatternSimple = /\.dto\.ts$/;
|
|
114
|
+
let expectedSuffix = null;
|
|
115
|
+
// Check if file matches .<type>.dto.ts pattern (e.g., .body.dto.ts, .query.dto.ts)
|
|
116
|
+
const typeMatch = basename.match(dtoPatternWithType);
|
|
117
|
+
if (typeMatch && typeMatch[1]) {
|
|
118
|
+
const type = typeMatch[1].toLowerCase();
|
|
119
|
+
// Only process if the type is not just "dto" itself
|
|
120
|
+
// (to distinguish .body.dto.ts from .dto.ts)
|
|
121
|
+
if (type !== 'dto') {
|
|
122
|
+
expectedSuffix = `${capitalize(type)}Dto`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// Check if file matches .dto.ts pattern (e.g., user.dto.ts)
|
|
126
|
+
if (expectedSuffix === null && dtoPatternSimple.test(basename)) {
|
|
127
|
+
expectedSuffix = 'Dto';
|
|
128
|
+
}
|
|
129
|
+
// If this is not a DTO file, skip validation
|
|
130
|
+
if (expectedSuffix === null) {
|
|
131
|
+
return {};
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
/**
|
|
135
|
+
* Check class declarations to ensure they end with the expected suffix
|
|
136
|
+
*/
|
|
137
|
+
ClassDeclaration(node) {
|
|
138
|
+
// Skip anonymous classes
|
|
139
|
+
if (!node.id || !node.id.name)
|
|
140
|
+
return;
|
|
141
|
+
const className = node.id.name;
|
|
142
|
+
// Check if the class name ends with the expected suffix
|
|
143
|
+
if (!className.endsWith(expectedSuffix)) {
|
|
144
|
+
context.report({
|
|
145
|
+
node: node.id,
|
|
146
|
+
messageId: 'incorrectDtoClassName',
|
|
147
|
+
data: {
|
|
148
|
+
className,
|
|
149
|
+
expectedSuffix: expectedSuffix,
|
|
150
|
+
filename: basename,
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
});
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -31,6 +31,7 @@ module.exports = {
|
|
|
31
31
|
'class-validator-type-match/validate-nested-match': 'error',
|
|
32
32
|
'class-validator-type-match/type-decorator-match': 'error',
|
|
33
33
|
'class-validator-type-match/definite-assignment-match': 'error',
|
|
34
|
+
'class-validator-type-match/dto-filename-match': 'error',
|
|
34
35
|
},
|
|
35
36
|
};
|
|
36
37
|
```
|
|
@@ -49,7 +50,7 @@ module.exports = {
|
|
|
49
50
|
|
|
50
51
|
- **`recommended`** - All rules enabled (best for most projects)
|
|
51
52
|
- **`strict`** - All rules enabled with strict settings
|
|
52
|
-
- **`basic`** - Only core type matching rules (decorator-type-match, optional-decorator-match, definite-assignment-match)
|
|
53
|
+
- **`basic`** - Only core type matching rules (decorator-type-match, optional-decorator-match, definite-assignment-match, dto-filename-match)
|
|
53
54
|
|
|
54
55
|
```javascript
|
|
55
56
|
// Use basic preset for less strict validation
|
|
@@ -200,6 +201,66 @@ class User {
|
|
|
200
201
|
}
|
|
201
202
|
```
|
|
202
203
|
|
|
204
|
+
### `dto-filename-match`
|
|
205
|
+
|
|
206
|
+
Ensures DTO class names match their file naming convention.
|
|
207
|
+
|
|
208
|
+
**File Naming Rules:**
|
|
209
|
+
|
|
210
|
+
- Files structured like `.<type>.dto.ts` (e.g., `.body.dto.ts`, `.query.dto.ts`, `.param.dto.ts`) should have class names ending with `<Type>Dto` (e.g., `BodyDto`, `QueryDto`, `ParamDto`)
|
|
211
|
+
- Files structured like `.dto.ts` should have class names ending with `Dto`
|
|
212
|
+
|
|
213
|
+
**Examples:**
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
// File: create-user.body.dto.ts
|
|
217
|
+
import {IsString, IsEmail} from 'class-validator';
|
|
218
|
+
|
|
219
|
+
export class CreateUserBodyDto {
|
|
220
|
+
// ✅ Correct - ends with BodyDto
|
|
221
|
+
@IsString()
|
|
222
|
+
name!: string;
|
|
223
|
+
|
|
224
|
+
@IsEmail()
|
|
225
|
+
email!: string;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// File: create-user.body.dto.ts
|
|
229
|
+
export class CreateUserDto {
|
|
230
|
+
// ❌ Error: Expected class name to end with "BodyDto"
|
|
231
|
+
@IsString()
|
|
232
|
+
name!: string;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// File: update-profile.query.dto.ts
|
|
236
|
+
export class UpdateProfileQueryDto {
|
|
237
|
+
// ✅ Correct - ends with QueryDto
|
|
238
|
+
@IsString()
|
|
239
|
+
filter?: string;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// File: user.dto.ts
|
|
243
|
+
export class UserDto {
|
|
244
|
+
// ✅ Correct - ends with Dto
|
|
245
|
+
@IsString()
|
|
246
|
+
id!: string;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// File: user.dto.ts
|
|
250
|
+
export class User {
|
|
251
|
+
// ❌ Error: Expected class name to end with "Dto"
|
|
252
|
+
@IsString()
|
|
253
|
+
id!: string;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// File: get-user.param.dto.ts
|
|
257
|
+
export class GetUserParamDto {
|
|
258
|
+
// ✅ Correct - ends with ParamDto
|
|
259
|
+
@IsString()
|
|
260
|
+
userId!: string;
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
203
264
|
## Supported Decorators
|
|
204
265
|
|
|
205
266
|
### Type Validators
|