@openaddresses/batch-schema 4.0.0 → 4.3.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,26 @@
10
10
 
11
11
  ## Version History
12
12
 
13
+ ### v4.3.0
14
+
15
+ - :arrow_up: Update base deps
16
+ - :tada: Support casting query param arrays into `integer`, `number`, or `boolean`
17
+
18
+ ### v4.2.0
19
+
20
+ - :arrow_up: Update base deps
21
+ - :tada: Add `load()` function for loading routes dir
22
+
23
+ ### v4.1.0
24
+
25
+ - :bug: Ensure `default: false` is still applied
26
+ - :arrow_up: Update base deps
27
+
28
+ ### v4.0.0
29
+
30
+ - :white_check_mark: Update schema validator to latest version (AJV 8)
31
+ - :rocket: Remove `got` in favour of `fetch`
32
+
13
33
  ### v3.1.3
14
34
 
15
35
  - :rocket: Add automated releases
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
@@ -1,6 +1,8 @@
1
1
  import { Validator } from 'express-json-validator-middleware';
2
2
  import { ValidationError } from 'express-json-validator-middleware';
3
3
  import $RefParser from 'json-schema-ref-parser';
4
+ import fs from 'fs';
5
+ import path from 'path';
4
6
 
5
7
  import Err from './error.js';
6
8
  import SchemaRoute from '../routes/schema.js';
@@ -35,15 +37,12 @@ class Param {
35
37
 
36
38
  /**
37
39
  * @class
40
+ *
41
+ * @param {Object} router Express Router Object
42
+ * @param {Object} opts Options Object
43
+ * @param {String} opts.schemas Schemas Path
38
44
  */
39
45
  export default class Schemas {
40
- /**
41
- * @constructor
42
- *
43
- * @param {Object} router Express Router Object
44
- * @param {Object} opts Options Object
45
- * @param {String} opts.schemas Schemas Path
46
- */
47
46
  constructor(router, opts = {}) {
48
47
  if (!router) throw new Error('Router Param Required');
49
48
 
@@ -68,6 +67,23 @@ export default class Schemas {
68
67
  await SchemaRoute(this);
69
68
  }
70
69
 
70
+ /**
71
+ * Load routes directory. All .js files in the given directory will be loaded (ES Module)
72
+ * and the default method run
73
+ *
74
+ * @param {String} dirpath Directory to load
75
+ * @param {object} config Config Object
76
+ * @param {Object} opts Options Object
77
+ * @param {boolean} opts.silent Squelch StdOut
78
+ */
79
+ async load(dirpath, config, opts = {}) {
80
+ // Load dynamic routes directory
81
+ for (const r of fs.readdirSync(dirpath)) {
82
+ if (!opts.silent) console.error(`ok - loaded routes/${r}`);
83
+ await (await import(path.resolve(dirpath, r))).default(this, config);
84
+ }
85
+ }
86
+
71
87
  check(url, schemas, fns) {
72
88
  if (typeof url !== 'string') throw new Error('URL should be string');
73
89
 
@@ -180,19 +196,39 @@ export default class Schemas {
180
196
  */
181
197
  static query(schema) {
182
198
  return function (req, res, next) {
183
- for (const key of Object.keys(req.query)) {
199
+ for (const key in req.query) {
184
200
  if (!schema.properties[key] || !schema.properties[key].type) continue;
185
201
 
186
202
  // For easier processing use consistent array format IE: `type: ["integer", "boolean"]` vs type: "integer"
187
- if (!Array.isArray(schema.properties[key].type)) schema.properties[key].type = [schema.properties[key].type];
203
+ if (!Array.isArray(schema.properties[key].type)) {
204
+ schema.properties[key].type = [schema.properties[key].type];
205
+ }
188
206
 
189
207
  for (const type of schema.properties[key].type) {
190
- if (type === 'integer' && !isNaN(parseInt(req.query[key]))) {
208
+ if (type === 'array') {
209
+ req.query[key] = req.query[key].split(',');
210
+
211
+ if (schema.properties[key].items && schema.properties[key].items.type === 'number') {
212
+ req.query[key] = req.query[key].map((k) => {
213
+ return Number(k);
214
+ });
215
+ } else if (schema.properties[key].items && schema.properties[key].items.type === 'integer') {
216
+ req.query[key] = req.query[key].map((k) => {
217
+ return parseInt(k);
218
+ });
219
+ } else if (schema.properties[key].items && schema.properties[key].items.type === 'boolean') {
220
+ req.query[key] = req.query[key].map((k) => {
221
+ if (k === 'true') {
222
+ return true;
223
+ } else if (k === 'false') {
224
+ return false;
225
+ }
226
+ });
227
+ }
228
+ } else if (type === 'integer' && !isNaN(parseInt(req.query[key]))) {
191
229
  req.query[key] = parseInt(req.query[key]);
192
230
  } else if (type === 'number' && !isNaN(Number(req.query[key]))) {
193
231
  req.query[key] = Number(req.query[key]);
194
- } else if (type === 'array') {
195
- req.query[key] = req.query[key].split(',');
196
232
  } else if (type === 'boolean') {
197
233
  if (req.query[key] === 'true') {
198
234
  req.query[key] = true;
@@ -203,8 +239,8 @@ export default class Schemas {
203
239
  }
204
240
  }
205
241
 
206
- for (const key of Object.keys(schema.properties)) {
207
- if (req.query[key] === undefined && schema.properties[key].default) {
242
+ for (const key in schema.properties) {
243
+ if (req.query[key] === undefined && schema.properties[key].default !== undefined) {
208
244
  req.query[key] = schema.properties[key].default;
209
245
  }
210
246
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "root": true,
5
5
  "main": "index.js",
6
- "version": "4.0.0",
6
+ "version": "4.3.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>",