-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
EntityValidator.ts
152 lines (116 loc) · 4.46 KB
/
EntityValidator.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { AnyEntity, Dictionary, EntityData, EntityMetadata, EntityProperty, FilterQuery } from '../typings';
import { ReferenceType } from '../enums';
import { Utils } from '../utils/Utils';
import { ValidationError } from '../errors';
export class EntityValidator {
constructor(private strict: boolean) { }
validate<T extends AnyEntity<T>>(entity: T, payload: any, meta: EntityMetadata): void {
meta.props.forEach(prop => {
if (prop.inherited) {
return;
}
if ([ReferenceType.ONE_TO_MANY, ReferenceType.MANY_TO_MANY].includes(prop.reference)) {
this.validateCollection(entity, prop);
}
const SCALAR_TYPES = ['string', 'number', 'boolean', 'Date'];
if (prop.reference !== ReferenceType.SCALAR || !SCALAR_TYPES.includes(prop.type)) {
return;
}
const newValue = this.validateProperty(prop, this.getValue(payload, prop), entity);
if (this.getValue(payload, prop) === newValue) {
return;
}
this.setValue(payload, prop, newValue);
if (entity[prop.name]) {
entity[prop.name] = payload[prop.name];
}
});
}
validateProperty<T extends AnyEntity<T>>(prop: EntityProperty, givenValue: any, entity: T) {
if (givenValue === null || givenValue === undefined) {
return givenValue;
}
const expectedType = prop.type.toLowerCase();
let givenType = Utils.getObjectType(givenValue);
let ret = givenValue;
if (!this.strict) {
ret = this.fixTypes(expectedType, givenType, givenValue);
givenType = Utils.getObjectType(ret);
}
if (givenType !== expectedType) {
throw ValidationError.fromWrongPropertyType(entity, prop.name, expectedType, givenType, givenValue);
}
return ret;
}
validateParams(params: any, type = 'search condition', field?: string): void {
if (Utils.isPrimaryKey(params) || Utils.isEntity(params)) {
return;
}
if (Array.isArray(params)) {
return params.forEach((item: any) => this.validateParams(item, type, field));
}
if (Utils.isPlainObject(params)) {
Object.keys(params).forEach(k => {
this.validateParams(params[k], type, k);
});
}
}
validatePrimaryKey<T extends AnyEntity<T>>(entity: EntityData<T>, meta: EntityMetadata): void {
const pkExists = meta.primaryKeys.every(pk => Utils.isDefined(entity[pk], true)) || Utils.isDefined(entity[meta.serializedPrimaryKey], true);
if (!entity || !pkExists) {
throw ValidationError.fromMergeWithoutPK(meta);
}
}
validateEmptyWhere<T extends AnyEntity<T>>(where: FilterQuery<T>): void {
if (Utils.isEmpty(where)) {
throw new Error(`You cannot call 'EntityManager.findOne()' with empty 'where' parameter`);
}
}
private getValue(o: Dictionary, prop: EntityProperty) {
if (prop.embedded && prop.embedded[0] in o) {
return o[prop.embedded[0]][prop.embedded[1]];
}
return o[prop.name];
}
private setValue(o: Dictionary, prop: EntityProperty, v: any) {
/* istanbul ignore next */
if (prop.embedded && prop.embedded[0] in o) {
return o[prop.embedded[0]][prop.embedded[1]] = v;
}
o[prop.name] = v;
}
private validateCollection<T extends AnyEntity<T>>(entity: T, prop: EntityProperty): void {
if (entity.__helper!.__initialized && !entity[prop.name as keyof T]) {
throw ValidationError.fromCollectionNotInitialized(entity, prop);
}
}
private fixTypes(expectedType: string, givenType: string, givenValue: any): any {
if (expectedType === 'date' && ['string', 'number'].includes(givenType)) {
givenValue = this.fixDateType(givenValue);
}
if (expectedType === 'number' && givenType === 'string') {
givenValue = this.fixNumberType(givenValue);
}
if (expectedType === 'boolean' && givenType === 'number') {
givenValue = this.fixBooleanType(givenValue);
}
return givenValue;
}
private fixDateType(givenValue: string): Date | string {
let date: Date;
if (Utils.isString(givenValue) && givenValue.match(/^-?\d+(\.\d+)?$/)) {
date = new Date(+givenValue);
} else {
date = new Date(givenValue);
}
return date.toString() !== 'Invalid Date' ? date : givenValue;
}
private fixNumberType(givenValue: string): number | string {
const num = +givenValue;
return '' + num === givenValue ? num : givenValue;
}
private fixBooleanType(givenValue: number): boolean | number {
const bool = !!givenValue;
return +bool === givenValue ? bool : givenValue;
}
}