@openaddresses/batch-schema 4.1.0 → 4.4.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,20 @@
10
10
 
11
11
  ## Version History
12
12
 
13
+ ### v4.4.0
14
+
15
+ - :rocket: Add ability to take a `URL` as a schemas directory
16
+
17
+ ### v4.3.0
18
+
19
+ - :arrow_up: Update base deps
20
+ - :tada: Support casting query param arrays into `integer`, `number`, or `boolean`
21
+
22
+ ### v4.2.0
23
+
24
+ - :arrow_up: Update base deps
25
+ - :tada: Add `load()` function for loading routes dir
26
+
13
27
  ### v4.1.0
14
28
 
15
29
  - :bug: Ensure `default: false` is still applied
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';
@@ -45,8 +47,11 @@ export default class Schemas {
45
47
  if (!router) throw new Error('Router Param Required');
46
48
 
47
49
  if (!opts.schemas) {
48
- this.schemas_path = String(new URL('../schema/', import.meta.url)).replace('file://', '');
50
+ this.schemas_path = new URL('../schema/', import.meta.url).pathname;
49
51
  } else {
52
+ if (dir instanceof URL) dir = dir.pathname;
53
+ else dir = String(dir)
54
+
50
55
  this.schemas_path = opts.schemas;
51
56
  }
52
57
 
@@ -65,6 +70,23 @@ export default class Schemas {
65
70
  await SchemaRoute(this);
66
71
  }
67
72
 
73
+ /**
74
+ * Load routes directory. All .js files in the given directory will be loaded (ES Module)
75
+ * and the default method run
76
+ *
77
+ * @param {String} dirpath Directory to load
78
+ * @param {object} config Config Object
79
+ * @param {Object} opts Options Object
80
+ * @param {boolean} opts.silent Squelch StdOut
81
+ */
82
+ async load(dirpath, config, opts = {}) {
83
+ // Load dynamic routes directory
84
+ for (const r of fs.readdirSync(dirpath)) {
85
+ if (!opts.silent) console.error(`ok - loaded routes/${r}`);
86
+ await (await import(path.resolve(dirpath, r))).default(this, config);
87
+ }
88
+ }
89
+
68
90
  check(url, schemas, fns) {
69
91
  if (typeof url !== 'string') throw new Error('URL should be string');
70
92
 
@@ -186,12 +208,30 @@ export default class Schemas {
186
208
  }
187
209
 
188
210
  for (const type of schema.properties[key].type) {
189
- if (type === 'integer' && !isNaN(parseInt(req.query[key]))) {
211
+ if (type === 'array') {
212
+ req.query[key] = req.query[key].split(',');
213
+
214
+ if (schema.properties[key].items && schema.properties[key].items.type === 'number') {
215
+ req.query[key] = req.query[key].map((k) => {
216
+ return Number(k);
217
+ });
218
+ } else if (schema.properties[key].items && schema.properties[key].items.type === 'integer') {
219
+ req.query[key] = req.query[key].map((k) => {
220
+ return parseInt(k);
221
+ });
222
+ } else if (schema.properties[key].items && schema.properties[key].items.type === 'boolean') {
223
+ req.query[key] = req.query[key].map((k) => {
224
+ if (k === 'true') {
225
+ return true;
226
+ } else if (k === 'false') {
227
+ return false;
228
+ }
229
+ });
230
+ }
231
+ } else if (type === 'integer' && !isNaN(parseInt(req.query[key]))) {
190
232
  req.query[key] = parseInt(req.query[key]);
191
233
  } else if (type === 'number' && !isNaN(Number(req.query[key]))) {
192
234
  req.query[key] = Number(req.query[key]);
193
- } else if (type === 'array') {
194
- req.query[key] = req.query[key].split(',');
195
235
  } else if (type === 'boolean') {
196
236
  if (req.query[key] === 'true') {
197
237
  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.1.0",
6
+ "version": "4.4.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>",