Skip to content

Commit 4388a7d

Browse files
committed
Define empty list handling options for ListField
1 parent 789d76a commit 4388a7d

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

src/common/list-field.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import { applyDecorators } from '@nestjs/common';
2+
import { ArrayNotEmpty } from 'class-validator';
23
import { OptionalField, OptionalFieldOptions } from './optional-field';
34

4-
export type ListFieldOptions = OptionalFieldOptions;
5+
export type ListFieldOptions = OptionalFieldOptions & {
6+
/**
7+
* How should empty lists be handled?
8+
*/
9+
empty?: 'allow' | 'omit' | 'deny';
10+
};
511

612
export const ListField = (typeFn: () => any, options: ListFieldOptions) =>
713
applyDecorators(
814
OptionalField(() => [typeFn()], {
915
optional: false,
1016
...options,
1117
transform: (value) => {
12-
const deduped = value ? [...new Set(value)] : value;
13-
return options.transform ? options.transform(deduped) : value;
18+
let out = value ? [...new Set(value)] : value;
19+
out = options.empty === 'omit' && out.length === 0 ? undefined : out;
20+
return options.transform ? options.transform(out) : out;
1421
},
1522
}),
23+
...(options.empty === 'deny' ? [ArrayNotEmpty()] : []),
1624
);

src/components/project-change-request/dto/create-project-change-request.dto.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Field, InputType, ObjectType } from '@nestjs/graphql';
2+
import { NonEmptyArray } from '@seedcompany/common';
23
import { Type } from 'class-transformer';
34
import { ValidateNested } from 'class-validator';
4-
import { ID, IdField } from '~/common';
5+
import { ID, IdField, ListField } from '~/common';
56
import { ProjectChangeRequestType } from './project-change-request-type.enum';
67
import { ProjectChangeRequest } from './project-change-request.dto';
78

@@ -12,8 +13,8 @@ export abstract class CreateProjectChangeRequest {
1213
})
1314
readonly projectId: ID;
1415

15-
@Field(() => [ProjectChangeRequestType])
16-
readonly types: ProjectChangeRequestType[];
16+
@ListField(() => ProjectChangeRequestType, { empty: 'deny' })
17+
readonly types: NonEmptyArray<ProjectChangeRequestType>;
1718

1819
@Field()
1920
readonly summary: string;

src/components/project-change-request/dto/update-project-change-request.dto.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Field, InputType, ObjectType } from '@nestjs/graphql';
2+
import { NonEmptyArray } from '@seedcompany/common';
23
import { Type } from 'class-transformer';
34
import { ValidateNested } from 'class-validator';
45
import { ID, IdField, ListField, NameField, OptionalField } from '~/common';
@@ -11,8 +12,8 @@ export abstract class UpdateProjectChangeRequest {
1112
@IdField()
1213
readonly id: ID;
1314

14-
@ListField(() => ProjectChangeRequestType, { optional: true })
15-
readonly types?: readonly ProjectChangeRequestType[];
15+
@ListField(() => ProjectChangeRequestType, { optional: true, empty: 'deny' })
16+
readonly types?: NonEmptyArray<ProjectChangeRequestType>;
1617

1718
@NameField({ optional: true })
1819
readonly summary?: string;

0 commit comments

Comments
 (0)