-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (129 loc) · 3.47 KB
/
index.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var _ = require('underscore')
, validator = require('./lib/validator');
var validate = function(schema, name, value, args) {
// simple usage schema is constructor
if (schema instanceof Function) {
schema = {
allowNull: true,
type: schema
}
}
// allowNull check
if (schema.allowNull === true && value == null) return;
// type check
if (!validator.type(value, schema.type)) throw Error(schema.message || 'Argument `' + name + '` type must be `' + schema.type.name + '`');
// validate check
schema.validate && _.each(schema.validate, function(val, key) {
var pass = false;
if (_.isFunction(val)) {
pass = val(value, schema, args);
} else if (val === false) {
pass = !validator[key]('' + value);
} else if (val === true) {
pass = validator[key]('' + value);
} else {
pass = validator[key]('' + value, val);
}
if (!pass) {
throw Error(schema.message || '`' + name + '` validate failure: ' + key);
}
});
// iterator check
if (schema.iterator) {
_.each(value, function(v, i) {
_.each(schema.iterator, function(val, key) {
validate(val, key, v[key])
});
});
}
};
var delegate = function(fn, schemas) {
var args = [];
var getArgument = function(index) {
return function(value) {
if (value != null) args[index] = value;
return func;
};
};
var func = function() {
[].slice.call(arguments, 0).forEach(function(x, i) {
if (x != null) args[i] = x;
});
return func.exec();
};
_.each(schemas, function(schema, index) {
if (func.hasOwnProperty(schema.name)) throw Error("Function " + schema.name + " already exists!");
func[schema.name] = getArgument(index);
});
func.exec = function() {
var result;
try {
_.each(schemas, function(schema, index) {
if (schema.hasOwnProperty('defaultValue')) {
if (args[index] == null) args[index] = schema.defaultValue;
}
validate(schema, schema.name, args[index], args);
});
result = fn.apply(null, args);
} catch(e) {
throw e;
} finally {
args = [];
}
return result;
};
return func;
};
module.exports = delegate(delegate, [{
name: 'func',
allowNull: false,
type: Function,
message: 'The first argument `func` must be a function'
}, {
name: 'schemas',
type: Array,
allowNull: false,
validate: {
iteratorCheck: function(schemas) {
_.each(schemas, function(schema, index) {
if (schema.type === Array || !schema.iterator) return;
throw Error('`iterator` enabled when `Type` must be `Array` schemas[' + index + ']');
});
return true;
}
},
iterator: {
name: {
type: String,
allowNull: false,
validate: {
matches: /^[\$_A-z][\$_\w]+$/
},
message: 'The `name` must be a string, A-z0-9$_, Can\'t start with numbers'
},
type: {
type: Function,
allowNull: false,
message: 'The `type` must be a Type Function, eg. Array, Object, String...'
},
allowNull: Boolean,
validate: {
type: Object,
allowNull: true,
validate: {
exists: function(obj) {
var k, v;
for (k in obj) {
v = obj[k];
if (!_.isFunction(v) && !_.isFunction(validator[k])) {
throw Error('Not found validate rule: ' + k);
}
};
return true;
}
}
},
iterator: Object,
message: String
}
}]);