@middy/validator 3.0.3 → 3.1.0-rc.1
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 +3 -3
- package/index.cjs +123 -1
- package/index.js +112 -1
- package/package.json +15 -8
package/README.md
CHANGED
|
@@ -64,7 +64,7 @@ npm install --save @middy/validator
|
|
|
64
64
|
|
|
65
65
|
- `eventSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
|
|
66
66
|
to validate the input (`request.event`) of the Lambda handler. Supports alias `inputSchema`
|
|
67
|
-
- `contextSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
|
|
67
|
+
- `contextSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
|
|
68
68
|
to validate the input (`request.context`) of the Lambda handler. Has additional support for `typeof` keyword to allow validation of `"typeof":"function"`.
|
|
69
69
|
- `responseSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
|
|
70
70
|
to validate the output (`request.response`) of the Lambda handler. Supports alias `inputSchema`
|
|
@@ -73,7 +73,7 @@ npm install --save @middy/validator
|
|
|
73
73
|
- `i18nEnabled` (boolean) (default `true`): Option to disable i18n default package.
|
|
74
74
|
|
|
75
75
|
NOTES:
|
|
76
|
-
- At least one of `
|
|
76
|
+
- At least one of `eventSchema` or `responseSchema` is required.
|
|
77
77
|
- **Important** Compiling schemas on the fly will cause a 50-100ms performance hit during cold start for simple JSON Schemas. Precompiling is highly recommended.
|
|
78
78
|
- Default ajv plugins used: `ajv-i18n`, `ajv-formats`, `ajv-formats-draft2019`
|
|
79
79
|
- If you'd like to have the error details as part of the response, it will need to be handled separately. You can access them from `request.error.cause`, the original response can be found at `request.error.response`.
|
|
@@ -139,7 +139,7 @@ const schema = {
|
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
handler.use(validator({
|
|
142
|
+
handler.use(validator({responseSchema: schema}))
|
|
143
143
|
|
|
144
144
|
handler({}, {}, (err, response) => {
|
|
145
145
|
t.not(err, null)
|
package/index.cjs
CHANGED
|
@@ -1,3 +1,125 @@
|
|
|
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
|
+
var _fastUri = _interopRequireDefault(require("fast-uri"));
|
|
13
|
+
function _interopRequireDefault(obj) {
|
|
14
|
+
return obj && obj.__esModule ? obj : {
|
|
15
|
+
default: obj
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const Ajv = _2020Js.default.default;
|
|
19
|
+
let ajv;
|
|
20
|
+
const ajvDefaults = {
|
|
21
|
+
strict: true,
|
|
22
|
+
coerceTypes: 'array',
|
|
23
|
+
allErrors: true,
|
|
24
|
+
useDefaults: 'empty',
|
|
25
|
+
messages: false,
|
|
26
|
+
uriResolver: _fastUri.default,
|
|
27
|
+
keywords: [
|
|
28
|
+
(0, _typeofJs).default()
|
|
29
|
+
]
|
|
30
|
+
};
|
|
31
|
+
const defaults = {
|
|
32
|
+
eventSchema: undefined,
|
|
33
|
+
contextSchema: undefined,
|
|
34
|
+
responseSchema: undefined,
|
|
35
|
+
ajvOptions: {},
|
|
36
|
+
ajvInstance: undefined,
|
|
37
|
+
defaultLanguage: 'en',
|
|
38
|
+
i18nEnabled: true
|
|
39
|
+
};
|
|
40
|
+
const validatorMiddleware = (opts = {})=>{
|
|
41
|
+
let { inputSchema , outputSchema , eventSchema , contextSchema , responseSchema , ajvOptions , ajvInstance , defaultLanguage , i18nEnabled } = {
|
|
42
|
+
...defaults,
|
|
43
|
+
...opts
|
|
44
|
+
};
|
|
45
|
+
eventSchema = compile(eventSchema ?? inputSchema, ajvOptions, ajvInstance);
|
|
46
|
+
contextSchema = compile(contextSchema, ajvOptions, ajvInstance);
|
|
47
|
+
responseSchema = compile(responseSchema ?? outputSchema, ajvOptions, ajvInstance);
|
|
48
|
+
const validatorMiddlewareBefore = async (request)=>{
|
|
49
|
+
if (eventSchema) {
|
|
50
|
+
const validEvent = await eventSchema(request.event);
|
|
51
|
+
if (!validEvent) {
|
|
52
|
+
if (i18nEnabled) {
|
|
53
|
+
const language = chooseLanguage(request.event, defaultLanguage);
|
|
54
|
+
_ajvI18N.default[language](eventSchema.errors);
|
|
55
|
+
}
|
|
56
|
+
const error = (0, _util).createError(400, 'Event object failed validation');
|
|
57
|
+
error.cause = eventSchema.errors;
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (contextSchema) {
|
|
62
|
+
const validContext = await contextSchema(request.context);
|
|
63
|
+
if (!validContext) {
|
|
64
|
+
const error = (0, _util).createError(500, 'Context object failed validation');
|
|
65
|
+
error.cause = contextSchema.errors;
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const validatorMiddlewareAfter = async (request)=>{
|
|
71
|
+
const valid = await responseSchema(request.response);
|
|
72
|
+
if (!valid) {
|
|
73
|
+
const error = (0, _util).createError(500, 'Response object failed validation');
|
|
74
|
+
error.cause = responseSchema.errors;
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
before: (eventSchema ?? inputSchema) ?? contextSchema ? validatorMiddlewareBefore : undefined,
|
|
80
|
+
after: responseSchema ?? outputSchema ? validatorMiddlewareAfter : undefined
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const compile = (schema, ajvOptions, ajvInstance = null)=>{
|
|
84
|
+
if (typeof schema === 'function' || !schema) return schema;
|
|
85
|
+
const options = {
|
|
86
|
+
...ajvDefaults,
|
|
87
|
+
...ajvOptions
|
|
88
|
+
};
|
|
89
|
+
if (!ajv) {
|
|
90
|
+
ajv = ajvInstance ?? new Ajv(options);
|
|
91
|
+
(0, _ajvFormats).default(ajv);
|
|
92
|
+
(0, _ajvFormatsDraft2019).default(ajv);
|
|
93
|
+
} else if (!ajvInstance) {
|
|
94
|
+
ajv.opts = {
|
|
95
|
+
...ajv.opts,
|
|
96
|
+
...options
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return ajv.compile(schema);
|
|
100
|
+
};
|
|
101
|
+
const languageNormalizationMap = {
|
|
102
|
+
pt: 'pt-BR',
|
|
103
|
+
'pt-br': 'pt-BR',
|
|
104
|
+
pt_BR: 'pt-BR',
|
|
105
|
+
pt_br: 'pt-BR',
|
|
106
|
+
'zh-tw': 'zh-TW',
|
|
107
|
+
zh_TW: 'zh-TW',
|
|
108
|
+
zh_tw: 'zh-TW'
|
|
109
|
+
};
|
|
110
|
+
const normalizePreferredLanguage = (lang)=>languageNormalizationMap[lang] ?? lang;
|
|
111
|
+
const availableLanguages = Object.keys(_ajvI18N.default);
|
|
112
|
+
const chooseLanguage = ({ preferredLanguage }, defaultLanguage)=>{
|
|
113
|
+
if (preferredLanguage) {
|
|
114
|
+
const lang = normalizePreferredLanguage(preferredLanguage);
|
|
115
|
+
if (availableLanguages.includes(lang)) {
|
|
116
|
+
return lang;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return defaultLanguage;
|
|
120
|
+
};
|
|
121
|
+
var _default = validatorMiddleware;
|
|
122
|
+
module.exports = _default;
|
|
123
|
+
|
|
2
124
|
|
|
3
125
|
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,3 +1,114 @@
|
|
|
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
|
+
import uriResolver from 'fast-uri';
|
|
8
|
+
const Ajv = _ajv.default;
|
|
9
|
+
let ajv;
|
|
10
|
+
const ajvDefaults = {
|
|
11
|
+
strict: true,
|
|
12
|
+
coerceTypes: 'array',
|
|
13
|
+
allErrors: true,
|
|
14
|
+
useDefaults: 'empty',
|
|
15
|
+
messages: false,
|
|
16
|
+
uriResolver,
|
|
17
|
+
keywords: [
|
|
18
|
+
typeofKeyword()
|
|
19
|
+
]
|
|
20
|
+
};
|
|
21
|
+
const defaults = {
|
|
22
|
+
eventSchema: undefined,
|
|
23
|
+
contextSchema: undefined,
|
|
24
|
+
responseSchema: undefined,
|
|
25
|
+
ajvOptions: {},
|
|
26
|
+
ajvInstance: undefined,
|
|
27
|
+
defaultLanguage: 'en',
|
|
28
|
+
i18nEnabled: true
|
|
29
|
+
};
|
|
30
|
+
const validatorMiddleware = (opts = {})=>{
|
|
31
|
+
let { inputSchema , outputSchema , eventSchema , contextSchema , responseSchema , ajvOptions , ajvInstance , defaultLanguage , i18nEnabled } = {
|
|
32
|
+
...defaults,
|
|
33
|
+
...opts
|
|
34
|
+
};
|
|
35
|
+
eventSchema = compile(eventSchema ?? inputSchema, ajvOptions, ajvInstance);
|
|
36
|
+
contextSchema = compile(contextSchema, ajvOptions, ajvInstance);
|
|
37
|
+
responseSchema = compile(responseSchema ?? outputSchema, ajvOptions, ajvInstance);
|
|
38
|
+
const validatorMiddlewareBefore = async (request)=>{
|
|
39
|
+
if (eventSchema) {
|
|
40
|
+
const validEvent = await eventSchema(request.event);
|
|
41
|
+
if (!validEvent) {
|
|
42
|
+
if (i18nEnabled) {
|
|
43
|
+
const language = chooseLanguage(request.event, defaultLanguage);
|
|
44
|
+
localize[language](eventSchema.errors);
|
|
45
|
+
}
|
|
46
|
+
const error = createError(400, 'Event object failed validation');
|
|
47
|
+
error.cause = eventSchema.errors;
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (contextSchema) {
|
|
52
|
+
const validContext = await contextSchema(request.context);
|
|
53
|
+
if (!validContext) {
|
|
54
|
+
const error = createError(500, 'Context object failed validation');
|
|
55
|
+
error.cause = contextSchema.errors;
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const validatorMiddlewareAfter = async (request)=>{
|
|
61
|
+
const valid = await responseSchema(request.response);
|
|
62
|
+
if (!valid) {
|
|
63
|
+
const error = createError(500, 'Response object failed validation');
|
|
64
|
+
error.cause = responseSchema.errors;
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
return {
|
|
69
|
+
before: (eventSchema ?? inputSchema) ?? contextSchema ? validatorMiddlewareBefore : undefined,
|
|
70
|
+
after: responseSchema ?? outputSchema ? validatorMiddlewareAfter : undefined
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const compile = (schema, ajvOptions, ajvInstance = null)=>{
|
|
74
|
+
if (typeof schema === 'function' || !schema) return schema;
|
|
75
|
+
const options = {
|
|
76
|
+
...ajvDefaults,
|
|
77
|
+
...ajvOptions
|
|
78
|
+
};
|
|
79
|
+
if (!ajv) {
|
|
80
|
+
ajv = ajvInstance ?? new Ajv(options);
|
|
81
|
+
formats(ajv);
|
|
82
|
+
formatsDraft2019(ajv);
|
|
83
|
+
} else if (!ajvInstance) {
|
|
84
|
+
ajv.opts = {
|
|
85
|
+
...ajv.opts,
|
|
86
|
+
...options
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return ajv.compile(schema);
|
|
90
|
+
};
|
|
91
|
+
const languageNormalizationMap = {
|
|
92
|
+
pt: 'pt-BR',
|
|
93
|
+
'pt-br': 'pt-BR',
|
|
94
|
+
pt_BR: 'pt-BR',
|
|
95
|
+
pt_br: 'pt-BR',
|
|
96
|
+
'zh-tw': 'zh-TW',
|
|
97
|
+
zh_TW: 'zh-TW',
|
|
98
|
+
zh_tw: 'zh-TW'
|
|
99
|
+
};
|
|
100
|
+
const normalizePreferredLanguage = (lang)=>languageNormalizationMap[lang] ?? lang;
|
|
101
|
+
const availableLanguages = Object.keys(localize);
|
|
102
|
+
const chooseLanguage = ({ preferredLanguage }, defaultLanguage)=>{
|
|
103
|
+
if (preferredLanguage) {
|
|
104
|
+
const lang = normalizePreferredLanguage(preferredLanguage);
|
|
105
|
+
if (availableLanguages.includes(lang)) {
|
|
106
|
+
return lang;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return defaultLanguage;
|
|
110
|
+
};
|
|
111
|
+
export default validatorMiddleware;
|
|
112
|
+
|
|
2
113
|
|
|
3
114
|
//# 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.1.0-rc.1",
|
|
4
4
|
"description": "Validator middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,11 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
+
"main": "./index.cjs",
|
|
13
14
|
"exports": {
|
|
14
15
|
".": {
|
|
15
|
-
"import":
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"default": "./index.cjs"
|
|
23
|
+
}
|
|
18
24
|
}
|
|
19
25
|
},
|
|
20
26
|
"types": "index.d.ts",
|
|
@@ -54,17 +60,18 @@
|
|
|
54
60
|
},
|
|
55
61
|
"homepage": "https://middy.js.org",
|
|
56
62
|
"dependencies": {
|
|
57
|
-
"@middy/util": "3.0.
|
|
63
|
+
"@middy/util": "3.1.0-rc.1",
|
|
58
64
|
"ajv": "8.11.0",
|
|
59
65
|
"ajv-formats": "2.1.1",
|
|
60
66
|
"ajv-formats-draft2019": "1.6.1",
|
|
61
67
|
"ajv-i18n": "4.2.0",
|
|
62
|
-
"ajv-keywords": "5.1.0"
|
|
68
|
+
"ajv-keywords": "5.1.0",
|
|
69
|
+
"fast-uri": "2.1.0"
|
|
63
70
|
},
|
|
64
71
|
"devDependencies": {
|
|
65
|
-
"@middy/core": "3.0.
|
|
72
|
+
"@middy/core": "3.1.0-rc.1",
|
|
66
73
|
"@types/http-errors": "^1.8.1",
|
|
67
74
|
"ajv-bsontype": "^1.0.7"
|
|
68
75
|
},
|
|
69
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "044c397e7a2b1de516b4b5c21ece2baffdbfa771"
|
|
70
77
|
}
|