-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddresseeDto.ts
82 lines (67 loc) · 2.04 KB
/
AddresseeDto.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
import { IsInstance, IsNumber, IsString, validateSync, ValidationError } from 'class-validator';
import TypeDto from './TypeDto';
export interface AddresseeFields {
azonosito: string;
kretaAzonosito: number;
nev: string;
tipus: TypeDto;
}
export default class AddresseeDto implements Partial<AddresseeFields> {
@IsString()
private readonly id?: string;
@IsNumber()
private readonly kretaId?: number;
@IsString()
private readonly name?: string;
@IsInstance(TypeDto)
private readonly type?: TypeDto;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.id = typeof input['azonosito'] === 'string' ? input['azonosito'].trim() : undefined;
this.kretaId = typeof input['kretaAzonosito'] === 'number' ? input['kretaAzonosito'] : undefined;
this.name = typeof input['nev'] === 'string' ? input['nev'].trim() : undefined;
this.type = typeof input['tipus'] === 'object' ? new TypeDto(input['tipus']) : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get azonosito(): string | undefined {
return this.id;
}
public get kretaAzonosito(): number | undefined {
return this.kretaId;
}
public get nev(): string | undefined {
return this.name;
}
public get tipus(): TypeDto | undefined {
return this.type;
}
public get json(): AddresseeFields {
return {
azonosito: this.id,
kretaAzonosito: this.kretaId,
nev: this.name,
tipus: this.type?.json,
} as AddresseeFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<AddresseeFields> = {
azonosito: this.id,
kretaAzonosito: this.kretaId,
nev: this.name,
tipus: this.type,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof AddresseeFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}