-
Notifications
You must be signed in to change notification settings - Fork 1
/
insert-model-validator.ts
53 lines (44 loc) · 1.8 KB
/
insert-model-validator.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
import {
ObjectValidator, ValidationMetadata, Validator, DefinedValidator,
UndefinedValidator
} from 'bsy-validation';
import { metaFactory, ColumnMetadata } from '../../metadata/';
import { ModelValidator } from '../';
/**
* Specialized validator that's used to validate objects against a class's
* validation schema and column metadata prior to creation. For example, when
* inserting a record non-nullable fields must be defined and non-null, and
* generated fields, like auto-incrementing primary keys, must not be defined.
* This differs from mutation (updating and deleting).
*/
export class InsertModelValidator extends ModelValidator {
/**
* Initialize with a [[ModelValidator]] instance. The [[ModelValidator]]
* handles the majority of the validation, while this class is concerned with
* verifying the presence of non-nullable columns.
*/
constructor(
protected objectValidator: ObjectValidator = new ModelValidator()) {
super(objectValidator);
}
/**
* Generate validation metadata (see bsy-validation) for the Entity.
*/
getValidationMetadata(Entity: {new(): any}): ValidationMetadata[] {
const colMeta = metaFactory
.getColumnStore()
.getColumnMetadata(Entity);
return colMeta
.map((meta: ColumnMetadata) => {
const validators: Validator[] = [];
// A non-nullable column is required in insert unless the column has a
// default or is generated. Generated columns, like auto-incrementing
// primary keys, cannot be set manually on insert.
if (meta.isGenerated)
validators.push(new UndefinedValidator());
else if (!meta.isNullable && !meta.hasDefault)
validators.push(new DefinedValidator());
return new ValidationMetadata(Entity, meta.mapTo, validators);
});
}
}