Skip to content

Commit 95f5c43

Browse files
feat: fix isArray detection for optional properties
1 parent e581d50 commit 95f5c43

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/rules/validate-non-primitves-needs-type-decorator/validateNonPrimitiveNeedsDecorators.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ const ruleTester = new RuleTester({
1414

1515
ruleTester.run("validated-non-primitive-property-needs-type-decorator", rule, {
1616
valid: [
17+
{
18+
// is a primitive type array - doesn't need type (from https://github.com/darraghoriordan/eslint-plugin-nestjs-typed/issues/22)
19+
code: `
20+
class ExampleDto {
21+
@ApiProperty({
22+
isArray: true,
23+
})
24+
@Allow()
25+
exampleProperty!: string[];
26+
}
27+
`,
28+
},
29+
{
30+
// is an OPTIONAL primitive type array - doesn't need type (from https://github.com/darraghoriordan/eslint-plugin-nestjs-typed/issues/22)
31+
code: `
32+
class ExampleDto {
33+
@ApiPropertyOptional({
34+
isArray: true,
35+
})
36+
@Allow()
37+
exampleProperty?: string[];
38+
}
39+
`,
40+
},
1741
{
1842
// scenario from https://github.com/darraghoriordan/eslint-plugin-nestjs-typed/issues/21
1943
code: `
@@ -83,7 +107,7 @@ ruleTester.run("validated-non-primitive-property-needs-type-decorator", rule, {
83107
`,
84108
},
85109
{
86-
// is an array - should have type
110+
// is an array - should have type and has it so pass!
87111
code: `
88112
export class CreateOrganisationDto {
89113
@ApiProperty({ type: Person, isArray: true })
@@ -103,6 +127,23 @@ ruleTester.run("validated-non-primitive-property-needs-type-decorator", rule, {
103127
@ValidateNested({each:true})
104128
members!: Foo[];
105129
}
130+
`,
131+
errors: [
132+
{
133+
messageId: "shouldUseTypeDecorator",
134+
},
135+
],
136+
},
137+
{
138+
// is an OPTIONAL array - should have type
139+
code: `
140+
export class Foo {}
141+
142+
export class CreateOrganisationDto {
143+
@ApiProperty({ type: Person, isArray: true })
144+
@ValidateNested({each:true})
145+
members?: Foo[];
146+
}
106147
`,
107148
errors: [
108149
{

src/rules/validate-non-primitves-needs-type-decorator/validateNonPrimitiveNeedsDecorators.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {createRule} from "../../utils/createRule";
44
import {typedTokenHelpers} from "../../utils/typedTokenHelpers";
55
import {getParserServices} from "@typescript-eslint/experimental-utils/dist/eslint-utils";
66
import {classValidatorDecorators} from "../../utils/classValidatorDecorators";
7-
//import util from "util";
87

98
const primitiveTypes = new Set([
109
AST_NODE_TYPES.TSStringKeyword,

src/utils/typedTokenHelpers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ export const typedTokenHelpers = {
2323
parserService: ParserServices,
2424
checker: ts.TypeChecker
2525
): boolean {
26+
if (
27+
(node as TSESTree.PropertyDefinition)?.typeAnnotation
28+
?.typeAnnotation?.type === TSESTree.AST_NODE_TYPES.TSArrayType
29+
) {
30+
return true;
31+
}
32+
2633
const nodeType = this.getNodeType(node, parserService, checker);
34+
if (checker.isArrayType(nodeType)) {
35+
return true;
36+
}
2737
for (const t of unionTypeParts(nodeType)) {
2838
if (!checker.isArrayType(t)) {
2939
return false;

0 commit comments

Comments
 (0)