-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBase.js
More file actions
48 lines (37 loc) · 1.33 KB
/
Base.js
File metadata and controls
48 lines (37 loc) · 1.33 KB
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
const LIVR = require('livr');
const Exception = require('./Exception');
class Base {
constructor(args) {
if (!args.context) throw new Error('CONTEXT_REQUIRED');
this.context = args.context;
}
run(params) {
return this.validate(params).then(cleanParams => {
return this.execute(cleanParams)
});
}
validate(data) {
// Feel free to override this method if you need dynamic validation
// Cache validator
const validator = this.constructor.validator || new LIVR.Validator(this.constructor.validationRules).prepare();
this.constructor.validator = validator;
return this._doValidationWithValidator( data, validator );
}
doValidation(data, rules) {
// You can use this in overriden "validate" method
const validator = new LIVR.Validator(rules).prepare();
return this._doValidationWithValidator(data, validator);
}
_doValidationWithValidator(data, validator) {
const result = validator.validate(data);
if (!result) {
const exception = new Exception({
code : 'FORMAT_ERROR',
fields : validator.getErrors()
});
return Promise.reject(exception);
}
return Promise.resolve(result);
}
}
module.exports = Base;