@openaddresses/batch-schema 1.0.0 → 2.2.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 +17 -0
- package/README.md +27 -4
- package/lib/error.js +10 -1
- package/lib/schema.js +10 -8
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,23 @@
|
|
|
10
10
|
|
|
11
11
|
## Version History
|
|
12
12
|
|
|
13
|
+
### v2.2.0
|
|
14
|
+
|
|
15
|
+
- :tada: Add support for errors with `status` set from external libraries (IE OpenAddresses/tilebase)
|
|
16
|
+
|
|
17
|
+
### v2.1.0
|
|
18
|
+
|
|
19
|
+
- :tada: Add more detailed schema error messages
|
|
20
|
+
|
|
21
|
+
### v2.0.0
|
|
22
|
+
|
|
23
|
+
- :tada: Add support for populating `default` keys in schema
|
|
24
|
+
- :arrow_up: General Dep Update
|
|
25
|
+
|
|
26
|
+
### v1.0.1
|
|
27
|
+
|
|
28
|
+
- :bug: Check for schema in library before looking for schema in calling project
|
|
29
|
+
|
|
13
30
|
### v1.0.0
|
|
14
31
|
|
|
15
32
|
- :rocket: Intial Release
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
npm add @openaddresses/batch-schema
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
## Usage
|
|
11
|
+
## Example Usage
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
14
|
const path = require('path');
|
|
@@ -36,12 +36,14 @@ async function server() {
|
|
|
36
36
|
note: 'I only return if the request meets the query & body schemas'
|
|
37
37
|
});
|
|
38
38
|
});
|
|
39
|
-
}
|
|
40
39
|
|
|
41
|
-
// Handle Validation Errors => JSON Middleware
|
|
42
|
-
schema.error();
|
|
40
|
+
// Handle Validation Errors => JSON Middleware
|
|
41
|
+
schema.error();
|
|
42
|
+
}
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
## API
|
|
46
|
+
|
|
45
47
|
```js
|
|
46
48
|
const schema = new Schema(<router>, <opts>);
|
|
47
49
|
|
|
@@ -53,3 +55,24 @@ const schema = new Schema(<router>, <opts>);
|
|
|
53
55
|
| `opts` | Optional Opts Object |
|
|
54
56
|
| `opts.schemas` | Directory of named schemas |
|
|
55
57
|
|
|
58
|
+
|
|
59
|
+
### schema.api
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
await schema.api()
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Adds a route called `GET /schema` which allows the caller to get a list of endpoints that the router manages
|
|
66
|
+
as well as full schema details for every route. If your API is public we recommend enabling this feature, however
|
|
67
|
+
if you do not wish for API routes to be published, this feature is disabled unless called.
|
|
68
|
+
|
|
69
|
+
### schema.error
|
|
70
|
+
|
|
71
|
+
Adds a middlware which will convert validation errors into a standard JSON error format.
|
|
72
|
+
This method should be called after all routes are defined. If this method is not called,
|
|
73
|
+
you must provide your own middleware for converting JSON Schema Validation Errors into
|
|
74
|
+
express compatible responses.
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
schema.error()
|
|
78
|
+
```
|
package/lib/error.js
CHANGED
|
@@ -15,12 +15,21 @@ class PublicError {
|
|
|
15
15
|
|
|
16
16
|
static respond(err, res, messages = []) {
|
|
17
17
|
if (err instanceof PublicError) {
|
|
18
|
-
|
|
19
18
|
res.status(err.status).json({
|
|
20
19
|
status: err.status,
|
|
21
20
|
message: err.safe,
|
|
22
21
|
messages: messages
|
|
23
22
|
});
|
|
23
|
+
|
|
24
|
+
// Duplicate Express supported Error extensions
|
|
25
|
+
} else if (err instanceof Error && (err.status || err.statusCode)) {
|
|
26
|
+
res.status(err.status).json({
|
|
27
|
+
status: err.status || err.statusCode,
|
|
28
|
+
message: err.message || 'Generic Error',
|
|
29
|
+
messages: messages
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Handle generic errors
|
|
24
33
|
} else {
|
|
25
34
|
console.error(err);
|
|
26
35
|
|
package/lib/schema.js
CHANGED
|
@@ -54,6 +54,8 @@ class Schemas {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
this.validator = new Validator({
|
|
57
|
+
removeAdditional: true,
|
|
58
|
+
useDefaults: true,
|
|
57
59
|
allErrors: true
|
|
58
60
|
});
|
|
59
61
|
|
|
@@ -115,7 +117,12 @@ class Schemas {
|
|
|
115
117
|
|
|
116
118
|
for (const type of ['body', 'query', 'res']) {
|
|
117
119
|
if (!schemas[type]) continue;
|
|
118
|
-
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
schemas[type] = await $RefParser.dereference(path.resolve(path.resolve(__dirname, '../schema/'), schemas[type]));
|
|
123
|
+
} catch (err) {
|
|
124
|
+
schemas[type] = await $RefParser.dereference(path.resolve(this.schemas_path, schemas[type]));
|
|
125
|
+
}
|
|
119
126
|
}
|
|
120
127
|
|
|
121
128
|
this.schemas.set(parsed.join(' '), schemas);
|
|
@@ -234,17 +241,12 @@ class Schemas {
|
|
|
234
241
|
this.router.use((err, req, res, next) => {
|
|
235
242
|
if (err instanceof ValidationError) {
|
|
236
243
|
let errs = [];
|
|
237
|
-
|
|
238
244
|
if (err.validationErrors.body) {
|
|
239
|
-
errs = errs.concat(err.validationErrors.body
|
|
240
|
-
return { message: e.message };
|
|
241
|
-
}));
|
|
245
|
+
errs = errs.concat(err.validationErrors.body);
|
|
242
246
|
}
|
|
243
247
|
|
|
244
248
|
if (err.validationErrors.query) {
|
|
245
|
-
errs = errs.concat(err.validationErrors.query
|
|
246
|
-
return { message: e.message };
|
|
247
|
-
}));
|
|
249
|
+
errs = errs.concat(err.validationErrors.query);
|
|
248
250
|
}
|
|
249
251
|
|
|
250
252
|
return Err.respond(
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openaddresses/batch-schema",
|
|
3
3
|
"root": true,
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.2.0",
|
|
5
5
|
"description": "Strongly Validated JSON Schema support for address",
|
|
6
6
|
"repository": "git@github.com:openaddresses/batch",
|
|
7
7
|
"author": "ingalls <nick@ingalls.ca>",
|