@middy/validator 3.0.0-alpha.6 → 3.0.0-alpha.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/README.md +30 -19
- package/index.js +2 -115
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,32 +1,42 @@
|
|
|
1
|
-
# Middy validator middleware
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy validator middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>Validator middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/validator?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Fvalidator.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/validator">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/validator" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
21
24
|
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
27
|
+
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
|
27
37
|
|
|
28
38
|
This middleware automatically validates incoming events and outgoing responses against custom
|
|
29
|
-
schemas defined with the [JSON schema syntax](
|
|
39
|
+
schemas defined with the [JSON schema syntax](https://json-schema.org/).
|
|
30
40
|
|
|
31
41
|
If an incoming event fails validation a `BadRequest` error is raised.
|
|
32
42
|
If an outgoing response fails validation a `InternalServerError` error is
|
|
@@ -36,7 +46,7 @@ This middleware can be used in combination with
|
|
|
36
46
|
[`httpErrorHandler`](#httperrorhandler) to automatically return the right
|
|
37
47
|
response to the user.
|
|
38
48
|
|
|
39
|
-
It can also be used in combination with [`httpcontentnegotiation`](#httpContentNegotiation) to load localised translations for the error messages (based on the currently requested language). This feature uses internally [`ajv-i18n`](
|
|
49
|
+
It can also be used in combination with [`httpcontentnegotiation`](#httpContentNegotiation) to load localised translations for the error messages (based on the currently requested language). This feature uses internally [`ajv-i18n`](https://www.npmjs.com/package/ajv-i18n) module, so reference to this module for options and more advanced use cases. By default the language used will be English (`en`), but you can redefine the default language by passing it in the `ajvOptions` options with the key `defaultLanguage` and specifying as value one of the [supported locales](https://www.npmjs.com/package/ajv-i18n#supported-locales).
|
|
40
50
|
|
|
41
51
|
Also, this middleware accepts an object with plugins to be applied to customize the internal `ajv` instance. Out-of-the-box `ajv-i18n` and `ajv-formats` are being used.
|
|
42
52
|
|
|
@@ -51,13 +61,14 @@ npm install --save @middy/validator
|
|
|
51
61
|
|
|
52
62
|
## Options
|
|
53
63
|
|
|
54
|
-
- `inputSchema` (object) (
|
|
64
|
+
- `inputSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
|
|
55
65
|
to validate the input (`request.event`) of the Lambda handler.
|
|
56
|
-
- `outputSchema` (object) (
|
|
66
|
+
- `outputSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
|
|
57
67
|
to validate the output (`request.response`) of the Lambda handler.
|
|
58
|
-
- `ajvOptions` (object) (
|
|
68
|
+
- `ajvOptions` (object) (default `undefined`): Options to pass to [ajv](https://ajv.js.org/docs/api.html#options)
|
|
59
69
|
class constructor. Defaults are `{ strict: true, coerceTypes: 'array', allErrors: true, useDefaults: 'empty', messages: false, defaultLanguage: 'en' }`.
|
|
60
|
-
- `i18nEnabled` (boolean) (
|
|
70
|
+
- `i18nEnabled` (boolean) (default `true`): Option to disable i18n default package.
|
|
71
|
+
|
|
61
72
|
NOTES:
|
|
62
73
|
- At least one of `inputSchema` or `outputSchema` is required.
|
|
63
74
|
- **Important** Compiling schemas on the fly will cause a 50-100ms performance hit during cold start for simple JSON Schemas. Precompiling is highly recommended.
|
package/index.js
CHANGED
|
@@ -1,116 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import _ajv from 'ajv/dist/2019.js';
|
|
3
|
-
import localize from 'ajv-i18n';
|
|
4
|
-
import formats from 'ajv-formats';
|
|
5
|
-
import formatsDraft2019 from 'ajv-formats-draft2019';
|
|
6
|
-
const Ajv = _ajv.default;
|
|
7
|
-
let ajv;
|
|
8
|
-
const ajvDefaults = {
|
|
9
|
-
strict: true,
|
|
10
|
-
coerceTypes: 'array',
|
|
11
|
-
allErrors: true,
|
|
12
|
-
useDefaults: 'empty',
|
|
13
|
-
messages: false
|
|
14
|
-
};
|
|
15
|
-
const defaults = {
|
|
16
|
-
inputSchema: undefined,
|
|
17
|
-
outputSchema: undefined,
|
|
18
|
-
ajvOptions: {},
|
|
19
|
-
ajvInstance: undefined,
|
|
20
|
-
defaultLanguage: 'en',
|
|
21
|
-
i18nEnabled: true
|
|
22
|
-
};
|
|
1
|
+
import{createError}from'@middy/util';import _ajv from'ajv/dist/2019.js';import localize from'ajv-i18n';import formats from'ajv-formats';import formatsDraft2019 from'ajv-formats-draft2019';const Ajv=_ajv.default;let ajv;const ajvDefaults={strict:true,coerceTypes:'array',allErrors:true,useDefaults:'empty',messages:false};const defaults={inputSchema:undefined,outputSchema:undefined,ajvOptions:{},ajvInstance:undefined,defaultLanguage:'en',i18nEnabled:true};const validatorMiddleware=(opts={})=>{let{inputSchema,outputSchema,ajvOptions,ajvInstance,defaultLanguage,i18nEnabled}={...defaults,...opts};inputSchema=compile(inputSchema,ajvOptions,ajvInstance);outputSchema=compile(outputSchema,ajvOptions,ajvInstance);const validatorMiddlewareBefore=async request=>{const valid=inputSchema(request.event);if(!valid){if(i18nEnabled){const language=chooseLanguage(request.event,defaultLanguage);localize[language](inputSchema.errors)}const error=createError(400,'Event object failed validation');error.cause=inputSchema.errors;throw error}};const validatorMiddlewareAfter=async request=>{const valid=outputSchema(request.response);if(!valid){const error=createError(500,'Response object failed validation');error.cause=outputSchema.errors;throw error}};return{before:inputSchema?validatorMiddlewareBefore:undefined,after:outputSchema?validatorMiddlewareAfter:undefined}};const compile=(schema,ajvOptions,ajvInstance=null)=>{if(typeof schema==='function'||!schema)return schema;const options={...ajvDefaults,...ajvOptions};if(!ajv){ajv=ajvInstance??new Ajv(options);formats(ajv);formatsDraft2019(ajv)}else if(!ajvInstance){ajv.opts={...ajv.opts,...options}}return ajv.compile(schema)};const languageNormalizationMap={pt:'pt-BR','pt-br':'pt-BR',pt_BR:'pt-BR',pt_br:'pt-BR','zh-tw':'zh-TW',zh_TW:'zh-TW',zh_tw:'zh-TW'};const normalizePreferredLanguage=lang=>languageNormalizationMap[lang]??lang;const availableLanguages=Object.keys(localize);const chooseLanguage=({preferredLanguage},defaultLanguage)=>{if(preferredLanguage){const lang=normalizePreferredLanguage(preferredLanguage);if(availableLanguages.includes(lang)){return lang}}return defaultLanguage};export default validatorMiddleware
|
|
23
2
|
|
|
24
|
-
|
|
25
|
-
let {
|
|
26
|
-
inputSchema,
|
|
27
|
-
outputSchema,
|
|
28
|
-
ajvOptions,
|
|
29
|
-
ajvInstance,
|
|
30
|
-
defaultLanguage,
|
|
31
|
-
i18nEnabled
|
|
32
|
-
} = { ...defaults,
|
|
33
|
-
...opts
|
|
34
|
-
};
|
|
35
|
-
inputSchema = compile(inputSchema, ajvOptions, ajvInstance);
|
|
36
|
-
outputSchema = compile(outputSchema, ajvOptions, ajvInstance);
|
|
37
|
-
|
|
38
|
-
const validatorMiddlewareBefore = async request => {
|
|
39
|
-
const valid = inputSchema(request.event);
|
|
40
|
-
|
|
41
|
-
if (!valid) {
|
|
42
|
-
if (i18nEnabled) {
|
|
43
|
-
const language = chooseLanguage(request.event, defaultLanguage);
|
|
44
|
-
localize[language](inputSchema.errors);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const error = createError(400, 'Event object failed validation');
|
|
48
|
-
error.cause = inputSchema.errors;
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const validatorMiddlewareAfter = async request => {
|
|
54
|
-
const valid = outputSchema(request.response);
|
|
55
|
-
|
|
56
|
-
if (!valid) {
|
|
57
|
-
const error = createError(500, 'Response object failed validation');
|
|
58
|
-
error.cause = outputSchema.errors;
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
return {
|
|
64
|
-
before: inputSchema ? validatorMiddlewareBefore : undefined,
|
|
65
|
-
after: outputSchema ? validatorMiddlewareAfter : undefined
|
|
66
|
-
};
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const compile = (schema, ajvOptions, ajvInstance = null) => {
|
|
70
|
-
if (typeof schema === 'function' || !schema) return schema;
|
|
71
|
-
const options = { ...ajvDefaults,
|
|
72
|
-
...ajvOptions
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
if (!ajv) {
|
|
76
|
-
ajv = ajvInstance ?? new Ajv(options);
|
|
77
|
-
formats(ajv);
|
|
78
|
-
formatsDraft2019(ajv);
|
|
79
|
-
} else if (!ajvInstance) {
|
|
80
|
-
ajv.opts = { ...ajv.opts,
|
|
81
|
-
...options
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return ajv.compile(schema);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const languageNormalizationMap = {
|
|
89
|
-
pt: 'pt-BR',
|
|
90
|
-
'pt-br': 'pt-BR',
|
|
91
|
-
pt_BR: 'pt-BR',
|
|
92
|
-
pt_br: 'pt-BR',
|
|
93
|
-
'zh-tw': 'zh-TW',
|
|
94
|
-
zh_TW: 'zh-TW',
|
|
95
|
-
zh_tw: 'zh-TW'
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const normalizePreferredLanguage = lang => languageNormalizationMap[lang] ?? lang;
|
|
99
|
-
|
|
100
|
-
const availableLanguages = Object.keys(localize);
|
|
101
|
-
|
|
102
|
-
const chooseLanguage = ({
|
|
103
|
-
preferredLanguage
|
|
104
|
-
}, defaultLanguage) => {
|
|
105
|
-
if (preferredLanguage) {
|
|
106
|
-
const lang = normalizePreferredLanguage(preferredLanguage);
|
|
107
|
-
|
|
108
|
-
if (availableLanguages.includes(lang)) {
|
|
109
|
-
return lang;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return defaultLanguage;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
export default validatorMiddleware;
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/validator",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.7",
|
|
4
4
|
"description": "Validator middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -47,16 +47,16 @@
|
|
|
47
47
|
},
|
|
48
48
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@middy/util": "^3.0.0-alpha.
|
|
50
|
+
"@middy/util": "^3.0.0-alpha.7",
|
|
51
51
|
"ajv": "8.9.0",
|
|
52
52
|
"ajv-formats": "2.1.1",
|
|
53
53
|
"ajv-formats-draft2019": "1.6.1",
|
|
54
54
|
"ajv-i18n": "4.2.0"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@middy/core": "^3.0.0-alpha.
|
|
57
|
+
"@middy/core": "^3.0.0-alpha.7",
|
|
58
58
|
"@types/http-errors": "^1.8.1",
|
|
59
59
|
"ajv-bsontype": "^1.0.7"
|
|
60
60
|
},
|
|
61
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "5cef39ebe49c201f97d71bb0680004de4b82cb91"
|
|
62
62
|
}
|