@middy/validator 2.5.2 → 2.5.6
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/README.md +1 -0
- package/index.d.ts +1 -0
- package/index.js +59 -81
- package/package.json +5 -6
package/README.md
CHANGED
|
@@ -57,6 +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
|
+
- `i18nEnabled` (boolean) (optional): Option to disable i18n default package. Defaults to `true`
|
|
60
61
|
|
|
61
62
|
NOTES:
|
|
62
63
|
- At least one of `inputSchema` or `outputSchema` is required.
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,36 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
const { createError } = require('@middy/util')
|
|
2
|
+
const _ajv = require('ajv/dist/2019')
|
|
3
|
+
const localize = require('ajv-i18n')
|
|
4
|
+
const formats = require('ajv-formats')
|
|
5
|
+
const formatsDraft2019 = require('ajv-formats-draft2019')
|
|
2
6
|
|
|
3
|
-
const
|
|
4
|
-
createError
|
|
5
|
-
} = require('@middy/util');
|
|
7
|
+
const Ajv = _ajv.default // esm workaround for linting
|
|
6
8
|
|
|
7
|
-
|
|
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
|
-
|
|
17
|
-
let ajv;
|
|
9
|
+
let ajv
|
|
18
10
|
const ajvDefaults = {
|
|
19
11
|
strict: true,
|
|
20
|
-
coerceTypes: 'array',
|
|
21
|
-
// important for query string params
|
|
12
|
+
coerceTypes: 'array', // important for query string params
|
|
22
13
|
allErrors: true,
|
|
23
14
|
useDefaults: 'empty',
|
|
24
15
|
messages: false // allow i18n
|
|
16
|
+
}
|
|
25
17
|
|
|
26
|
-
};
|
|
27
18
|
const defaults = {
|
|
28
19
|
inputSchema: undefined,
|
|
29
20
|
outputSchema: undefined,
|
|
30
21
|
ajvOptions: {},
|
|
31
22
|
ajvInstance: undefined,
|
|
32
|
-
defaultLanguage: 'en'
|
|
33
|
-
|
|
23
|
+
defaultLanguage: 'en',
|
|
24
|
+
i18nEnabled: true
|
|
25
|
+
}
|
|
34
26
|
|
|
35
27
|
const validatorMiddleware = (opts = {}) => {
|
|
36
28
|
let {
|
|
@@ -38,102 +30,88 @@ const validatorMiddleware = (opts = {}) => {
|
|
|
38
30
|
outputSchema,
|
|
39
31
|
ajvOptions,
|
|
40
32
|
ajvInstance,
|
|
41
|
-
defaultLanguage
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
outputSchema = compile(outputSchema, ajvOptions, ajvInstance);
|
|
33
|
+
defaultLanguage,
|
|
34
|
+
i18nEnabled
|
|
35
|
+
} = { ...defaults, ...opts }
|
|
36
|
+
inputSchema = compile(inputSchema, ajvOptions, ajvInstance)
|
|
37
|
+
outputSchema = compile(outputSchema, ajvOptions, ajvInstance)
|
|
47
38
|
|
|
48
|
-
const validatorMiddlewareBefore = async request => {
|
|
49
|
-
const valid = inputSchema(request.event)
|
|
39
|
+
const validatorMiddlewareBefore = async (request) => {
|
|
40
|
+
const valid = inputSchema(request.event)
|
|
50
41
|
|
|
51
42
|
if (!valid) {
|
|
52
43
|
// Bad Request
|
|
53
|
-
const error = createError(400, 'Event object failed validation')
|
|
54
|
-
request.event.headers = { ...request.event.headers
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
44
|
+
const error = createError(400, 'Event object failed validation')
|
|
45
|
+
request.event.headers = { ...request.event.headers }
|
|
46
|
+
|
|
47
|
+
if (i18nEnabled) {
|
|
48
|
+
const language = chooseLanguage(request.event, defaultLanguage)
|
|
49
|
+
localize[language](inputSchema.errors)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
error.details = inputSchema.errors
|
|
53
|
+
throw error
|
|
60
54
|
}
|
|
61
|
-
}
|
|
55
|
+
}
|
|
62
56
|
|
|
63
|
-
const validatorMiddlewareAfter = async request => {
|
|
64
|
-
const valid = outputSchema(request.response)
|
|
57
|
+
const validatorMiddlewareAfter = async (request) => {
|
|
58
|
+
const valid = outputSchema(request.response)
|
|
65
59
|
|
|
66
60
|
if (!valid) {
|
|
67
61
|
// Internal Server Error
|
|
68
|
-
const error = createError(500, 'Response object failed validation')
|
|
69
|
-
error.details = outputSchema.errors
|
|
70
|
-
error.response = request.response
|
|
71
|
-
throw error
|
|
62
|
+
const error = createError(500, 'Response object failed validation')
|
|
63
|
+
error.details = outputSchema.errors
|
|
64
|
+
error.response = request.response
|
|
65
|
+
throw error
|
|
72
66
|
}
|
|
73
|
-
}
|
|
74
|
-
|
|
67
|
+
}
|
|
75
68
|
return {
|
|
76
69
|
before: inputSchema ? validatorMiddlewareBefore : undefined,
|
|
77
70
|
after: outputSchema ? validatorMiddlewareAfter : undefined
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
// Precompile your schema during a build step is recommended.
|
|
81
|
-
|
|
71
|
+
}
|
|
72
|
+
}
|
|
82
73
|
|
|
74
|
+
// This is pulled out due to it's performance cost (50-100ms on cold start)
|
|
75
|
+
// Precompile your schema during a build step is recommended.
|
|
83
76
|
const compile = (schema, ajvOptions, ajvInstance = null) => {
|
|
84
77
|
// Check if already compiled
|
|
85
|
-
if (typeof schema === 'function' || !schema) return schema
|
|
86
|
-
const options = { ...ajvDefaults,
|
|
87
|
-
...ajvOptions
|
|
88
|
-
};
|
|
89
|
-
|
|
78
|
+
if (typeof schema === 'function' || !schema) return schema
|
|
79
|
+
const options = { ...ajvDefaults, ...ajvOptions }
|
|
90
80
|
if (!ajv) {
|
|
91
|
-
ajv = ajvInstance
|
|
92
|
-
formats(ajv)
|
|
93
|
-
formatsDraft2019(ajv)
|
|
81
|
+
ajv = ajvInstance ?? new Ajv(options)
|
|
82
|
+
formats(ajv)
|
|
83
|
+
formatsDraft2019(ajv)
|
|
94
84
|
} else if (!ajvInstance) {
|
|
95
85
|
// Update options when initializing the middleware multiple times
|
|
96
|
-
ajv.opts = { ...ajv.opts,
|
|
97
|
-
...options
|
|
98
|
-
};
|
|
86
|
+
ajv.opts = { ...ajv.opts, ...options }
|
|
99
87
|
}
|
|
88
|
+
return ajv.compile(schema)
|
|
89
|
+
}
|
|
100
90
|
|
|
101
|
-
return ajv.compile(schema);
|
|
102
|
-
};
|
|
103
91
|
/* in ajv-i18n Portuguese is represented as pt-BR */
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
}
|
|
116
|
-
|
|
117
|
-
const normalizePreferredLanguage = lang => {
|
|
118
|
-
var _languageNormalizatio;
|
|
100
|
+
}
|
|
119
101
|
|
|
120
|
-
|
|
121
|
-
|
|
102
|
+
const normalizePreferredLanguage = (lang) =>
|
|
103
|
+
languageNormalizationMap[lang] ?? lang
|
|
122
104
|
|
|
123
|
-
const availableLanguages = Object.keys(localize)
|
|
124
|
-
|
|
125
|
-
const chooseLanguage = ({
|
|
126
|
-
preferredLanguage
|
|
127
|
-
}, defaultLanguage) => {
|
|
105
|
+
const availableLanguages = Object.keys(localize)
|
|
106
|
+
const chooseLanguage = ({ preferredLanguage }, defaultLanguage) => {
|
|
128
107
|
if (preferredLanguage) {
|
|
129
|
-
const lang = normalizePreferredLanguage(preferredLanguage)
|
|
130
|
-
|
|
108
|
+
const lang = normalizePreferredLanguage(preferredLanguage)
|
|
131
109
|
if (availableLanguages.includes(lang)) {
|
|
132
|
-
return lang
|
|
110
|
+
return lang
|
|
133
111
|
}
|
|
134
112
|
}
|
|
135
113
|
|
|
136
|
-
return defaultLanguage
|
|
137
|
-
}
|
|
114
|
+
return defaultLanguage
|
|
115
|
+
}
|
|
138
116
|
|
|
139
|
-
module.exports = validatorMiddleware
|
|
117
|
+
module.exports = validatorMiddleware
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/validator",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.6",
|
|
4
4
|
"description": "Validator middleware for the middy framework",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"engines": {
|
|
@@ -45,17 +45,16 @@
|
|
|
45
45
|
},
|
|
46
46
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@middy/util": "^2.5.
|
|
48
|
+
"@middy/util": "^2.5.6",
|
|
49
49
|
"ajv": "8.6.3",
|
|
50
50
|
"ajv-formats": "2.1.1",
|
|
51
51
|
"ajv-formats-draft2019": "1.6.1",
|
|
52
52
|
"ajv-i18n": "4.1.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@middy/core": "^2.5.
|
|
55
|
+
"@middy/core": "^2.5.6",
|
|
56
56
|
"@types/http-errors": "^1.8.1",
|
|
57
|
-
"ajv-bsontype": "^1.0.7"
|
|
58
|
-
"http-errors": "^1.8.0"
|
|
57
|
+
"ajv-bsontype": "^1.0.7"
|
|
59
58
|
},
|
|
60
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
|
|
61
60
|
}
|