@middy/validator 3.0.2 → 3.1.0-rc.0
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/LICENSE +1 -1
- package/README.md +7 -6
- package/index.cjs +123 -1
- package/index.js +112 -1
- package/package.json +15 -8
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
3
|
+
Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
<a href="https://packagephobia.com/result?p=@middy/validator">
|
|
10
10
|
<img src="https://packagephobia.com/badge?p=@middy/validator" alt="npm install size" style="max-width:100%;">
|
|
11
11
|
</a>
|
|
12
|
-
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
-
<img src="https://github.com/middyjs/middy/workflows/
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
|
|
14
14
|
</a>
|
|
15
15
|
<br/>
|
|
16
16
|
<a href="https://standardjs.com/">
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
34
34
|
</a>
|
|
35
35
|
</p>
|
|
36
|
+
<p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/validator">https://middy.js.org/docs/middlewares/validator</a></p>
|
|
36
37
|
</div>
|
|
37
38
|
|
|
38
39
|
This middleware automatically validates incoming events and outgoing responses against custom
|
|
@@ -63,7 +64,7 @@ npm install --save @middy/validator
|
|
|
63
64
|
|
|
64
65
|
- `eventSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
|
|
65
66
|
to validate the input (`request.event`) of the Lambda handler. Supports alias `inputSchema`
|
|
66
|
-
- `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
|
|
67
68
|
to validate the input (`request.context`) of the Lambda handler. Has additional support for `typeof` keyword to allow validation of `"typeof":"function"`.
|
|
68
69
|
- `responseSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
|
|
69
70
|
to validate the output (`request.response`) of the Lambda handler. Supports alias `inputSchema`
|
|
@@ -72,7 +73,7 @@ npm install --save @middy/validator
|
|
|
72
73
|
- `i18nEnabled` (boolean) (default `true`): Option to disable i18n default package.
|
|
73
74
|
|
|
74
75
|
NOTES:
|
|
75
|
-
- At least one of `
|
|
76
|
+
- At least one of `eventSchema` or `responseSchema` is required.
|
|
76
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.
|
|
77
78
|
- Default ajv plugins used: `ajv-i18n`, `ajv-formats`, `ajv-formats-draft2019`
|
|
78
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`.
|
|
@@ -138,7 +139,7 @@ const schema = {
|
|
|
138
139
|
}
|
|
139
140
|
}
|
|
140
141
|
|
|
141
|
-
handler.use(validator({
|
|
142
|
+
handler.use(validator({responseSchema: schema}))
|
|
142
143
|
|
|
143
144
|
handler({}, {}, (err, response) => {
|
|
144
145
|
t.not(err, null)
|
|
@@ -161,7 +162,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
161
162
|
|
|
162
163
|
## License
|
|
163
164
|
|
|
164
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
165
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
165
166
|
|
|
166
167
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
167
168
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
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.0",
|
|
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": "
|
|
63
|
+
"@middy/util": "3.1.0-rc.0",
|
|
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": "
|
|
72
|
+
"@middy/core": "3.1.0-rc.0",
|
|
66
73
|
"@types/http-errors": "^1.8.1",
|
|
67
74
|
"ajv-bsontype": "^1.0.7"
|
|
68
75
|
},
|
|
69
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "03a8794d3cdb4319eca49ba4c55420bea5d66430"
|
|
70
77
|
}
|