1 | var Validator = require('validator').Validator;
|
2 |
|
3 | Validator.prototype.error = function (msg) {
|
4 | this._errors.push(msg);
|
5 | }
|
6 |
|
7 | Validator.prototype.getErrors = function () {
|
8 | return this._errors;
|
9 | }
|
10 |
|
11 | var ValidateAble = module.exports = function () {
|
12 |
|
13 | this.validateFn = function () {};
|
14 |
|
15 | this.runValidate = function (cb) {
|
16 | var failed = false;
|
17 | var validator = new Validator();
|
18 |
|
19 | var check = function (item, msg) {
|
20 | return validator.check(item, msg);
|
21 | }
|
22 |
|
23 | try {
|
24 |
|
25 | this.validateFn && this.validateFn(check, this);
|
26 | } catch (e) {
|
27 | failed = true;
|
28 | cb([e.toString()], null);
|
29 | }
|
30 |
|
31 | var errors = validator.getErrors();
|
32 | if (errors && errors.length > 0) {
|
33 | failed = true;
|
34 | cb(errors, null);
|
35 | }
|
36 |
|
37 | return !failed
|
38 | };
|
39 |
|
40 | };
|