-
Notifications
You must be signed in to change notification settings - Fork 1
/
StateDto.ts
92 lines (75 loc) · 2.2 KB
/
StateDto.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { IsString, validateSync, ValidationError } from 'class-validator';
export interface StateFields {
kod: string;
leiras: string;
azonosito: string;
nev: string;
rovidNev: string;
}
export default class StateDto implements Partial<StateFields> {
@IsString()
private readonly code?: string;
@IsString()
private readonly description?: string;
@IsString()
private readonly id?: string;
@IsString()
private readonly name?: string;
@IsString()
private readonly shortName?: string;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.code = typeof input['kod'] === 'string' ? input['kod'].trim() : undefined;
this.description = typeof input['leiras'] === 'string' ? input['leiras'].trim() : undefined;
this.id = typeof input['azonosito'] === 'string' ? input['azonosito'].trim() : undefined;
this.name = typeof input['nev'] === 'string' ? input['nev'].trim() : undefined;
this.shortName = typeof input['rovidNev'] === 'string' ? input['rovidNev'].trim() : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get kod(): string | undefined {
return this.code;
}
public get leiras(): string | undefined {
return this.description;
}
public get azonosito(): string | undefined {
return this.id;
}
public get nev(): string | undefined {
return this.name;
}
public get rovidNev(): string | undefined {
return this.shortName;
}
public get json(): StateFields {
return {
azonosito: this.id,
kod: this.code,
leiras: this.description,
nev: this.name,
rovidNev: this.shortName,
} as StateFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<StateFields> = {
kod: this.code,
leiras: this.description,
azonosito: this.id,
nev: this.name,
rovidNev: this.shortName,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof StateFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}