-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
ValidationError.ts
73 lines (53 loc) · 3.07 KB
/
ValidationError.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
import { EntityMetadata, EntityProperty, IEntity } from '../decorators';
export class ValidationError extends Error {
constructor(message: string) {
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
}
static fromWrongPropertyType(entity: IEntity, property: string, expectedType: string, givenType: string, givenValue: string): ValidationError {
const entityName = entity.constructor.name;
const msg = `Trying to set ${entityName}.${property} of type '${expectedType}' to '${givenValue}' of type '${givenType}'`;
return new ValidationError(msg);
}
static fromEntityInsteadOfIdentifier(entity: IEntity, type: string, field?: string): ValidationError {
const entityName = entity.constructor.name;
const msg = `${entityName} entity provided in ${type}${field ? ` in field '${field}'` : ''}. Please provide identifier instead.`;
return new ValidationError(msg);
}
static fromCollectionNotInitialized(entity: IEntity, prop: EntityProperty): ValidationError {
const entityName = entity.constructor.name;
const msg = `${entityName}.${prop.name} is not initialized, define it as '${prop.name} = new Collection<${prop.type}>(this);'`;
return new ValidationError(msg);
}
static fromMissingPrimaryKey(meta: EntityMetadata): ValidationError {
return new ValidationError(`${meta.name} entity is missing @PrimaryKey()`);
}
static fromWrongReference(meta: EntityMetadata, prop: EntityProperty, key: keyof EntityProperty, owner?: EntityProperty): ValidationError {
if (owner) {
return ValidationError.fromMessage(meta, prop, `has wrong '${key}' reference type: ${owner.type} instead of ${meta.name}`);
}
return ValidationError.fromMessage(meta, prop, `has unknown '${key}' reference: ${prop.type}.${prop[key]}`);
}
static fromWrongTypeDefinition(meta: EntityMetadata, prop: EntityProperty): ValidationError {
if (!prop.type) {
return ValidationError.fromMessage(meta, prop, `is missing type definition`);
}
return ValidationError.fromMessage(meta, prop, `has unknown type: ${prop.type}`);
}
static fromWrongOwnership(meta: EntityMetadata, prop: EntityProperty, key: keyof EntityProperty): ValidationError {
const type = key === 'inversedBy' ? 'owning' : 'inverse';
const other = key === 'inversedBy' ? 'mappedBy' : 'inversedBy';
return new ValidationError(`Both ${meta.name}.${prop.name} and ${prop.type}.${prop[key]} are defined as ${type} sides, use ${other} on one of them`);
}
static fromMissingOwnership(meta: EntityMetadata, prop: EntityProperty): ValidationError {
return ValidationError.fromMessage(meta, prop, `needs to have one of 'owner', 'mappedBy' or 'inversedBy' attributes`);
}
static fromMergeWithoutPK(meta: EntityMetadata): void {
throw new Error(`You cannot merge entity '${meta.name}' without identifier!`);
}
private static fromMessage(meta: EntityMetadata, prop: EntityProperty, message: string): ValidationError {
return new ValidationError(`${meta.name}.${prop.name} ${message}`);
}
}