@middy/validator 2.5.5 → 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/index.js +51 -78
- package/package.json +5 -6
package/index.js
CHANGED
|
@@ -1,29 +1,20 @@
|
|
|
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,
|
|
@@ -31,7 +22,7 @@ const defaults = {
|
|
|
31
22
|
ajvInstance: undefined,
|
|
32
23
|
defaultLanguage: 'en',
|
|
33
24
|
i18nEnabled: true
|
|
34
|
-
}
|
|
25
|
+
}
|
|
35
26
|
|
|
36
27
|
const validatorMiddleware = (opts = {}) => {
|
|
37
28
|
let {
|
|
@@ -41,74 +32,63 @@ const validatorMiddleware = (opts = {}) => {
|
|
|
41
32
|
ajvInstance,
|
|
42
33
|
defaultLanguage,
|
|
43
34
|
i18nEnabled
|
|
44
|
-
} = { ...defaults,
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
inputSchema = compile(inputSchema, ajvOptions, ajvInstance);
|
|
48
|
-
outputSchema = compile(outputSchema, ajvOptions, ajvInstance);
|
|
35
|
+
} = { ...defaults, ...opts }
|
|
36
|
+
inputSchema = compile(inputSchema, ajvOptions, ajvInstance)
|
|
37
|
+
outputSchema = compile(outputSchema, ajvOptions, ajvInstance)
|
|
49
38
|
|
|
50
|
-
const validatorMiddlewareBefore = async request => {
|
|
51
|
-
const valid = inputSchema(request.event)
|
|
39
|
+
const validatorMiddlewareBefore = async (request) => {
|
|
40
|
+
const valid = inputSchema(request.event)
|
|
52
41
|
|
|
53
42
|
if (!valid) {
|
|
54
43
|
// Bad Request
|
|
55
|
-
const error = createError(400, 'Event object failed validation')
|
|
56
|
-
request.event.headers = { ...request.event.headers
|
|
57
|
-
};
|
|
44
|
+
const error = createError(400, 'Event object failed validation')
|
|
45
|
+
request.event.headers = { ...request.event.headers }
|
|
58
46
|
|
|
59
47
|
if (i18nEnabled) {
|
|
60
|
-
const language = chooseLanguage(request.event, defaultLanguage)
|
|
61
|
-
localize[language](inputSchema.errors)
|
|
48
|
+
const language = chooseLanguage(request.event, defaultLanguage)
|
|
49
|
+
localize[language](inputSchema.errors)
|
|
62
50
|
}
|
|
63
51
|
|
|
64
|
-
error.details = inputSchema.errors
|
|
65
|
-
throw error
|
|
52
|
+
error.details = inputSchema.errors
|
|
53
|
+
throw error
|
|
66
54
|
}
|
|
67
|
-
}
|
|
55
|
+
}
|
|
68
56
|
|
|
69
|
-
const validatorMiddlewareAfter = async request => {
|
|
70
|
-
const valid = outputSchema(request.response)
|
|
57
|
+
const validatorMiddlewareAfter = async (request) => {
|
|
58
|
+
const valid = outputSchema(request.response)
|
|
71
59
|
|
|
72
60
|
if (!valid) {
|
|
73
61
|
// Internal Server Error
|
|
74
|
-
const error = createError(500, 'Response object failed validation')
|
|
75
|
-
error.details = outputSchema.errors
|
|
76
|
-
error.response = request.response
|
|
77
|
-
throw error
|
|
62
|
+
const error = createError(500, 'Response object failed validation')
|
|
63
|
+
error.details = outputSchema.errors
|
|
64
|
+
error.response = request.response
|
|
65
|
+
throw error
|
|
78
66
|
}
|
|
79
|
-
}
|
|
80
|
-
|
|
67
|
+
}
|
|
81
68
|
return {
|
|
82
69
|
before: inputSchema ? validatorMiddlewareBefore : undefined,
|
|
83
70
|
after: outputSchema ? validatorMiddlewareAfter : undefined
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
// Precompile your schema during a build step is recommended.
|
|
87
|
-
|
|
71
|
+
}
|
|
72
|
+
}
|
|
88
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.
|
|
89
76
|
const compile = (schema, ajvOptions, ajvInstance = null) => {
|
|
90
77
|
// Check if already compiled
|
|
91
|
-
if (typeof schema === 'function' || !schema) return schema
|
|
92
|
-
const options = { ...ajvDefaults,
|
|
93
|
-
...ajvOptions
|
|
94
|
-
};
|
|
95
|
-
|
|
78
|
+
if (typeof schema === 'function' || !schema) return schema
|
|
79
|
+
const options = { ...ajvDefaults, ...ajvOptions }
|
|
96
80
|
if (!ajv) {
|
|
97
|
-
ajv = ajvInstance
|
|
98
|
-
formats(ajv)
|
|
99
|
-
formatsDraft2019(ajv)
|
|
81
|
+
ajv = ajvInstance ?? new Ajv(options)
|
|
82
|
+
formats(ajv)
|
|
83
|
+
formatsDraft2019(ajv)
|
|
100
84
|
} else if (!ajvInstance) {
|
|
101
85
|
// Update options when initializing the middleware multiple times
|
|
102
|
-
ajv.opts = { ...ajv.opts,
|
|
103
|
-
...options
|
|
104
|
-
};
|
|
86
|
+
ajv.opts = { ...ajv.opts, ...options }
|
|
105
87
|
}
|
|
88
|
+
return ajv.compile(schema)
|
|
89
|
+
}
|
|
106
90
|
|
|
107
|
-
return ajv.compile(schema);
|
|
108
|
-
};
|
|
109
91
|
/* in ajv-i18n Portuguese is represented as pt-BR */
|
|
110
|
-
|
|
111
|
-
|
|
112
92
|
const languageNormalizationMap = {
|
|
113
93
|
pt: 'pt-BR',
|
|
114
94
|
'pt-br': 'pt-BR',
|
|
@@ -117,28 +97,21 @@ const languageNormalizationMap = {
|
|
|
117
97
|
'zh-tw': 'zh-TW',
|
|
118
98
|
zh_TW: 'zh-TW',
|
|
119
99
|
zh_tw: 'zh-TW'
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const normalizePreferredLanguage = lang => {
|
|
123
|
-
var _languageNormalizatio;
|
|
100
|
+
}
|
|
124
101
|
|
|
125
|
-
|
|
126
|
-
|
|
102
|
+
const normalizePreferredLanguage = (lang) =>
|
|
103
|
+
languageNormalizationMap[lang] ?? lang
|
|
127
104
|
|
|
128
|
-
const availableLanguages = Object.keys(localize)
|
|
129
|
-
|
|
130
|
-
const chooseLanguage = ({
|
|
131
|
-
preferredLanguage
|
|
132
|
-
}, defaultLanguage) => {
|
|
105
|
+
const availableLanguages = Object.keys(localize)
|
|
106
|
+
const chooseLanguage = ({ preferredLanguage }, defaultLanguage) => {
|
|
133
107
|
if (preferredLanguage) {
|
|
134
|
-
const lang = normalizePreferredLanguage(preferredLanguage)
|
|
135
|
-
|
|
108
|
+
const lang = normalizePreferredLanguage(preferredLanguage)
|
|
136
109
|
if (availableLanguages.includes(lang)) {
|
|
137
|
-
return lang
|
|
110
|
+
return lang
|
|
138
111
|
}
|
|
139
112
|
}
|
|
140
113
|
|
|
141
|
-
return defaultLanguage
|
|
142
|
-
}
|
|
114
|
+
return defaultLanguage
|
|
115
|
+
}
|
|
143
116
|
|
|
144
|
-
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
|
}
|