@middy/validator 3.0.0-alpha.0 → 3.0.0-alpha.4

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/index.js +116 -0
  3. package/package.json +7 -5
package/README.md CHANGED
@@ -62,7 +62,7 @@ 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.
64
64
  - Default ajv plugins used: `ajv-i18n`, `ajv-formats`, `ajv-formats-draft2019`
65
- - 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.details`, the original response can be found at `request.error.response`.
65
+ - 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`.
66
66
 
67
67
  ## Sample usage
68
68
 
package/index.js ADDED
@@ -0,0 +1,116 @@
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
+ if (i18nEnabled) {
43
+ const language = chooseLanguage(request.event, defaultLanguage);
44
+ localize[language](inputSchema.errors);
45
+ }
46
+
47
+ throw createError(400, 'Event object failed validation', {
48
+ cause: inputSchema.errors
49
+ });
50
+ }
51
+ };
52
+
53
+ const validatorMiddlewareAfter = async request => {
54
+ const valid = outputSchema(request.response);
55
+
56
+ if (!valid) {
57
+ throw createError(500, 'Response object failed validation', {
58
+ cause: outputSchema.errors
59
+ });
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/validator",
3
- "version": "3.0.0-alpha.0",
3
+ "version": "3.0.0-alpha.4",
4
4
  "description": "Validator middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -13,11 +13,13 @@
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": {
19
20
  "test": "npm run test:unit",
20
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
21
23
  },
22
24
  "license": "MIT",
23
25
  "keywords": [
@@ -45,16 +47,16 @@
45
47
  },
46
48
  "homepage": "https://github.com/middyjs/middy#readme",
47
49
  "dependencies": {
48
- "@middy/util": "^3.0.0-alpha.0",
50
+ "@middy/util": "^3.0.0-alpha.4",
49
51
  "ajv": "8.8.2",
50
52
  "ajv-formats": "2.1.1",
51
53
  "ajv-formats-draft2019": "1.6.1",
52
54
  "ajv-i18n": "4.2.0"
53
55
  },
54
56
  "devDependencies": {
55
- "@middy/core": "^3.0.0-alpha.0",
57
+ "@middy/core": "^3.0.0-alpha.4",
56
58
  "@types/http-errors": "^1.8.1",
57
59
  "ajv-bsontype": "^1.0.7"
58
60
  },
59
- "gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db"
61
+ "gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53"
60
62
  }