-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
200 lines (150 loc) · 5.95 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
/**
@module express-jsonschema
@author Adrian Adkison
*/
var jsonschema = require('jsonschema'),
customProperties = {};
/**
@function formatValidations
@desc Formats the validation data structure from the jsonschema
library into a more convenient data structure.
@private
@param {Object} validations - An object where the keys are request
properties and the values are their respective jsonschema
validations.
@returns {Object} formatted - An object where the keys are request
properties and the values are their respective formatted
validations.
*/
function formatValidations(validations) {
var formatted = {};
Object.keys(validations).forEach(function(requestProperty) {
var validation = validations[requestProperty],
propertyValidations = [],
currentPropertyValidation = {};
validation.errors.forEach(function(propertyValidation) {
var isNewProperty = currentPropertyValidation.property !== propertyValidation.property;
if (isNewProperty) {
currentPropertyValidation = {
value: propertyValidation.instance,
property: propertyValidation.property,
messages: [propertyValidation.message]
};
propertyValidations.push(currentPropertyValidation);
} else {
currentPropertyValidation.messages.push(propertyValidation.message);
}
});
formatted[requestProperty] = propertyValidations;
});
return formatted;
}
/**
@constructor JsonSchemaCustomPropertyError
@desc Instantiated when a client attempts to add a custom schema property
that already exists.
@public
@param {String} propertyName - The name of the schema property that has a conflict.
*/
function JsonSchemaCustomPropertyError(propertyName) {
/** @member {String} name */
this.name = 'JsonSchemaCustomPropertyError';
/** @member {String} message */
this.message = (
'express-jsonschema: The schema property "' + propertyName +
'" already exists. See if it achieves what you need or try ' +
'giving it another name.'
);
}
/**
@constructor JsonSchemaValidation
@desc Instantiated when invalid data is found in the request.
@public
@param {Object} validations - An object where the keys are request
properties and the values are their respective jsonschema
validations.
*/
function JsonSchemaValidation(validations) {
/** @member {String} name */
this.name = 'JsonSchemaValidation';
/** @member {String} message */
this.message = 'express-jsonschema: Invalid data found';
/** @member {Object} validations */
this.validations = formatValidations(validations);
}
/**
@function addSchemaProperties
@desc Updates customProperties with
the newProperties param. Provides a way for client
to extend JSON Schema validations.
@public
@param {Object} newProperties - An object where the keys are the
names of the new schema properties and the values are the respective
functions that implement the validation.
@throws {JsonSchemaCustomPropertyError} Client tries to override
an existing JSON Schema property.
*/
function addSchemaProperties(newProperties) {
var validator = new jsonschema.Validator();
Object.keys(newProperties).forEach(function(attr) {
if (validator.attributes[attr]) {
throw new JsonSchemaCustomPropertyError(attr);
}
customProperties[attr] = newProperties[attr];
});
}
/**
@function validate
@desc Accepts an object where the keys are request properties and the
values are their respective schemas. Optionally, you may provide
dependency schemas that are referenced by your schemas using `$ref`
(see https://www.npmjs.com/package/jsonschema#complex-example-with-split-schemas-and-references
for more details).
Returns a middleware function that validates the given
request properties when a request is made. If there is any invalid
data a JsonSchemaValidation instance is passed to the next middleware.
If the data is valid the next middleware is called with no params.
@public
@param {Object} schemas - An object where the keys are request properties
and the values are their respective schemas.
@param {Array<Object>} [schemaDependencies] - A list of schemas on which
schemas in `schemas` parameter are dependent. These will be added
to the `jsonschema` validator.
@returns {callback} - A middleware function.
*/
function validate(schemas, schemaDependencies) {
var validator = new jsonschema.Validator();
if (Array.isArray(schemaDependencies)) {
schemaDependencies.forEach(function(dependency){
validator.addSchema(dependency);
});
}
Object.keys(customProperties).forEach(function(attr) {
validator.attributes[attr] = customProperties[attr];
});
return function(req, res, next) {
var validations = {};
Object.keys(schemas).forEach(function(requestProperty) {
var schema = schemas[requestProperty],
validation;
validation = validator.validate(
req[requestProperty],
schema,
{propertyName: 'request.' + requestProperty}
);
if (!validation.valid) {
validations[requestProperty] = validation;
}
});
if (Object.keys(validations).length) {
next(new JsonSchemaValidation(validations));
} else {
next();
}
};
}
exports = module.exports;
exports.validate = validate;
exports.addSchemaProperties = addSchemaProperties;
exports.JsonSchemaValidation = JsonSchemaValidation;
exports.JsonSchemaCustomPropertyError = JsonSchemaCustomPropertyError;