@middy/validator 2.5.4 → 3.0.0-alpha.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 +2 -2
- package/index.d.ts +1 -0
- package/package.json +10 -11
- package/index.js +0 -139
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-
|
|
3
|
+
Copyright (c) 2017-2022 Luciano Mammino, will Farrell 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
|
@@ -57,7 +57,7 @@ npm install --save @middy/validator
|
|
|
57
57
|
to validate the output (`request.response`) of the Lambda handler.
|
|
58
58
|
- `ajvOptions` (object) (optional): Options to pass to [ajv](https://ajv.js.org/docs/api.html#options)
|
|
59
59
|
class constructor. Defaults are `{ strict: true, coerceTypes: 'array', allErrors: true, useDefaults: 'empty', messages: false, defaultLanguage: 'en' }`.
|
|
60
|
-
|
|
60
|
+
- `i18nEnabled` (boolean) (optional): Option to disable i18n default package. Defaults to `true`
|
|
61
61
|
NOTES:
|
|
62
62
|
- At least one of `inputSchema` or `outputSchema` is required.
|
|
63
63
|
- **Important** Compiling schemas on the fly will cause a 50-100ms performance hit during cold start for simple JSON Schemas. Precompiling is highly recommended.
|
|
@@ -148,7 +148,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
148
148
|
|
|
149
149
|
## License
|
|
150
150
|
|
|
151
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-
|
|
151
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
152
152
|
|
|
153
153
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
154
154
|
<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.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/validator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
4
4
|
"description": "Validator middleware for the middy framework",
|
|
5
|
-
"type": "
|
|
5
|
+
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=14"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"
|
|
13
|
+
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
16
|
"index.d.ts"
|
|
@@ -45,17 +45,16 @@
|
|
|
45
45
|
},
|
|
46
46
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@middy/util": "^
|
|
49
|
-
"ajv": "8.
|
|
48
|
+
"@middy/util": "^3.0.0-alpha.0",
|
|
49
|
+
"ajv": "8.8.2",
|
|
50
50
|
"ajv-formats": "2.1.1",
|
|
51
51
|
"ajv-formats-draft2019": "1.6.1",
|
|
52
|
-
"ajv-i18n": "4.
|
|
52
|
+
"ajv-i18n": "4.2.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@middy/core": "^
|
|
55
|
+
"@middy/core": "^3.0.0-alpha.0",
|
|
56
56
|
"@types/http-errors": "^1.8.1",
|
|
57
|
-
"ajv-bsontype": "^1.0.7"
|
|
58
|
-
"http-errors": "^1.8.0"
|
|
57
|
+
"ajv-bsontype": "^1.0.7"
|
|
59
58
|
},
|
|
60
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db"
|
|
61
60
|
}
|
package/index.js
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
createError
|
|
5
|
-
} = require('@middy/util');
|
|
6
|
-
|
|
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;
|
|
18
|
-
const ajvDefaults = {
|
|
19
|
-
strict: true,
|
|
20
|
-
coerceTypes: 'array',
|
|
21
|
-
// important for query string params
|
|
22
|
-
allErrors: true,
|
|
23
|
-
useDefaults: 'empty',
|
|
24
|
-
messages: false // allow i18n
|
|
25
|
-
|
|
26
|
-
};
|
|
27
|
-
const defaults = {
|
|
28
|
-
inputSchema: undefined,
|
|
29
|
-
outputSchema: undefined,
|
|
30
|
-
ajvOptions: {},
|
|
31
|
-
ajvInstance: undefined,
|
|
32
|
-
defaultLanguage: 'en'
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const validatorMiddleware = (opts = {}) => {
|
|
36
|
-
let {
|
|
37
|
-
inputSchema,
|
|
38
|
-
outputSchema,
|
|
39
|
-
ajvOptions,
|
|
40
|
-
ajvInstance,
|
|
41
|
-
defaultLanguage
|
|
42
|
-
} = { ...defaults,
|
|
43
|
-
...opts
|
|
44
|
-
};
|
|
45
|
-
inputSchema = compile(inputSchema, ajvOptions, ajvInstance);
|
|
46
|
-
outputSchema = compile(outputSchema, ajvOptions, ajvInstance);
|
|
47
|
-
|
|
48
|
-
const validatorMiddlewareBefore = async request => {
|
|
49
|
-
const valid = inputSchema(request.event);
|
|
50
|
-
|
|
51
|
-
if (!valid) {
|
|
52
|
-
// Bad Request
|
|
53
|
-
const error = createError(400, 'Event object failed validation');
|
|
54
|
-
request.event.headers = { ...request.event.headers
|
|
55
|
-
};
|
|
56
|
-
const language = chooseLanguage(request.event, defaultLanguage);
|
|
57
|
-
localize[language](inputSchema.errors);
|
|
58
|
-
error.details = inputSchema.errors;
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const validatorMiddlewareAfter = async request => {
|
|
64
|
-
const valid = outputSchema(request.response);
|
|
65
|
-
|
|
66
|
-
if (!valid) {
|
|
67
|
-
// Internal Server Error
|
|
68
|
-
const error = createError(500, 'Response object failed validation');
|
|
69
|
-
error.details = outputSchema.errors;
|
|
70
|
-
error.response = request.response;
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
before: inputSchema ? validatorMiddlewareBefore : undefined,
|
|
77
|
-
after: outputSchema ? validatorMiddlewareAfter : undefined
|
|
78
|
-
};
|
|
79
|
-
}; // This is pulled out due to it's performance cost (50-100ms on cold start)
|
|
80
|
-
// Precompile your schema during a build step is recommended.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const compile = (schema, ajvOptions, ajvInstance = null) => {
|
|
84
|
-
// Check if already compiled
|
|
85
|
-
if (typeof schema === 'function' || !schema) return schema;
|
|
86
|
-
const options = { ...ajvDefaults,
|
|
87
|
-
...ajvOptions
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
if (!ajv) {
|
|
91
|
-
ajv = ajvInstance !== null && ajvInstance !== void 0 ? ajvInstance : new Ajv(options);
|
|
92
|
-
formats(ajv);
|
|
93
|
-
formatsDraft2019(ajv);
|
|
94
|
-
} else if (!ajvInstance) {
|
|
95
|
-
// Update options when initializing the middleware multiple times
|
|
96
|
-
ajv.opts = { ...ajv.opts,
|
|
97
|
-
...options
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return ajv.compile(schema);
|
|
102
|
-
};
|
|
103
|
-
/* in ajv-i18n Portuguese is represented as pt-BR */
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const languageNormalizationMap = {
|
|
107
|
-
pt: 'pt-BR',
|
|
108
|
-
'pt-br': 'pt-BR',
|
|
109
|
-
pt_BR: 'pt-BR',
|
|
110
|
-
pt_br: 'pt-BR',
|
|
111
|
-
zh: 'zh-TW',
|
|
112
|
-
'zh-tw': 'zh-TW',
|
|
113
|
-
zh_TW: 'zh-TW',
|
|
114
|
-
zh_tw: 'zh-TW'
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const normalizePreferredLanguage = lang => {
|
|
118
|
-
var _languageNormalizatio;
|
|
119
|
-
|
|
120
|
-
return (_languageNormalizatio = languageNormalizationMap[lang]) !== null && _languageNormalizatio !== void 0 ? _languageNormalizatio : lang;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
const availableLanguages = Object.keys(localize);
|
|
124
|
-
|
|
125
|
-
const chooseLanguage = ({
|
|
126
|
-
preferredLanguage
|
|
127
|
-
}, defaultLanguage) => {
|
|
128
|
-
if (preferredLanguage) {
|
|
129
|
-
const lang = normalizePreferredLanguage(preferredLanguage);
|
|
130
|
-
|
|
131
|
-
if (availableLanguages.includes(lang)) {
|
|
132
|
-
return lang;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return defaultLanguage;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
module.exports = validatorMiddleware;
|