@middy/validator 3.0.3 → 3.0.4
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.cjs +121 -1
- package/index.js +110 -1
- package/package.json +4 -4
package/index.cjs
CHANGED
|
@@ -1,3 +1,123 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
module.exports = void 0;
|
|
6
|
+
var _util = require("@middy/util");
|
|
7
|
+
var _2020Js = _interopRequireDefault(require("ajv/dist/2020.js"));
|
|
8
|
+
var _ajvI18N = _interopRequireDefault(require("ajv-i18n"));
|
|
9
|
+
var _ajvFormats = _interopRequireDefault(require("ajv-formats"));
|
|
10
|
+
var _ajvFormatsDraft2019 = _interopRequireDefault(require("ajv-formats-draft2019"));
|
|
11
|
+
var _typeofJs = _interopRequireDefault(require("ajv-keywords/dist/definitions/typeof.js"));
|
|
12
|
+
function _interopRequireDefault(obj) {
|
|
13
|
+
return obj && obj.__esModule ? obj : {
|
|
14
|
+
default: obj
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const Ajv = _2020Js.default.default;
|
|
18
|
+
let ajv;
|
|
19
|
+
const ajvDefaults = {
|
|
20
|
+
strict: true,
|
|
21
|
+
coerceTypes: 'array',
|
|
22
|
+
allErrors: true,
|
|
23
|
+
useDefaults: 'empty',
|
|
24
|
+
messages: false,
|
|
25
|
+
keywords: [
|
|
26
|
+
(0, _typeofJs).default()
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
const defaults = {
|
|
30
|
+
eventSchema: undefined,
|
|
31
|
+
contextSchema: undefined,
|
|
32
|
+
responseSchema: undefined,
|
|
33
|
+
ajvOptions: {},
|
|
34
|
+
ajvInstance: undefined,
|
|
35
|
+
defaultLanguage: 'en',
|
|
36
|
+
i18nEnabled: true
|
|
37
|
+
};
|
|
38
|
+
const validatorMiddleware = (opts = {})=>{
|
|
39
|
+
let { inputSchema , outputSchema , eventSchema , contextSchema , responseSchema , ajvOptions , ajvInstance , defaultLanguage , i18nEnabled } = {
|
|
40
|
+
...defaults,
|
|
41
|
+
...opts
|
|
42
|
+
};
|
|
43
|
+
eventSchema = compile(eventSchema ?? inputSchema, ajvOptions, ajvInstance);
|
|
44
|
+
contextSchema = compile(contextSchema, ajvOptions, ajvInstance);
|
|
45
|
+
responseSchema = compile(responseSchema ?? outputSchema, ajvOptions, ajvInstance);
|
|
46
|
+
const validatorMiddlewareBefore = async (request)=>{
|
|
47
|
+
if (eventSchema) {
|
|
48
|
+
const validEvent = await eventSchema(request.event);
|
|
49
|
+
if (!validEvent) {
|
|
50
|
+
if (i18nEnabled) {
|
|
51
|
+
const language = chooseLanguage(request.event, defaultLanguage);
|
|
52
|
+
_ajvI18N.default[language](eventSchema.errors);
|
|
53
|
+
}
|
|
54
|
+
const error = (0, _util).createError(400, 'Event object failed validation');
|
|
55
|
+
error.cause = eventSchema.errors;
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (contextSchema) {
|
|
60
|
+
const validContext = await contextSchema(request.context);
|
|
61
|
+
if (!validContext) {
|
|
62
|
+
const error = (0, _util).createError(500, 'Context object failed validation');
|
|
63
|
+
error.cause = contextSchema.errors;
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const validatorMiddlewareAfter = async (request)=>{
|
|
69
|
+
const valid = await responseSchema(request.response);
|
|
70
|
+
if (!valid) {
|
|
71
|
+
const error = (0, _util).createError(500, 'Response object failed validation');
|
|
72
|
+
error.cause = responseSchema.errors;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
before: (eventSchema ?? inputSchema) ?? contextSchema ? validatorMiddlewareBefore : undefined,
|
|
78
|
+
after: responseSchema ?? outputSchema ? validatorMiddlewareAfter : undefined
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
const compile = (schema, ajvOptions, ajvInstance = null)=>{
|
|
82
|
+
if (typeof schema === 'function' || !schema) return schema;
|
|
83
|
+
const options = {
|
|
84
|
+
...ajvDefaults,
|
|
85
|
+
...ajvOptions
|
|
86
|
+
};
|
|
87
|
+
if (!ajv) {
|
|
88
|
+
ajv = ajvInstance ?? new Ajv(options);
|
|
89
|
+
(0, _ajvFormats).default(ajv);
|
|
90
|
+
(0, _ajvFormatsDraft2019).default(ajv);
|
|
91
|
+
} else if (!ajvInstance) {
|
|
92
|
+
ajv.opts = {
|
|
93
|
+
...ajv.opts,
|
|
94
|
+
...options
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return ajv.compile(schema);
|
|
98
|
+
};
|
|
99
|
+
const languageNormalizationMap = {
|
|
100
|
+
pt: 'pt-BR',
|
|
101
|
+
'pt-br': 'pt-BR',
|
|
102
|
+
pt_BR: 'pt-BR',
|
|
103
|
+
pt_br: 'pt-BR',
|
|
104
|
+
'zh-tw': 'zh-TW',
|
|
105
|
+
zh_TW: 'zh-TW',
|
|
106
|
+
zh_tw: 'zh-TW'
|
|
107
|
+
};
|
|
108
|
+
const normalizePreferredLanguage = (lang)=>languageNormalizationMap[lang] ?? lang;
|
|
109
|
+
const availableLanguages = Object.keys(_ajvI18N.default);
|
|
110
|
+
const chooseLanguage = ({ preferredLanguage }, defaultLanguage)=>{
|
|
111
|
+
if (preferredLanguage) {
|
|
112
|
+
const lang = normalizePreferredLanguage(preferredLanguage);
|
|
113
|
+
if (availableLanguages.includes(lang)) {
|
|
114
|
+
return lang;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return defaultLanguage;
|
|
118
|
+
};
|
|
119
|
+
var _default = validatorMiddleware;
|
|
120
|
+
module.exports = _default;
|
|
121
|
+
|
|
2
122
|
|
|
3
123
|
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,3 +1,112 @@
|
|
|
1
|
-
import{createError}from
|
|
1
|
+
import { createError } from '@middy/util';
|
|
2
|
+
import _ajv from 'ajv/dist/2020.js';
|
|
3
|
+
import localize from 'ajv-i18n';
|
|
4
|
+
import formats from 'ajv-formats';
|
|
5
|
+
import formatsDraft2019 from 'ajv-formats-draft2019';
|
|
6
|
+
import typeofKeyword from 'ajv-keywords/dist/definitions/typeof.js';
|
|
7
|
+
const Ajv = _ajv.default;
|
|
8
|
+
let ajv;
|
|
9
|
+
const ajvDefaults = {
|
|
10
|
+
strict: true,
|
|
11
|
+
coerceTypes: 'array',
|
|
12
|
+
allErrors: true,
|
|
13
|
+
useDefaults: 'empty',
|
|
14
|
+
messages: false,
|
|
15
|
+
keywords: [
|
|
16
|
+
typeofKeyword()
|
|
17
|
+
]
|
|
18
|
+
};
|
|
19
|
+
const defaults = {
|
|
20
|
+
eventSchema: undefined,
|
|
21
|
+
contextSchema: undefined,
|
|
22
|
+
responseSchema: undefined,
|
|
23
|
+
ajvOptions: {},
|
|
24
|
+
ajvInstance: undefined,
|
|
25
|
+
defaultLanguage: 'en',
|
|
26
|
+
i18nEnabled: true
|
|
27
|
+
};
|
|
28
|
+
const validatorMiddleware = (opts = {})=>{
|
|
29
|
+
let { inputSchema , outputSchema , eventSchema , contextSchema , responseSchema , ajvOptions , ajvInstance , defaultLanguage , i18nEnabled } = {
|
|
30
|
+
...defaults,
|
|
31
|
+
...opts
|
|
32
|
+
};
|
|
33
|
+
eventSchema = compile(eventSchema ?? inputSchema, ajvOptions, ajvInstance);
|
|
34
|
+
contextSchema = compile(contextSchema, ajvOptions, ajvInstance);
|
|
35
|
+
responseSchema = compile(responseSchema ?? outputSchema, ajvOptions, ajvInstance);
|
|
36
|
+
const validatorMiddlewareBefore = async (request)=>{
|
|
37
|
+
if (eventSchema) {
|
|
38
|
+
const validEvent = await eventSchema(request.event);
|
|
39
|
+
if (!validEvent) {
|
|
40
|
+
if (i18nEnabled) {
|
|
41
|
+
const language = chooseLanguage(request.event, defaultLanguage);
|
|
42
|
+
localize[language](eventSchema.errors);
|
|
43
|
+
}
|
|
44
|
+
const error = createError(400, 'Event object failed validation');
|
|
45
|
+
error.cause = eventSchema.errors;
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (contextSchema) {
|
|
50
|
+
const validContext = await contextSchema(request.context);
|
|
51
|
+
if (!validContext) {
|
|
52
|
+
const error = createError(500, 'Context object failed validation');
|
|
53
|
+
error.cause = contextSchema.errors;
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const validatorMiddlewareAfter = async (request)=>{
|
|
59
|
+
const valid = await responseSchema(request.response);
|
|
60
|
+
if (!valid) {
|
|
61
|
+
const error = createError(500, 'Response object failed validation');
|
|
62
|
+
error.cause = responseSchema.errors;
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
before: (eventSchema ?? inputSchema) ?? contextSchema ? validatorMiddlewareBefore : undefined,
|
|
68
|
+
after: responseSchema ?? outputSchema ? validatorMiddlewareAfter : undefined
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const compile = (schema, ajvOptions, ajvInstance = null)=>{
|
|
72
|
+
if (typeof schema === 'function' || !schema) return schema;
|
|
73
|
+
const options = {
|
|
74
|
+
...ajvDefaults,
|
|
75
|
+
...ajvOptions
|
|
76
|
+
};
|
|
77
|
+
if (!ajv) {
|
|
78
|
+
ajv = ajvInstance ?? new Ajv(options);
|
|
79
|
+
formats(ajv);
|
|
80
|
+
formatsDraft2019(ajv);
|
|
81
|
+
} else if (!ajvInstance) {
|
|
82
|
+
ajv.opts = {
|
|
83
|
+
...ajv.opts,
|
|
84
|
+
...options
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
return ajv.compile(schema);
|
|
88
|
+
};
|
|
89
|
+
const languageNormalizationMap = {
|
|
90
|
+
pt: 'pt-BR',
|
|
91
|
+
'pt-br': 'pt-BR',
|
|
92
|
+
pt_BR: 'pt-BR',
|
|
93
|
+
pt_br: 'pt-BR',
|
|
94
|
+
'zh-tw': 'zh-TW',
|
|
95
|
+
zh_TW: 'zh-TW',
|
|
96
|
+
zh_tw: 'zh-TW'
|
|
97
|
+
};
|
|
98
|
+
const normalizePreferredLanguage = (lang)=>languageNormalizationMap[lang] ?? lang;
|
|
99
|
+
const availableLanguages = Object.keys(localize);
|
|
100
|
+
const chooseLanguage = ({ preferredLanguage }, defaultLanguage)=>{
|
|
101
|
+
if (preferredLanguage) {
|
|
102
|
+
const lang = normalizePreferredLanguage(preferredLanguage);
|
|
103
|
+
if (availableLanguages.includes(lang)) {
|
|
104
|
+
return lang;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return defaultLanguage;
|
|
108
|
+
};
|
|
109
|
+
export default validatorMiddleware;
|
|
110
|
+
|
|
2
111
|
|
|
3
112
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/validator",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "Validator middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://middy.js.org",
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@middy/util": "3.0.
|
|
57
|
+
"@middy/util": "3.0.4",
|
|
58
58
|
"ajv": "8.11.0",
|
|
59
59
|
"ajv-formats": "2.1.1",
|
|
60
60
|
"ajv-formats-draft2019": "1.6.1",
|
|
@@ -62,9 +62,9 @@
|
|
|
62
62
|
"ajv-keywords": "5.1.0"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@middy/core": "3.0.
|
|
65
|
+
"@middy/core": "3.0.4",
|
|
66
66
|
"@types/http-errors": "^1.8.1",
|
|
67
67
|
"ajv-bsontype": "^1.0.7"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "3e9bc83e791f943c71cd7003fc27f0a3692d83a1"
|
|
70
70
|
}
|