forked from sailshq/anchor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
254 lines (197 loc) · 5.97 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* Module dependencies
*/
var util = require('lodash');
var _ = require('lodash');
/**
* Public access
*/
module.exports = function (entity) {
return new Validator(entity);
};
/**
* Constructor of individual instance of Offshore Validator
* Specify the function, object, or list to be validatored
*/
function Validator(entity) {
if (util.isFunction(entity)) {
this.fn = entity;
throw new Error ('Offshore Validator does not support functions yet!');
} else {
this.data = entity;
}
return this;
}
/**
* Built-in data type rules
*/
Validator.prototype.rules = require('./lib/match/rules');
/**
* Enforce that the data matches the specified ruleset
*/
Validator.prototype.to = function (ruleset, context) {
var errors = [];
// If ruleset doesn't contain any explicit rule keys,
// assume that this is a type
// Look for explicit rules
for (var rule in ruleset) {
if (rule === 'type') {
// Use deep match to descend into the collection and verify each item and/or key
// Stop at default maxDepth (50) to prevent infinite loops in self-associations
errors = errors.concat(Validator.match.type.call(context, this.data, ruleset.type));
}
// Validate a dbType rule
else if (rule === 'dbType') {
// only if a validation rule exists for it so it doesn't break on an adapter that
// doesn't support the particular dbType
if (Validator.prototype.rules[ruleset.dbType]) {
errors = errors.concat(Validator.match.type.call(context, this.data, ruleset.dbType));
}
}
// Validate a non-type rule
else {
// Normalize the value if it looks like a boolean
if (ruleset[rule] === 'true') {
ruleset[rule] = true;
}
if (ruleset[rule] === 'false') {
ruleset[rule] = false;
}
// If the value is false, then we shouldn't even run the validation
if (ruleset[rule] === false) {
break;
}
// If the rule value is a boolean we don't need to pass the value along.
// Otherwise we can pass it along so it's options are available in
// the validation.
var ruleVal = _.isBoolean(ruleset[rule]) ? undefined : ruleset[rule];
errors = errors.concat(Validator.match.rule.call(context, this.data, rule, ruleVal));
}
}
// If errors exist, return the list of them
if (errors.length) {
return errors;
}
// No errors, so return false
else {
return false;
}
};
Validator.prototype.hasErrors = Validator.prototype.to;
/**
* Coerce the data to the specified ruleset if possible
* otherwise throw an error
* Priority: this should probably provide the default
* implementation in Offshore core. Currently it's completely
* up to the adapter to define type coercion.
*
* Which is fine!.. but complicates custom CRUD adapter development.
* Much handier would be an evented architecture, that allows
* for adapter developers to write:
*
{
// Called before find() receives criteria
// Here, criteria refers to just attributes (the `where`)
// limit, skip, and sort are not included
coerceCriteria: function (criteria) {
return criteria;
},
// Called before create() or update() receive values
coerceValues: function () {}
}
*
* Adapter developers would be able to use Validator.prototype.cast()
* to declaritively define these type coercions.
* Down the line, we could take this further for an even nicer API,
* but for now, this alone would be a nice improvement.
*
*/
Validator.prototype.cast = function (ruleset) {
todo(ruleset);
};
/**
* Coerce the data to the specified ruleset no matter what
*/
Validator.prototype.hurl = function (ruleset) {
// Iterate trough given data attributes
// to check if they exist in the ruleset
for (var attr in this.data) {
if (this.data.hasOwnProperty(attr)) {
// If it doesnt...
if (!ruleset[attr]) {
// Declaring err here as error helpers live in match.js
var err = new Error('Validation error: Attribute \"' + attr + '\" is not in the ruleset.');
// just throw it
throw err;
}
}
}
// Once we make sure that attributes match
// we can just proceed to deepMatch
Validator.match(this.data, ruleset, this);
};
/**
* Specify default values to automatically populated when undefined
*/
Validator.prototype.defaults = function (ruleset) {
todo(ruleset);
};
/**
* Declare a custom data type
* If function definition is specified, `name` is required.
* Otherwise, if dictionary-type `definition` is specified,
* `name` must not be present.
*
* @param {String} name [optional]
* @param {Object|Function} definition
*/
Validator.prototype.define = function (name, definition) {
// check to see if we have an dictionary
if ( util.isObject(name) ) {
// if so all the attributes should be validation functions
for (var attr in name) {
if (!util.isFunction(name[attr])){
throw new Error('Definition error: \"' + attr + '\" does not have a definition');
}
}
// add the new custom data types
util.extend(Validator.prototype.rules, name);
return this;
}
if ( util.isFunction(definition) && util.isString(name) ) {
// Add a single data type
Validator.prototype.rules[name] = definition;
return this;
}
throw new Error('Definition error: \"' + name + '\" is not a valid definition.');
};
/**
* Specify custom ruleset
*/
Validator.prototype.as = function (ruleset) {
todo(ruleset);
};
/**
* Specify named arguments and their rulesets as an object
*/
Validator.prototype.args = function (args) {
todo(args);
};
/**
* Specify each of the permitted usages for this function
*/
Validator.prototype.usage = function () {
var usages = util.toArray(arguments);
todo(usages);
};
/**
* Deep-match a complex collection or model against a schema
*/
Validator.match = require('./lib/match');
/**
* Expose `define` so it can be used globally
*/
module.exports.define = Validator.prototype.define;
function todo() {
throw new Error('Not implemented yet!');
}