-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.js
More file actions
127 lines (101 loc) · 2.68 KB
/
environment.js
File metadata and controls
127 lines (101 loc) · 2.68 KB
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
/**
* Schema environment.
*/
'use strict';
/**
* Module dependencies.
*/
var jjv = require('jjv');
var jjve = require('jjve');
var lodash = require('lodash');
/**
* Initialize a new `Environment`.
*
* @param {Object} options
* @api public
*/
function Environment(options) {
if (!(this instanceof Environment)) {
return new Environment(options);
}
// defaults
options = lodash.defaults(options || {}, {
setup: true,
});
this.env = jjv();
this.env.defaultOptions.checkRequired = true;
this.env.defaultOptions.useDefault = true;
this.env.defaultOptions.useCoerce = false;
this._jjve = jjve(this.env);
if (options.setup) this.setup();
}
Environment.prototype.setup = function() {
this.env.addTypeCoercion('array', function(v) {
if (!Array.isArray(v)) {
return typeof v === 'undefined' ? [] : [v];
}
return v;
});
this.env.addTypeCoercion('integer', function(v) {
if (typeof v === 'string' && v.match(/^\-?\d+$/)) {
return parseInt(v, 10);
}
return v;
});
this.env.addTypeCoercion('number', function(v) {
if (typeof v === 'string' && v.match(/^\-?(\d+|\d*\.\d+|\d+\.\d*)$/)) {
return parseFloat(v);
}
return v;
});
this.env.addTypeCoercion('boolean', function(v) {
if (typeof v === 'string') {
return ['', '0', 'false', 'no'].indexOf(v) < 0;
}
return v;
});
this.env.addFormat('uri', function() {
// ignore jjv uri format check because it isn't liberal enough for swagger
// valdiation
return true;
});
};
Environment.prototype.setupValidation = function() {
console.log('Environment.setupValidation is deprecated');
};
Environment.prototype.validate = function(schema, data, options) {
var self = this;
options = options || {};
if (options.hasOwnProperty('coerce')) {
options.useCoerce = options.coerce;
delete options.coerce;
}
var result = self.env.validate(schema, data, options);
if (result) {
result.errors = function() {
return self._jjve(schema, data, result);
};
}
return result;
};
Environment.prototype.validateThrow = function(schema, data, options) {
if (typeof options === 'string') {
options = { message: options };
}
options = options || {};
var results = this.validate(schema, data, options);
if (results) {
var err = new Error(options.message || 'Validation failed');
err.errors = results.errors();
err.message += '\n' + JSON.stringify(data, null, 4);
err.message += '\nErrors:\n' + JSON.stringify(err.errors, null, 4);
throw err;
}
};
Environment.prototype.addSchema = function(name, data) {
this.env.addSchema(name, data);
};
/**
* Expose Environment.
*/
module.exports = Environment;