eslint-plugin-class-validator-type-match 4.1.7 → 4.1.9

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.
@@ -220,6 +220,20 @@ exports.default = createRule({
220
220
  return false;
221
221
  });
222
222
  };
223
+ /**
224
+ * Helper: Check if property has @ValidateIf decorator
225
+ */
226
+ const hasValidateIfDecorator = (node) => {
227
+ if (!node.decorators || node.decorators.length === 0)
228
+ return false;
229
+ return node.decorators.some((decorator) => {
230
+ const expr = decorator.expression;
231
+ if (expr.type === 'CallExpression' && expr.callee.type === 'Identifier') {
232
+ return expr.callee.name === 'ValidateIf';
233
+ }
234
+ return false;
235
+ });
236
+ };
223
237
  /**
224
238
  * Helper: Create fixer to add ? to property
225
239
  */
@@ -362,7 +376,7 @@ exports.default = createRule({
362
376
  /**
363
377
  * Priority 5: Property is optional (?) but missing @IsOptional() decorator
364
378
  */
365
- if (isOptionalProperty && !hasDecorator) {
379
+ if (isOptionalProperty && !hasDecorator && !hasValidateIfDecorator(node)) {
366
380
  issues.push({
367
381
  messageId: 'missingOptionalDecorator',
368
382
  });
@@ -371,7 +385,7 @@ exports.default = createRule({
371
385
  * Priority 6: Type includes undefined but missing @IsOptional() and not optional
372
386
  * Only check if strictNullChecks is enabled
373
387
  */
374
- if (strictNullChecks && hasUndefined && !hasDecorator && !isOptionalProperty) {
388
+ if (strictNullChecks && hasUndefined && !hasDecorator && !isOptionalProperty && !hasValidateIfDecorator(node)) {
375
389
  issues.push({
376
390
  messageId: 'undefinedUnionWithoutDecorator',
377
391
  });
@@ -163,6 +163,13 @@ export declare function hasEachOption(decorator: TSESTree.Decorator): boolean;
163
163
  * Returns false for decorators without parentheses or without the each option.
164
164
  */
165
165
  export declare function hasValidateNestedEachOption(decorator: TSESTree.Decorator): boolean;
166
+ /**
167
+ * Checks if a type is an object-producing utility type or Record.
168
+ * These types always produce object types and should be valid with @IsObject.
169
+ *
170
+ * Includes: Record, Partial, Required, Pick, Omit, Readonly
171
+ */
172
+ export declare function isObjectUtilityType(typeNode: TSESTree.TypeNode): boolean;
166
173
  /**
167
174
  * Validates if a decorator matches the TypeScript type annotation.
168
175
  * Handles special cases like @IsEnum validation, nullable unions, and utility types.
@@ -22,6 +22,7 @@ exports.isEnumArgumentArraySubset = isEnumArgumentArraySubset;
22
22
  exports.getTypeDecoratorClassName = getTypeDecoratorClassName;
23
23
  exports.hasEachOption = hasEachOption;
24
24
  exports.hasValidateNestedEachOption = hasValidateNestedEachOption;
25
+ exports.isObjectUtilityType = isObjectUtilityType;
25
26
  exports.checkTypeMatch = checkTypeMatch;
26
27
  /**
27
28
  * Decorators that don't enforce specific types and can work with any type.
@@ -780,6 +781,21 @@ function hasEachOption(decorator) {
780
781
  function hasValidateNestedEachOption(decorator) {
781
782
  return hasEachOption(decorator);
782
783
  }
784
+ /**
785
+ * Checks if a type is an object-producing utility type or Record.
786
+ * These types always produce object types and should be valid with @IsObject.
787
+ *
788
+ * Includes: Record, Partial, Required, Pick, Omit, Readonly
789
+ */
790
+ function isObjectUtilityType(typeNode) {
791
+ const unwrapped = unwrapReadonlyOperator(typeNode);
792
+ if (unwrapped.type !== 'TSTypeReference') {
793
+ return false;
794
+ }
795
+ const typeName = getTypeReferenceName(unwrapped.typeName);
796
+ const objectUtilityTypes = ['Record', 'Partial', 'Required', 'Pick', 'Omit', 'Readonly'];
797
+ return objectUtilityTypes.includes(typeName);
798
+ }
783
799
  /**
784
800
  * Validates if a decorator matches the TypeScript type annotation.
785
801
  * Handles special cases like @IsEnum validation, nullable unions, and utility types.
@@ -790,6 +806,10 @@ function checkTypeMatch(decorator, typeAnnotation, actualType, checker, esTreeNo
790
806
  if (!expectedTypes || expectedTypes.length === 0) {
791
807
  return true;
792
808
  }
809
+ // Special handling for @IsObject and @IsNotEmptyObject - utility types that produce objects
810
+ if ((decorator === 'IsObject' || decorator === 'IsNotEmptyObject') && isObjectUtilityType(typeAnnotation)) {
811
+ return true;
812
+ }
793
813
  // Special handling for @IsEnum
794
814
  if (decorator === 'IsEnum') {
795
815
  // @IsEnum should work with:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-class-validator-type-match",
3
- "version": "4.1.7",
3
+ "version": "4.1.9",
4
4
  "description": "ESLint plugin to ensure class-validator decorators match TypeScript type annotations",
5
5
  "keywords": [
6
6
  "eslint",
@@ -37,25 +37,25 @@
37
37
  "eslint": "^9.0.0"
38
38
  },
39
39
  "dependencies": {
40
- "@typescript-eslint/utils": "8.53.0"
40
+ "@typescript-eslint/utils": "8.56.1"
41
41
  },
42
42
  "devDependencies": {
43
- "@commitlint/cli": "20.3.1",
44
- "@commitlint/config-conventional": "20.3.1",
45
- "@eslint/js": "9.39.2",
46
- "@typescript-eslint/parser": "8.53.0",
47
- "@typescript-eslint/rule-tester": "8.53.0",
48
- "@vitest/coverage-v8": "4.0.17",
49
- "eslint": "9.39.2",
50
- "globals": "17.0.0",
43
+ "@commitlint/cli": "20.4.2",
44
+ "@commitlint/config-conventional": "20.4.2",
45
+ "@eslint/js": "9.39.3",
46
+ "@typescript-eslint/parser": "8.56.1",
47
+ "@typescript-eslint/rule-tester": "8.56.1",
48
+ "@vitest/coverage-v8": "4.0.18",
49
+ "eslint": "9.39.3",
50
+ "globals": "17.4.0",
51
51
  "husky": "9.1.7",
52
- "lint-staged": "16.2.7",
53
- "prettier": "3.8.0",
52
+ "lint-staged": "16.3.1",
53
+ "prettier": "3.8.1",
54
54
  "typescript": "5.9.3",
55
- "typescript-eslint": "8.53.0",
56
- "vitest": "4.0.17"
55
+ "typescript-eslint": "8.56.1",
56
+ "vitest": "4.0.18"
57
57
  },
58
58
  "engines": {
59
- "node": "v25.3.0"
59
+ "node": "25.7.0"
60
60
  }
61
61
  }