-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadMessageRequestDto.ts
60 lines (49 loc) · 1.75 KB
/
ReadMessageRequestDto.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { IsArray, IsBoolean, IsNumber, validateSync, ValidationError } from 'class-validator';
export interface ReadMessageRequestFields {
postaladaElemAzonositoLista: Array<number>;
isOlvasott: boolean;
}
export default class ReadMessageRequestDto implements Partial<ReadMessageRequestFields> {
@IsArray()
@IsNumber({}, { each: true })
private readonly mailBoxItemId?: Array<number>;
@IsBoolean()
private readonly readByUser?: boolean;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.mailBoxItemId = Array.isArray(input['postaladaElemAzonositoLista']) ? input['postaladaElemAzonositoLista'] : undefined;
this.readByUser = typeof input['isOlvasott'] === 'boolean' ? input['isOlvasott'] : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get postaladaElemAzonositoLista(): Array<number> | undefined {
return this.mailBoxItemId;
}
public get isOlvasott(): boolean | undefined {
return this.readByUser;
}
public get json(): ReadMessageRequestFields {
return {
isOlvasott: this.readByUser,
postaladaElemAzonositoLista: this.mailBoxItemId,
} as ReadMessageRequestFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<ReadMessageRequestFields> = {
isOlvasott: this.readByUser,
postaladaElemAzonositoLista: this.mailBoxItemId,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof ReadMessageRequestFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}