@middy/validator 2.5.6 → 3.0.0-alpha.3
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/LICENSE +1 -1
- package/README.md +2 -3
- package/index.js +9 -16
- package/package.json +12 -10
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-
|
|
3
|
+
Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -58,12 +58,11 @@ npm install --save @middy/validator
|
|
|
58
58
|
- `ajvOptions` (object) (optional): Options to pass to [ajv](https://ajv.js.org/docs/api.html#options)
|
|
59
59
|
class constructor. Defaults are `{ strict: true, coerceTypes: 'array', allErrors: true, useDefaults: 'empty', messages: false, defaultLanguage: 'en' }`.
|
|
60
60
|
- `i18nEnabled` (boolean) (optional): Option to disable i18n default package. Defaults to `true`
|
|
61
|
-
|
|
62
61
|
NOTES:
|
|
63
62
|
- At least one of `inputSchema` or `outputSchema` is required.
|
|
64
63
|
- **Important** Compiling schemas on the fly will cause a 50-100ms performance hit during cold start for simple JSON Schemas. Precompiling is highly recommended.
|
|
65
64
|
- Default ajv plugins used: `ajv-i18n`, `ajv-formats`, `ajv-formats-draft2019`
|
|
66
|
-
- If you'd like to have the error details as part of the response, it will need to be handled separately. You can access them from `request.error.
|
|
65
|
+
- If you'd like to have the error details as part of the response, it will need to be handled separately. You can access them from `request.error.cause`, the original response can be found at `request.error.response`.
|
|
67
66
|
|
|
68
67
|
## Sample usage
|
|
69
68
|
|
|
@@ -149,7 +148,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
149
148
|
|
|
150
149
|
## License
|
|
151
150
|
|
|
152
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-
|
|
151
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
153
152
|
|
|
154
153
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
155
154
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
package/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { createError } from '@middy/util'
|
|
2
|
+
import _ajv from 'ajv/dist/2019.js'
|
|
3
|
+
import localize from 'ajv-i18n'
|
|
4
|
+
import formats from 'ajv-formats'
|
|
5
|
+
import formatsDraft2019 from 'ajv-formats-draft2019'
|
|
6
6
|
|
|
7
7
|
const Ajv = _ajv.default // esm workaround for linting
|
|
8
8
|
|
|
@@ -40,17 +40,13 @@ const validatorMiddleware = (opts = {}) => {
|
|
|
40
40
|
const valid = inputSchema(request.event)
|
|
41
41
|
|
|
42
42
|
if (!valid) {
|
|
43
|
-
// Bad Request
|
|
44
|
-
const error = createError(400, 'Event object failed validation')
|
|
45
|
-
request.event.headers = { ...request.event.headers }
|
|
46
|
-
|
|
47
43
|
if (i18nEnabled) {
|
|
48
44
|
const language = chooseLanguage(request.event, defaultLanguage)
|
|
49
45
|
localize[language](inputSchema.errors)
|
|
50
46
|
}
|
|
51
47
|
|
|
52
|
-
|
|
53
|
-
throw
|
|
48
|
+
// Bad Request
|
|
49
|
+
throw createError(400, 'Event object failed validation', { cause: inputSchema.errors })
|
|
54
50
|
}
|
|
55
51
|
}
|
|
56
52
|
|
|
@@ -59,10 +55,7 @@ const validatorMiddleware = (opts = {}) => {
|
|
|
59
55
|
|
|
60
56
|
if (!valid) {
|
|
61
57
|
// Internal Server Error
|
|
62
|
-
|
|
63
|
-
error.details = outputSchema.errors
|
|
64
|
-
error.response = request.response
|
|
65
|
-
throw error
|
|
58
|
+
throw createError(500, 'Response object failed validation', { cause: outputSchema.errors })
|
|
66
59
|
}
|
|
67
60
|
}
|
|
68
61
|
return {
|
|
@@ -114,4 +107,4 @@ const chooseLanguage = ({ preferredLanguage }, defaultLanguage) => {
|
|
|
114
107
|
return defaultLanguage
|
|
115
108
|
}
|
|
116
109
|
|
|
117
|
-
|
|
110
|
+
export default validatorMiddleware
|
package/package.json
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/validator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.3",
|
|
4
4
|
"description": "Validator middleware for the middy framework",
|
|
5
|
-
"type": "
|
|
5
|
+
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=14"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"
|
|
13
|
+
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
|
+
"index.js",
|
|
16
17
|
"index.d.ts"
|
|
17
18
|
],
|
|
18
19
|
"scripts": {
|
|
19
20
|
"test": "npm run test:unit",
|
|
20
|
-
"test:unit": "ava"
|
|
21
|
+
"test:unit": "ava",
|
|
22
|
+
"test:benchmark": "node __benchmarks__/index.js"
|
|
21
23
|
},
|
|
22
24
|
"license": "MIT",
|
|
23
25
|
"keywords": [
|
|
@@ -45,16 +47,16 @@
|
|
|
45
47
|
},
|
|
46
48
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
47
49
|
"dependencies": {
|
|
48
|
-
"@middy/util": "^
|
|
49
|
-
"ajv": "8.
|
|
50
|
+
"@middy/util": "^3.0.0-alpha.3",
|
|
51
|
+
"ajv": "8.8.2",
|
|
50
52
|
"ajv-formats": "2.1.1",
|
|
51
53
|
"ajv-formats-draft2019": "1.6.1",
|
|
52
|
-
"ajv-i18n": "4.
|
|
54
|
+
"ajv-i18n": "4.2.0"
|
|
53
55
|
},
|
|
54
56
|
"devDependencies": {
|
|
55
|
-
"@middy/core": "^
|
|
57
|
+
"@middy/core": "^3.0.0-alpha.3",
|
|
56
58
|
"@types/http-errors": "^1.8.1",
|
|
57
59
|
"ajv-bsontype": "^1.0.7"
|
|
58
60
|
},
|
|
59
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "1441158711580313765e6d156046ef0fade0d156"
|
|
60
62
|
}
|