UNPKG

843 BJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('./Validator');
7
8module.exports = class IdValidator extends Base {
9
10 constructor (config) {
11 super({
12 normalize: true,
13 skipOnEmpty: false,
14 ...config
15 });
16 }
17
18 getMessage () {
19 return this.createMessage(this.message, 'Invalid ID');
20 }
21
22 validateAttr (attr, model) {
23 let value = model.get(attr);
24 if (this.isEmptyValue(value)) {
25 return model.set(attr, null);
26 }
27 value = model.getDb().normalizeId(value);
28 if (value === null) {
29 this.addError(model, attr, this.getMessage());
30 } else if (this.normalize) {
31 model.set(attr, value);
32 }
33 }
34};
\No newline at end of file