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