'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
74
75
76
77
78
79
80
81
82
var Class = require('./Class');
var keys = require('./keys');
var safeGet = require('./safeGet');
var isFn = require('./isFn');
var isUndef = require('./isUndef');
var isNum = require('./isNum');
var isStr = require('./isStr');
var isBool = require('./isBool');
exports = Class(
    {
        className: 'Validator',
        initialize: function(options) {
            this._options = options;
            this._optKeys = keys(options);
        },
        validate: function(obj) {
            obj = obj || {};
            var options = this._options;
            var objKeys = this._optKeys;
            for (var i = 0, len = objKeys.length; i < len; i++) {
                var key = objKeys[i];
                var result = this._validateVal(
                    safeGet(obj, key),
                    options[key],
                    key
                );
                if (result !== true) return result;
            }
            return true;
        },
        _validateVal: function(val, rules, objKey) {
            var plugins = exports.plugins;
            if (isFn(rules)) return rules(val);
            var ruleKeys = keys(rules);
            for (var i = 0, len = ruleKeys.length; i < len; i++) {
                var key = ruleKeys[i];
                var config = rules[key];
                var result = true;
                if (isFn(config)) result = config(val, objKey);
                var plugin = plugins[key];
                if (plugin) result = plugin(val, objKey, config);
                if (result !== true) return result;
            }
            return true;
        }
    },
    {
        plugins: {
            required: function(val, key, config) {
                if (config && isUndef(val)) return key + ' is required';
                return true;
            },
            number: function(val, key, config) {
                if (config && !isUndef(val) && !isNum(val))
                    return key + ' should be a number';
                return true;
            },
            boolean: function(val, key, config) {
                if (config && !isUndef(val) && !isBool(val))
                    return key + ' should be a boolean';
                return true;
            },
            string: function(val, key, config) {
                if (config && !isUndef(val) && !isStr(val))
                    return key + ' should be a string';
                return true;
            },
            regexp: function(val, key, config) {
                if (isStr(val) && !config.test(val))
                    return (
                        key + ' should match given regexp ' + config.toString()
                    );
                return true;
            }
        },
        addPlugin: function(name, plugin) {
            exports.plugins[name] = plugin;
        }
    }
);
 
module.exports = exports;