-
Notifications
You must be signed in to change notification settings - Fork 1
/
StudentDto.ts
59 lines (48 loc) · 1.49 KB
/
StudentDto.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
import { IsNumber, IsString, validateSync, ValidationError } from 'class-validator';
export interface StudentFields {
TanuloId: number;
TanuloNev: string;
}
export default class StudentDto implements Partial<StudentFields> {
@IsNumber()
private readonly studentId?: number;
@IsString()
private readonly studentName?: string;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.studentId = typeof input['TanuloId'] === 'number' ? input['TanuloId'] : undefined;
this.studentName = typeof input['TanuloNev'] === 'string' ? input['TanuloNev'].trim() : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get TanuloId(): number | undefined {
return this.studentId;
}
public get TanuloNev(): string | undefined {
return this.studentName;
}
public get json(): StudentFields {
return {
TanuloId: this.studentId,
TanuloNev: this.studentName,
} as StudentFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<StudentFields> = {
TanuloId: this.studentId,
TanuloNev: this.studentName,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof StudentFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}