@middy/validator 3.0.0-alpha.0 → 3.0.0-alpha.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/index.js +120 -0
- package/package.json +5 -4
package/index.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { createError } from '@middy/util';
|
|
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
|
+
};
|
|
23
|
+
|
|
24
|
+
const validatorMiddleware = (opts = {}) => {
|
|
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
|
+
const error = createError(400, 'Event object failed validation');
|
|
43
|
+
request.event.headers = { ...request.event.headers
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
if (i18nEnabled) {
|
|
47
|
+
const language = chooseLanguage(request.event, defaultLanguage);
|
|
48
|
+
localize[language](inputSchema.errors);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
error.details = inputSchema.errors;
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const validatorMiddlewareAfter = async request => {
|
|
57
|
+
const valid = outputSchema(request.response);
|
|
58
|
+
|
|
59
|
+
if (!valid) {
|
|
60
|
+
const error = createError(500, 'Response object failed validation');
|
|
61
|
+
error.details = outputSchema.errors;
|
|
62
|
+
error.response = request.response;
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
before: inputSchema ? validatorMiddlewareBefore : undefined,
|
|
69
|
+
after: outputSchema ? validatorMiddlewareAfter : undefined
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const compile = (schema, ajvOptions, ajvInstance = null) => {
|
|
74
|
+
if (typeof schema === 'function' || !schema) return schema;
|
|
75
|
+
const options = { ...ajvDefaults,
|
|
76
|
+
...ajvOptions
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
if (!ajv) {
|
|
80
|
+
ajv = ajvInstance ?? new Ajv(options);
|
|
81
|
+
formats(ajv);
|
|
82
|
+
formatsDraft2019(ajv);
|
|
83
|
+
} else if (!ajvInstance) {
|
|
84
|
+
ajv.opts = { ...ajv.opts,
|
|
85
|
+
...options
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return ajv.compile(schema);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const languageNormalizationMap = {
|
|
93
|
+
pt: 'pt-BR',
|
|
94
|
+
'pt-br': 'pt-BR',
|
|
95
|
+
pt_BR: 'pt-BR',
|
|
96
|
+
pt_br: 'pt-BR',
|
|
97
|
+
'zh-tw': 'zh-TW',
|
|
98
|
+
zh_TW: 'zh-TW',
|
|
99
|
+
zh_tw: 'zh-TW'
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const normalizePreferredLanguage = lang => languageNormalizationMap[lang] ?? lang;
|
|
103
|
+
|
|
104
|
+
const availableLanguages = Object.keys(localize);
|
|
105
|
+
|
|
106
|
+
const chooseLanguage = ({
|
|
107
|
+
preferredLanguage
|
|
108
|
+
}, defaultLanguage) => {
|
|
109
|
+
if (preferredLanguage) {
|
|
110
|
+
const lang = normalizePreferredLanguage(preferredLanguage);
|
|
111
|
+
|
|
112
|
+
if (availableLanguages.includes(lang)) {
|
|
113
|
+
return lang;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return defaultLanguage;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export default validatorMiddleware;
|
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.1",
|
|
4
4
|
"description": "Validator middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
|
+
"index.js",
|
|
16
17
|
"index.d.ts"
|
|
17
18
|
],
|
|
18
19
|
"scripts": {
|
|
@@ -45,16 +46,16 @@
|
|
|
45
46
|
},
|
|
46
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
47
48
|
"dependencies": {
|
|
48
|
-
"@middy/util": "^3.0.0-alpha.
|
|
49
|
+
"@middy/util": "^3.0.0-alpha.1",
|
|
49
50
|
"ajv": "8.8.2",
|
|
50
51
|
"ajv-formats": "2.1.1",
|
|
51
52
|
"ajv-formats-draft2019": "1.6.1",
|
|
52
53
|
"ajv-i18n": "4.2.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
|
-
"@middy/core": "^3.0.0-alpha.
|
|
56
|
+
"@middy/core": "^3.0.0-alpha.1",
|
|
56
57
|
"@types/http-errors": "^1.8.1",
|
|
57
58
|
"ajv-bsontype": "^1.0.7"
|
|
58
59
|
},
|
|
59
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "a14125c6b2e21b181824f9985a919a47f1e4711f"
|
|
60
61
|
}
|