Skip to content

Commit b549566

Browse files
feat: rename the whitelisted property rule and add documentation
Renamed "no-whitelisted-properties" to be "all-properties-are-whitelisted"
1 parent fc0d689 commit b549566

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Preventing bugs
2828
- param-decorator-name-matches-route-param
2929
- validate-nested-of-array-should-set-each
3030
- validated-non-primitive-property-needs-type-decorator
31+
- all-properties-are-whitelisted
3132

3233
Security
3334

@@ -114,6 +115,45 @@ Disable a single rule with the full name e.g. in your eslint configuration...
114115

115116
## Rule Details
116117

118+
### Rule: all-properties-are-whitelisted
119+
120+
You should forbid non-whitelisted properties in your DTOs.
121+
122+
If you have a DTO that has one property with a class-validator decorator, it's very unlikely that the same DTO will have any properties without a decorator - i.e. ALL DTO properties should be validated or explicitly whitelisted with `@Allow()`.
123+
124+
This rule will flag any properties that are not whitelisted as expected because it's probably a mistake.
125+
126+
This PASSES - all properties are decorated
127+
128+
```ts
129+
export class CreateOrganisationDto {
130+
@ApiProperty({type: Person, isArray: true})
131+
@ValidateNested({each: true})
132+
members!: MyClass[];
133+
134+
@ApiProperty()
135+
@Allow()
136+
otherProperty!: MyClass;
137+
138+
@ApiProperty()
139+
@IsString()
140+
someStringProperty!: string;
141+
}
142+
```
143+
144+
This FAILS - one property here is missing a validation decorator. This is likely a mistake.
145+
146+
```ts
147+
export class CreateOrganisationDto {
148+
@ApiProperty({type: Person, isArray: true})
149+
@ValidateNested({each: true})
150+
members!: MyClass[];
151+
152+
@ApiProperty()
153+
otherProperty!: MyClass;
154+
}
155+
```
156+
117157
### Rule: validate-nested-of-array-should-set-each
118158

119159
If you use the `@ValidateNested` decorator you should specify the `{each: true}` option if the property is an array.

src/configs/recommended.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ export = {
2727
"error",
2828
"@darraghor/nestjs-typed/validate-nested-of-array-should-set-each":
2929
"error",
30+
"@darraghor/nestjs-typed/all-properties-are-whitelisted": "error",
3031
},
3132
};

src/rules/noWhitelistedProperties/noWhitelistedProperties.spec.ts renamed to src/rules/allPropertiesAreWhitelisted/allPropertiesAreWhitelisted.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {RuleTester} from "@typescript-eslint/experimental-utils/dist/eslint-utils";
2-
import rule from "./noWhitelistedProperties";
2+
import rule from "./allPropertiesAreWhitelisted";
33

44
const ruleTester = new RuleTester({
55
parser: "@typescript-eslint/parser",
66
});
77

8-
ruleTester.run("no-whitelisted-properties", rule, {
8+
ruleTester.run("all-properties-are-whitelisted", rule, {
99
valid: [
1010
{
1111
code: `

src/rules/noWhitelistedProperties/noWhitelistedProperties.ts renamed to src/rules/allPropertiesAreWhitelisted/allPropertiesAreWhitelisted.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ const CLASS_VALIDATOR_DECORATOR_NAMES = new Set(
88
);
99

1010
const rule = createRule({
11-
name: "no-whitelisted-properties",
11+
name: "all-properties-are-whitelisted",
1212
meta: {
1313
docs: {
14-
description: "Enforce no properties are whitelisted",
14+
description: "Enforce all properties are whitelisted",
1515
recommended: "error",
1616
requiresTypeChecking: false,
1717
},
1818
messages: {
1919
"missing-property-decorator":
20-
"Property has no class-validator decorator (use @Allow so it would not be whitelisted)",
20+
"Property has no class-validator decorator (use @Allow() if you don't need a validation)",
2121
},
2222
type: "problem",
2323
schema: {},

src/rules/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import shouldSpecifyForbidUnknownValues from "./shouldSpecifyForbidUnknownValues
99
import parameterDecoratorNameMatchesRouteParam from "./paramDecoratorNameMatchesRouteParam/paramDecoratorNameMatchesRouteParam";
1010
import validateNonPrimitiveNeedsDecorators from "./validate-non-primitves-needs-type-decorator/validateNonPrimitiveNeedsDecorators";
1111
import validateNestedOfArrayShouldSetEach from "./validateNestedOfArrayShouldSetEach/validateNestedOfArrayShouldSetEach";
12-
import noWhitelistedProperties from "./noWhitelistedProperties/noWhitelistedProperties";
12+
import allPropertiesAreWhitelisted from "./allPropertiesAreWhitelisted/allPropertiesAreWhitelisted";
1313

1414
const allRules = {
1515
"api-property-matches-property-optionality":
@@ -30,7 +30,7 @@ const allRules = {
3030
validateNonPrimitiveNeedsDecorators,
3131
"validate-nested-of-array-should-set-each":
3232
validateNestedOfArrayShouldSetEach,
33-
"no-whitelisted-properties": noWhitelistedProperties,
33+
"all-properties-are-whitelisted": allPropertiesAreWhitelisted,
3434
};
3535

3636
export default allRules;

0 commit comments

Comments
 (0)