-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddresseeTypeDto.ts
97 lines (80 loc) · 2.35 KB
/
AddresseeTypeDto.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
93
94
95
96
97
import { IsNumber, IsOptional, IsString, validateSync, ValidationError } from 'class-validator';
export interface AddresseeTypeFields {
kod?: string;
leiras?: string;
azonosito?: number;
nev?: string;
rovidNev?: string;
}
export default class AddresseeTypeDto implements Partial<AddresseeTypeFields> {
@IsOptional()
@IsString()
private readonly code?: string;
@IsOptional()
@IsString()
private readonly description?: string;
@IsOptional()
@IsNumber()
private readonly id?: number;
@IsOptional()
@IsString()
private readonly name?: string;
@IsOptional()
@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'] === 'number' ? input['azonosito'] : 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(): number | undefined {
return this.id;
}
public get nev(): string | undefined {
return this.name;
}
public get rovidNev(): string | undefined {
return this.shortName;
}
public get json(): AddresseeTypeFields {
return {
azonosito: this.id,
kod: this.code,
leiras: this.description,
nev: this.name,
rovidNev: this.shortName,
} as AddresseeTypeFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<AddresseeTypeFields> = {
azonosito: this.id,
kod: this.code,
leiras: this.description,
nev: this.name,
rovidNev: this.shortName,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof AddresseeTypeFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}