@middy/validator 2.5.3 → 3.0.0-alpha.2
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 -2
- package/index.d.ts +1 -0
- package/index.js +21 -40
- package/package.json +11 -11
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
|
@@ -57,7 +57,7 @@ npm install --save @middy/validator
|
|
|
57
57
|
to validate the output (`request.response`) of the Lambda handler.
|
|
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
61
|
NOTES:
|
|
62
62
|
- At least one of `inputSchema` or `outputSchema` is required.
|
|
63
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.
|
|
@@ -148,7 +148,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
148
148
|
|
|
149
149
|
## License
|
|
150
150
|
|
|
151
|
-
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).
|
|
152
152
|
|
|
153
153
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
154
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.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,35 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const _ajv = require('ajv/dist/2019');
|
|
8
|
-
|
|
9
|
-
const localize = require('ajv-i18n');
|
|
10
|
-
|
|
11
|
-
const formats = require('ajv-formats');
|
|
12
|
-
|
|
13
|
-
const formatsDraft2019 = require('ajv-formats-draft2019');
|
|
14
|
-
|
|
15
|
-
const Ajv = _ajv.default; // esm workaround for linting
|
|
16
|
-
|
|
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
|
+
const Ajv = _ajv.default;
|
|
17
7
|
let ajv;
|
|
18
8
|
const ajvDefaults = {
|
|
19
9
|
strict: true,
|
|
20
10
|
coerceTypes: 'array',
|
|
21
|
-
// important for query string params
|
|
22
11
|
allErrors: true,
|
|
23
12
|
useDefaults: 'empty',
|
|
24
|
-
messages: false
|
|
25
|
-
|
|
13
|
+
messages: false
|
|
26
14
|
};
|
|
27
15
|
const defaults = {
|
|
28
16
|
inputSchema: undefined,
|
|
29
17
|
outputSchema: undefined,
|
|
30
18
|
ajvOptions: {},
|
|
31
19
|
ajvInstance: undefined,
|
|
32
|
-
defaultLanguage: 'en'
|
|
20
|
+
defaultLanguage: 'en',
|
|
21
|
+
i18nEnabled: true
|
|
33
22
|
};
|
|
34
23
|
|
|
35
24
|
const validatorMiddleware = (opts = {}) => {
|
|
@@ -38,7 +27,8 @@ const validatorMiddleware = (opts = {}) => {
|
|
|
38
27
|
outputSchema,
|
|
39
28
|
ajvOptions,
|
|
40
29
|
ajvInstance,
|
|
41
|
-
defaultLanguage
|
|
30
|
+
defaultLanguage,
|
|
31
|
+
i18nEnabled
|
|
42
32
|
} = { ...defaults,
|
|
43
33
|
...opts
|
|
44
34
|
};
|
|
@@ -49,12 +39,15 @@ const validatorMiddleware = (opts = {}) => {
|
|
|
49
39
|
const valid = inputSchema(request.event);
|
|
50
40
|
|
|
51
41
|
if (!valid) {
|
|
52
|
-
// Bad Request
|
|
53
42
|
const error = createError(400, 'Event object failed validation');
|
|
54
43
|
request.event.headers = { ...request.event.headers
|
|
55
44
|
};
|
|
56
|
-
|
|
57
|
-
|
|
45
|
+
|
|
46
|
+
if (i18nEnabled) {
|
|
47
|
+
const language = chooseLanguage(request.event, defaultLanguage);
|
|
48
|
+
localize[language](inputSchema.errors);
|
|
49
|
+
}
|
|
50
|
+
|
|
58
51
|
error.details = inputSchema.errors;
|
|
59
52
|
throw error;
|
|
60
53
|
}
|
|
@@ -64,7 +57,6 @@ const validatorMiddleware = (opts = {}) => {
|
|
|
64
57
|
const valid = outputSchema(request.response);
|
|
65
58
|
|
|
66
59
|
if (!valid) {
|
|
67
|
-
// Internal Server Error
|
|
68
60
|
const error = createError(500, 'Response object failed validation');
|
|
69
61
|
error.details = outputSchema.errors;
|
|
70
62
|
error.response = request.response;
|
|
@@ -76,23 +68,19 @@ const validatorMiddleware = (opts = {}) => {
|
|
|
76
68
|
before: inputSchema ? validatorMiddlewareBefore : undefined,
|
|
77
69
|
after: outputSchema ? validatorMiddlewareAfter : undefined
|
|
78
70
|
};
|
|
79
|
-
};
|
|
80
|
-
// Precompile your schema during a build step is recommended.
|
|
81
|
-
|
|
71
|
+
};
|
|
82
72
|
|
|
83
73
|
const compile = (schema, ajvOptions, ajvInstance = null) => {
|
|
84
|
-
// Check if already compiled
|
|
85
74
|
if (typeof schema === 'function' || !schema) return schema;
|
|
86
75
|
const options = { ...ajvDefaults,
|
|
87
76
|
...ajvOptions
|
|
88
77
|
};
|
|
89
78
|
|
|
90
79
|
if (!ajv) {
|
|
91
|
-
ajv = ajvInstance
|
|
80
|
+
ajv = ajvInstance ?? new Ajv(options);
|
|
92
81
|
formats(ajv);
|
|
93
82
|
formatsDraft2019(ajv);
|
|
94
83
|
} else if (!ajvInstance) {
|
|
95
|
-
// Update options when initializing the middleware multiple times
|
|
96
84
|
ajv.opts = { ...ajv.opts,
|
|
97
85
|
...options
|
|
98
86
|
};
|
|
@@ -100,25 +88,18 @@ const compile = (schema, ajvOptions, ajvInstance = null) => {
|
|
|
100
88
|
|
|
101
89
|
return ajv.compile(schema);
|
|
102
90
|
};
|
|
103
|
-
/* in ajv-i18n Portuguese is represented as pt-BR */
|
|
104
|
-
|
|
105
91
|
|
|
106
92
|
const languageNormalizationMap = {
|
|
107
93
|
pt: 'pt-BR',
|
|
108
94
|
'pt-br': 'pt-BR',
|
|
109
95
|
pt_BR: 'pt-BR',
|
|
110
96
|
pt_br: 'pt-BR',
|
|
111
|
-
zh: 'zh-TW',
|
|
112
97
|
'zh-tw': 'zh-TW',
|
|
113
98
|
zh_TW: 'zh-TW',
|
|
114
99
|
zh_tw: 'zh-TW'
|
|
115
100
|
};
|
|
116
101
|
|
|
117
|
-
const normalizePreferredLanguage = lang =>
|
|
118
|
-
var _languageNormalizatio;
|
|
119
|
-
|
|
120
|
-
return (_languageNormalizatio = languageNormalizationMap[lang]) !== null && _languageNormalizatio !== void 0 ? _languageNormalizatio : lang;
|
|
121
|
-
};
|
|
102
|
+
const normalizePreferredLanguage = lang => languageNormalizationMap[lang] ?? lang;
|
|
122
103
|
|
|
123
104
|
const availableLanguages = Object.keys(localize);
|
|
124
105
|
|
|
@@ -136,4 +117,4 @@ const chooseLanguage = ({
|
|
|
136
117
|
return defaultLanguage;
|
|
137
118
|
};
|
|
138
119
|
|
|
139
|
-
|
|
120
|
+
export default validatorMiddleware;
|
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/validator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.2",
|
|
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": {
|
|
@@ -45,17 +46,16 @@
|
|
|
45
46
|
},
|
|
46
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
47
48
|
"dependencies": {
|
|
48
|
-
"@middy/util": "^
|
|
49
|
-
"ajv": "8.
|
|
49
|
+
"@middy/util": "^3.0.0-alpha.2",
|
|
50
|
+
"ajv": "8.8.2",
|
|
50
51
|
"ajv-formats": "2.1.1",
|
|
51
52
|
"ajv-formats-draft2019": "1.6.1",
|
|
52
|
-
"ajv-i18n": "4.
|
|
53
|
+
"ajv-i18n": "4.2.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
|
-
"@middy/core": "^
|
|
56
|
+
"@middy/core": "^3.0.0-alpha.2",
|
|
56
57
|
"@types/http-errors": "^1.8.1",
|
|
57
|
-
"ajv-bsontype": "^1.0.7"
|
|
58
|
-
"http-errors": "^1.8.0"
|
|
58
|
+
"ajv-bsontype": "^1.0.7"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "de30419273ecbff08f367f47c7e320ec981cf145"
|
|
61
61
|
}
|