@openaddresses/batch-schema 4.2.0 → 5.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 CHANGED
@@ -10,6 +10,19 @@
10
10
 
11
11
  ## Version History
12
12
 
13
+ ### v5.0.0
14
+
15
+ - :tada: Logging and body parsing enabled by default
16
+
17
+ ### v4.4.0
18
+
19
+ - :rocket: Add ability to take a `URL` as a schemas directory
20
+
21
+ ### v4.3.0
22
+
23
+ - :arrow_up: Update base deps
24
+ - :tada: Support casting query param arrays into `integer`, `number`, or `boolean`
25
+
13
26
  ### v4.2.0
14
27
 
15
28
  - :arrow_up: Update base deps
package/README.md CHANGED
@@ -94,3 +94,18 @@ express compatible responses.
94
94
  ```
95
95
  schema.error()
96
96
  ```
97
+
98
+ ### schema.load
99
+
100
+ Loads and runs all routes files in a directory
101
+
102
+ ```
103
+ schema.load(directory, config, opts)
104
+ ```
105
+
106
+ | Config Option | Notes |
107
+ | ----------------- | ----- |
108
+ | `directory` | Directory to load .js files from |
109
+ | `config` | Config option to pass to each route |
110
+ | `opts` | Optional Opts Object |
111
+ | `opts.silent` | Squelch output |
package/lib/schema.js CHANGED
@@ -3,6 +3,8 @@ import { ValidationError } from 'express-json-validator-middleware';
3
3
  import $RefParser from 'json-schema-ref-parser';
4
4
  import fs from 'fs';
5
5
  import path from 'path';
6
+ import morgan from 'morgan';
7
+ import bodyparser from 'body-parser';
6
8
 
7
9
  import Err from './error.js';
8
10
  import SchemaRoute from '../routes/schema.js';
@@ -41,14 +43,19 @@ class Param {
41
43
  * @param {Object} router Express Router Object
42
44
  * @param {Object} opts Options Object
43
45
  * @param {String} opts.schemas Schemas Path
46
+ * @param {boolean|string} [opts.logging=true] disable logging with false
47
+ * @param {number} [opts.limit=50] body size limit in mb
44
48
  */
45
49
  export default class Schemas {
46
50
  constructor(router, opts = {}) {
47
51
  if (!router) throw new Error('Router Param Required');
48
52
 
49
53
  if (!opts.schemas) {
50
- this.schemas_path = String(new URL('../schema/', import.meta.url)).replace('file://', '');
54
+ this.schemas_path = new URL('../schema/', import.meta.url).pathname;
51
55
  } else {
56
+ if (opts.schemas instanceof URL) opts.schemas = opts.schemas.pathname;
57
+ else opts.schemas = String(opts.schemas);
58
+
52
59
  this.schemas_path = opts.schemas;
53
60
  }
54
61
 
@@ -59,6 +66,11 @@ export default class Schemas {
59
66
  });
60
67
 
61
68
  this.router = router;
69
+
70
+ if (opts.morgan !== false) this.router.use(morgan('combined'));
71
+ this.router.use(bodyparser.urlencoded({ extended: true }));
72
+ this.router.use(bodyparser.json({ limit: `${opts.limit}mb` }));
73
+
62
74
  this.schemas = new Map();
63
75
  this.validate = this.validator.validate;
64
76
  }
@@ -205,12 +217,30 @@ export default class Schemas {
205
217
  }
206
218
 
207
219
  for (const type of schema.properties[key].type) {
208
- if (type === 'integer' && !isNaN(parseInt(req.query[key]))) {
220
+ if (type === 'array') {
221
+ req.query[key] = req.query[key].split(',');
222
+
223
+ if (schema.properties[key].items && schema.properties[key].items.type === 'number') {
224
+ req.query[key] = req.query[key].map((k) => {
225
+ return Number(k);
226
+ });
227
+ } else if (schema.properties[key].items && schema.properties[key].items.type === 'integer') {
228
+ req.query[key] = req.query[key].map((k) => {
229
+ return parseInt(k);
230
+ });
231
+ } else if (schema.properties[key].items && schema.properties[key].items.type === 'boolean') {
232
+ req.query[key] = req.query[key].map((k) => {
233
+ if (k === 'true') {
234
+ return true;
235
+ } else if (k === 'false') {
236
+ return false;
237
+ }
238
+ });
239
+ }
240
+ } else if (type === 'integer' && !isNaN(parseInt(req.query[key]))) {
209
241
  req.query[key] = parseInt(req.query[key]);
210
242
  } else if (type === 'number' && !isNaN(Number(req.query[key]))) {
211
243
  req.query[key] = Number(req.query[key]);
212
- } else if (type === 'array') {
213
- req.query[key] = req.query[key].split(',');
214
244
  } else if (type === 'boolean') {
215
245
  if (req.query[key] === 'true') {
216
246
  req.query[key] = true;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "root": true,
5
5
  "main": "index.js",
6
- "version": "4.2.0",
6
+ "version": "5.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,10 +19,12 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "ajv": "^8.6.3",
22
+ "body-parser": "^1.20.0",
22
23
  "express": "^4.17.1",
23
24
  "express-json-validator-middleware": "^3.0.0",
24
25
  "glob": "^8.0.0",
25
26
  "json-schema-ref-parser": "^9.0.9",
27
+ "morgan": "^1.10.0",
26
28
  "tape": "^5.3.1"
27
29
  },
28
30
  "devDependencies": {