forked from sailshq/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
307 lines (199 loc) · 5.99 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* Module dependencies
*/
var util = require('lodash');
var sanitize = require('validator').sanitize;
var _ = require('lodash');
/**
* Public access
*/
module.exports = function (entity) {
return new Anchor(entity);
};
/**
* Constructor of individual instance of Anchor
* Specify the function, object, or list to be anchored
*/
function Anchor (entity) {
if (util.isFunction(entity)) {
this.fn = entity;
throw new Error ('Anchor does not support functions yet!');
}
else {
this.data = entity;
}
return this;
}
/**
* Built-in data type rules
*/
Anchor.prototype.rules = require('./lib/match/rules');
/**
* Enforce that the data matches the specified ruleset
*/
Anchor.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(Anchor.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(Anchor.prototype.rules[ruleset.dbType]) {
errors = errors.concat(Anchor.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(Anchor.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;
}
};
Anchor.prototype.hasErrors = Anchor.prototype.to;
/**
* Coerce the data to the specified ruleset if possible
* otherwise throw an error
* Priority: this should probably provide the default
* implementation in Waterline 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 Anchor.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.
*
*/
Anchor.prototype.cast = function (ruleset) {
todo();
};
/**
* Coerce the data to the specified ruleset no matter what
*/
Anchor.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
Anchor.match(this.data, ruleset, this);
};
/**
* Specify default values to automatically populated when undefined
*/
Anchor.prototype.defaults = function (ruleset) {
todo();
};
/**
* 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
*/
Anchor.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(Anchor.prototype.rules, name);
return this;
}
if ( util.isFunction(definition) && util.isString(name) ) {
// Add a single data type
Anchor.prototype.rules[name] = definition;
return this;
}
throw new Error('Definition error: \"' + name + '\" is not a valid definition.');
};
/**
* Specify custom ruleset
*/
Anchor.prototype.as = function (ruleset) {
todo();
};
/**
* Specify named arguments and their rulesets as an object
*/
Anchor.prototype.args = function (args) {
todo();
};
/**
* Specify each of the permitted usages for this function
*/
Anchor.prototype.usage = function () {
var usages = util.toArray(arguments);
todo();
};
/**
* Deep-match a complex collection or model against a schema
*/
Anchor.match = require('./lib/match');
/**
* Expose `define` so it can be used globally
*/
module.exports.define = Anchor.prototype.define;
function todo() {
throw new Error('Not implemented yet! If you\'d like to contribute, tweet @mikermcneil.');
}