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