-
Notifications
You must be signed in to change notification settings - Fork 1
/
HomeworkGetDto.ts
72 lines (59 loc) · 1.8 KB
/
HomeworkGetDto.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
61
62
63
64
65
66
67
68
69
70
71
72
import { IsDate, IsNumber, IsOptional, IsString, validateSync, ValidationError } from 'class-validator';
export interface HomeworkGetFields {
HataridoDatuma?: Date;
Szoveg?: string;
Id: number;
}
export default class HomeworkGetDto implements Partial<HomeworkGetFields> {
@IsOptional()
@IsDate()
private readonly deadline?: Date;
@IsOptional()
@IsString()
private readonly description?: string;
@IsNumber()
private readonly id?: number;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.deadline = typeof input['HataridoDatuma'] === 'string' ? new Date(input['HataridoDatuma']) : input['HataridoDatuma'];
this.description = typeof input['Szoveg'] === 'string' ? input['Szoveg'] : undefined;
this.id = typeof input['Id'] === 'number' ? input['Id'] : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get HataridoDatuma(): Date | undefined {
return this.deadline;
}
public get Szoveg(): string | undefined {
return this.description;
}
public get Id(): number | undefined {
return this.id;
}
public get json(): HomeworkGetFields {
return {
HataridoDatuma: this.deadline,
Id: this.id,
Szoveg: this.description,
} as HomeworkGetFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<HomeworkGetFields> = {
HataridoDatuma: this.deadline,
Szoveg: this.description,
Id: this.id,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof HomeworkGetFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}