@openaddresses/batch-schema 5.1.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/lib/middleware.js +148 -0
- package/lib/param.js +29 -0
- package/lib/schema.js +10 -139
- package/package.json +2 -1
- package/schema/res.ListSchema.json +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import Err from './error.js';
|
|
2
|
+
import Param from './param.js';
|
|
3
|
+
import Ajv from 'ajv';
|
|
4
|
+
import addFormats from 'ajv-formats';
|
|
5
|
+
import { ValidationError } from 'express-json-validator-middleware';
|
|
6
|
+
|
|
7
|
+
const ajv = addFormats(new Ajv({
|
|
8
|
+
allErrors: true,
|
|
9
|
+
removeAdditional: true
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @class
|
|
14
|
+
*/
|
|
15
|
+
export default class Middleware {
|
|
16
|
+
/**
|
|
17
|
+
* Express middleware to identify express params that should be integers/number/booleans
|
|
18
|
+
* according to the schema and attempt to cast them as such to ensure they pass the schema
|
|
19
|
+
*
|
|
20
|
+
* @param {String} match Express Param
|
|
21
|
+
* @param {String} type Type to coerce it to
|
|
22
|
+
*
|
|
23
|
+
* @returns {Function}
|
|
24
|
+
*/
|
|
25
|
+
static param(match, type) {
|
|
26
|
+
return function (req, res, next) {
|
|
27
|
+
try {
|
|
28
|
+
Param[type](req, match.replace(':', ''));
|
|
29
|
+
} catch (err) {
|
|
30
|
+
return Err.respond(err, res);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return next();
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Express middleware to identify query params that should be integers/booleans according to the schema
|
|
39
|
+
* and attempt to cast them as such to ensure they pass the schema
|
|
40
|
+
*
|
|
41
|
+
* @param {Object} schema JSON Schema
|
|
42
|
+
*
|
|
43
|
+
* @returns {Function}
|
|
44
|
+
*/
|
|
45
|
+
static query(schema) {
|
|
46
|
+
return function (req, res, next) {
|
|
47
|
+
for (const key in req.query) {
|
|
48
|
+
if (!schema.properties[key] || !schema.properties[key].type) continue;
|
|
49
|
+
|
|
50
|
+
// For easier processing use consistent array format IE: `type: ["integer", "boolean"]` vs type: "integer"
|
|
51
|
+
if (!Array.isArray(schema.properties[key].type)) {
|
|
52
|
+
schema.properties[key].type = [schema.properties[key].type];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
for (const type of schema.properties[key].type) {
|
|
56
|
+
if (type === 'array') {
|
|
57
|
+
req.query[key] = req.query[key].split(',');
|
|
58
|
+
|
|
59
|
+
if (schema.properties[key].items && schema.properties[key].items.type === 'number') {
|
|
60
|
+
req.query[key] = req.query[key].map((k) => {
|
|
61
|
+
return Number(k);
|
|
62
|
+
});
|
|
63
|
+
} else if (schema.properties[key].items && schema.properties[key].items.type === 'integer') {
|
|
64
|
+
req.query[key] = req.query[key].map((k) => {
|
|
65
|
+
return parseInt(k);
|
|
66
|
+
});
|
|
67
|
+
} else if (schema.properties[key].items && schema.properties[key].items.type === 'boolean') {
|
|
68
|
+
req.query[key] = req.query[key].map((k) => {
|
|
69
|
+
if (k === 'true') {
|
|
70
|
+
return true;
|
|
71
|
+
} else if (k === 'false') {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
} else if (type === 'integer' && !isNaN(parseInt(req.query[key]))) {
|
|
77
|
+
req.query[key] = parseInt(req.query[key]);
|
|
78
|
+
} else if (type === 'number' && !isNaN(Number(req.query[key]))) {
|
|
79
|
+
req.query[key] = Number(req.query[key]);
|
|
80
|
+
} else if (type === 'boolean') {
|
|
81
|
+
if (req.query[key] === 'true') {
|
|
82
|
+
req.query[key] = true;
|
|
83
|
+
} else if (req.query[key] === 'false') {
|
|
84
|
+
req.query[key] = false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
for (const key in schema.properties) {
|
|
91
|
+
if (req.query[key] === undefined && schema.properties[key].default !== undefined) {
|
|
92
|
+
req.query[key] = schema.properties[key].default;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return next();
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Express middleware to validate the response body
|
|
102
|
+
*
|
|
103
|
+
* @param {Object} schema JSON Schema
|
|
104
|
+
*
|
|
105
|
+
* @returns {Function}
|
|
106
|
+
*/
|
|
107
|
+
static res(schema) {
|
|
108
|
+
const validate = ajv.compile(schema);
|
|
109
|
+
|
|
110
|
+
return function (req, res, next) {
|
|
111
|
+
const json = res.json;
|
|
112
|
+
res.json = function(obj) {
|
|
113
|
+
validate(obj);
|
|
114
|
+
|
|
115
|
+
json.call(this, obj);
|
|
116
|
+
};
|
|
117
|
+
next();
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Convert validation errors into standardized JSON Error Messages
|
|
123
|
+
*
|
|
124
|
+
* @returns {Function}
|
|
125
|
+
*/
|
|
126
|
+
static error() {
|
|
127
|
+
return function(err, req, res, next) {
|
|
128
|
+
if (err instanceof ValidationError) {
|
|
129
|
+
let errs = [];
|
|
130
|
+
if (err.validationErrors.body) {
|
|
131
|
+
errs = errs.concat(err.validationErrors.body);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (err.validationErrors.query) {
|
|
135
|
+
errs = errs.concat(err.validationErrors.query);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return Err.respond(
|
|
139
|
+
new Err(400, null, 'validation error'),
|
|
140
|
+
res,
|
|
141
|
+
errs
|
|
142
|
+
);
|
|
143
|
+
} else {
|
|
144
|
+
next(err);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
package/lib/param.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import Err from './error.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @class
|
|
5
|
+
*/
|
|
6
|
+
export default class Param {
|
|
7
|
+
static integer(req, name) {
|
|
8
|
+
req.params[name] = parseInt(req.params[name]);
|
|
9
|
+
if (isNaN(req.params[name])) {
|
|
10
|
+
throw new Err(400, null, `${name} param must be an integer`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static number(req, name) {
|
|
15
|
+
req.params[name] = Number(req.params[name]);
|
|
16
|
+
if (isNaN(req.params[name])) {
|
|
17
|
+
throw new Err(400, null, `${name} param must be numeric`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static boolean(req, name) {
|
|
22
|
+
if (!['true', 'false'].includes(req.params[name])) throw new Error(`${name} param must be a boolean`);
|
|
23
|
+
req.params[name] = req.params[name] === true ? true : false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static string(req, name) {
|
|
27
|
+
req.params[name] = String(req.params[name]);
|
|
28
|
+
}
|
|
29
|
+
}
|
package/lib/schema.js
CHANGED
|
@@ -1,41 +1,13 @@
|
|
|
1
1
|
import { Validator } from 'express-json-validator-middleware';
|
|
2
|
-
import { ValidationError } from 'express-json-validator-middleware';
|
|
3
2
|
import $RefParser from 'json-schema-ref-parser';
|
|
4
3
|
import fs from 'fs';
|
|
5
4
|
import path from 'path';
|
|
6
5
|
import morgan from 'morgan';
|
|
7
6
|
import bodyparser from 'body-parser';
|
|
8
7
|
|
|
9
|
-
import Err from './error.js';
|
|
10
8
|
import SchemaRoute from '../routes/schema.js';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* @class
|
|
14
|
-
*/
|
|
15
|
-
class Param {
|
|
16
|
-
static integer(req, name) {
|
|
17
|
-
req.params[name] = parseInt(req.params[name]);
|
|
18
|
-
if (isNaN(req.params[name])) {
|
|
19
|
-
throw new Err(400, null, `${name} param must be an integer`);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
static number(req, name) {
|
|
24
|
-
req.params[name] = Number(req.params[name]);
|
|
25
|
-
if (isNaN(req.params[name])) {
|
|
26
|
-
throw new Err(400, null, `${name} param must be numeric`);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static boolean(req, name) {
|
|
31
|
-
if (!['true', 'false'].includes(req.params[name])) throw new Error(`${name} param must be a boolean`);
|
|
32
|
-
req.params[name] = req.params[name] === true ? true : false;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
static string(req, name) {
|
|
36
|
-
req.params[name] = String(req.params[name]);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
9
|
+
import Param from './param.js';
|
|
10
|
+
import Middleware from './middleware.js';
|
|
39
11
|
|
|
40
12
|
/**
|
|
41
13
|
* @class
|
|
@@ -164,104 +136,29 @@ export default class Schemas {
|
|
|
164
136
|
|
|
165
137
|
const flow = [parsed[1], []];
|
|
166
138
|
|
|
167
|
-
if (schemas.query) flow[1].push(
|
|
139
|
+
if (schemas.query) flow[1].push(Middleware.query(schemas.query));
|
|
168
140
|
|
|
169
141
|
// Make sure express params are validated/coerced into proper type
|
|
170
142
|
const matches = url.match(/(:.+?)(?=\/|\.|$)/g);
|
|
171
143
|
if (matches) for (const match of matches) {
|
|
172
144
|
if (!schemas[match]) throw new Error(`${match} type is not defined in schema`);
|
|
173
145
|
|
|
174
|
-
flow[1].push(
|
|
146
|
+
flow[1].push(Middleware.param(match, schemas[match]));
|
|
175
147
|
}
|
|
176
148
|
|
|
177
149
|
flow[1].push(this.validate(opts));
|
|
178
150
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Express middleware to identify express params that should be integers/number/booleans
|
|
184
|
-
* according to the schema and attempt to cast them as such to ensure they pass the schema
|
|
185
|
-
*
|
|
186
|
-
* @param {String} match Express Param
|
|
187
|
-
* @param {String} type Type to coerce it to
|
|
188
|
-
*
|
|
189
|
-
* @returns {Function}
|
|
190
|
-
*/
|
|
191
|
-
static param(match, type) {
|
|
192
|
-
return function (req, res, next) {
|
|
193
|
-
|
|
194
|
-
try {
|
|
195
|
-
Param[type](req, match.replace(':', ''));
|
|
196
|
-
} catch (err) {
|
|
197
|
-
return Err.respond(err, res);
|
|
198
|
-
}
|
|
151
|
+
console.error(schemas);
|
|
152
|
+
if (schemas.res) flow[1].push(Middleware.res(schemas.res));
|
|
199
153
|
|
|
200
|
-
|
|
201
|
-
};
|
|
154
|
+
return flow;
|
|
202
155
|
}
|
|
203
156
|
|
|
204
157
|
/**
|
|
205
|
-
*
|
|
206
|
-
* and attempt to cast them as such to ensure they pass the schema
|
|
207
|
-
*
|
|
208
|
-
* @param {Object} schema JSON Schema
|
|
209
|
-
*
|
|
210
|
-
* @returns {Function}
|
|
158
|
+
* Convert validation errors into standardized JSON Error Messages
|
|
211
159
|
*/
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
for (const key in req.query) {
|
|
215
|
-
if (!schema.properties[key] || !schema.properties[key].type) continue;
|
|
216
|
-
|
|
217
|
-
// For easier processing use consistent array format IE: `type: ["integer", "boolean"]` vs type: "integer"
|
|
218
|
-
if (!Array.isArray(schema.properties[key].type)) {
|
|
219
|
-
schema.properties[key].type = [schema.properties[key].type];
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
for (const type of schema.properties[key].type) {
|
|
223
|
-
if (type === 'array') {
|
|
224
|
-
req.query[key] = req.query[key].split(',');
|
|
225
|
-
|
|
226
|
-
if (schema.properties[key].items && schema.properties[key].items.type === 'number') {
|
|
227
|
-
req.query[key] = req.query[key].map((k) => {
|
|
228
|
-
return Number(k);
|
|
229
|
-
});
|
|
230
|
-
} else if (schema.properties[key].items && schema.properties[key].items.type === 'integer') {
|
|
231
|
-
req.query[key] = req.query[key].map((k) => {
|
|
232
|
-
return parseInt(k);
|
|
233
|
-
});
|
|
234
|
-
} else if (schema.properties[key].items && schema.properties[key].items.type === 'boolean') {
|
|
235
|
-
req.query[key] = req.query[key].map((k) => {
|
|
236
|
-
if (k === 'true') {
|
|
237
|
-
return true;
|
|
238
|
-
} else if (k === 'false') {
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
} else if (type === 'integer' && !isNaN(parseInt(req.query[key]))) {
|
|
244
|
-
req.query[key] = parseInt(req.query[key]);
|
|
245
|
-
} else if (type === 'number' && !isNaN(Number(req.query[key]))) {
|
|
246
|
-
req.query[key] = Number(req.query[key]);
|
|
247
|
-
} else if (type === 'boolean') {
|
|
248
|
-
if (req.query[key] === 'true') {
|
|
249
|
-
req.query[key] = true;
|
|
250
|
-
} else if (req.query[key] === 'false') {
|
|
251
|
-
req.query[key] = false;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
for (const key in schema.properties) {
|
|
258
|
-
if (req.query[key] === undefined && schema.properties[key].default !== undefined) {
|
|
259
|
-
req.query[key] = schema.properties[key].default;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return next();
|
|
264
|
-
};
|
|
160
|
+
error() {
|
|
161
|
+
this.router.use(Middleware.error());
|
|
265
162
|
}
|
|
266
163
|
|
|
267
164
|
/**
|
|
@@ -298,32 +195,6 @@ export default class Schemas {
|
|
|
298
195
|
});
|
|
299
196
|
}
|
|
300
197
|
|
|
301
|
-
/**
|
|
302
|
-
* Convert validation errors into standardized JSON Error Messages
|
|
303
|
-
*/
|
|
304
|
-
error() {
|
|
305
|
-
this.router.use((err, req, res, next) => {
|
|
306
|
-
if (err instanceof ValidationError) {
|
|
307
|
-
let errs = [];
|
|
308
|
-
if (err.validationErrors.body) {
|
|
309
|
-
errs = errs.concat(err.validationErrors.body);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
if (err.validationErrors.query) {
|
|
313
|
-
errs = errs.concat(err.validationErrors.query);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return Err.respond(
|
|
317
|
-
new Err(400, null, 'validation error'),
|
|
318
|
-
res,
|
|
319
|
-
errs
|
|
320
|
-
);
|
|
321
|
-
} else {
|
|
322
|
-
next(err);
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
|
|
327
198
|
/**
|
|
328
199
|
* Return a list of endpoints with schemas
|
|
329
200
|
*
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"root": true,
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"version": "
|
|
6
|
+
"version": "6.0.0",
|
|
7
7
|
"description": "Strongly Validated JSON Schema support for address",
|
|
8
8
|
"repository": "git@github.com:openaddresses/batch-schema",
|
|
9
9
|
"author": "ingalls <nick@ingalls.ca>",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"ajv": "^8.6.3",
|
|
22
|
+
"ajv-formats": "^2.1.1",
|
|
22
23
|
"body-parser": "^1.20.0",
|
|
23
24
|
"express": "^4.17.1",
|
|
24
25
|
"express-json-validator-middleware": "^3.0.0",
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"oneOf": [{
|
|
3
3
|
"type": "object",
|
|
4
|
-
"additionalProperties": false,
|
|
5
4
|
"patternProperties": {
|
|
6
5
|
".*": {
|
|
7
6
|
"type": "object",
|
|
@@ -10,7 +9,6 @@
|
|
|
10
9
|
"query",
|
|
11
10
|
"res"
|
|
12
11
|
],
|
|
13
|
-
"additionalProperties": false,
|
|
14
12
|
"properties": {
|
|
15
13
|
"body": {
|
|
16
14
|
"type": "boolean",
|
|
@@ -34,7 +32,6 @@
|
|
|
34
32
|
"res",
|
|
35
33
|
"query"
|
|
36
34
|
],
|
|
37
|
-
"additionalProperties": false,
|
|
38
35
|
"properties": {
|
|
39
36
|
"body": {
|
|
40
37
|
"type": ["null", "object"]
|