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
|
var assign = require('object-assign');
var mschema = require('mschema');
var v = require('validator');
/**
* Custom types.
* To add a custom type, add a key with the type name and a value
* in the form {parent: {...}, assertValid: function(d) {...}}
* where parent is the base mschema spec and validate is a function
* accepting one data point and throwing an array of errors if invalid.
*
* Error array format is derived from mschema and is in the form
* [{property: x, constraint: x, actual: x, expected: x, message: x} ... ]
*/
var types = {
/**
* Larger text inputs. Currently just proxies to string type.
*/
'text': {
parent: {type: 'string'},
// Let parent handle validation
assertValid: function(s) {}
}
}
/**
* OKSchema!
* Meant as a thin wrapper around some existing schema validation
* module, mostly to allow for the extension of types.
*/
function OKSchema(spec) {
if (!(this instanceof OKSchema)) return new OKSchema(spec);
if (!spec)
throw new Error('No spec provided to OKSchema');
spec = assign({}, spec);
// Cache the mschema version of our spec
this._mschemaSpec = Object.keys(spec).reduce(function(cache, prop) {
// If custom type, return its parent spec
var type = spec[prop].type;
if (types[type]) {
cache[prop] = types[type].parent;
// Otherwise, it's already in mschema format
} else {
cache[prop] = spec[prop];
}
return cache;
}, {});
Object.defineProperty(this, 'spec', {
value: spec,
writable: false
});
}
OKSchema.prototype.assertValid = function(data) {
data = data || {};
var spec = this.spec;
// Run through custom validators, they'll throw if invalid
Object.keys(data).forEach(function(prop) {
var type = spec[prop].type;
if (types[type]) {
types[type].assertValid(data[prop]);
}
});
var result = mschema.validate(data, this.toMschema());
if (!result.valid)
throw result.errors;
};
/**
* Return our custom spec as an mschema spec
*/
OKSchema.prototype.toMschema = function() {
return this._mschemaSpec;
};
module.exports = OKSchema;
|