@middy/validator 3.0.0-alpha.2 → 3.0.0-alpha.3

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 +54 -64
  3. package/package.json +6 -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 CHANGED
@@ -1,17 +1,20 @@
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;
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
+
7
+ const Ajv = _ajv.default // esm workaround for linting
8
+
9
+ let ajv
8
10
  const ajvDefaults = {
9
11
  strict: true,
10
- coerceTypes: 'array',
12
+ coerceTypes: 'array', // important for query string params
11
13
  allErrors: true,
12
14
  useDefaults: 'empty',
13
- messages: false
14
- };
15
+ messages: false // allow i18n
16
+ }
17
+
15
18
  const defaults = {
16
19
  inputSchema: undefined,
17
20
  outputSchema: undefined,
@@ -19,7 +22,7 @@ const defaults = {
19
22
  ajvInstance: undefined,
20
23
  defaultLanguage: 'en',
21
24
  i18nEnabled: true
22
- };
25
+ }
23
26
 
24
27
  const validatorMiddleware = (opts = {}) => {
25
28
  let {
@@ -29,66 +32,56 @@ const validatorMiddleware = (opts = {}) => {
29
32
  ajvInstance,
30
33
  defaultLanguage,
31
34
  i18nEnabled
32
- } = { ...defaults,
33
- ...opts
34
- };
35
- inputSchema = compile(inputSchema, ajvOptions, ajvInstance);
36
- outputSchema = compile(outputSchema, ajvOptions, ajvInstance);
35
+ } = { ...defaults, ...opts }
36
+ inputSchema = compile(inputSchema, ajvOptions, ajvInstance)
37
+ outputSchema = compile(outputSchema, ajvOptions, ajvInstance)
37
38
 
38
- const validatorMiddlewareBefore = async request => {
39
- const valid = inputSchema(request.event);
39
+ const validatorMiddlewareBefore = async (request) => {
40
+ const valid = inputSchema(request.event)
40
41
 
41
42
  if (!valid) {
42
- const error = createError(400, 'Event object failed validation');
43
- request.event.headers = { ...request.event.headers
44
- };
45
-
46
43
  if (i18nEnabled) {
47
- const language = chooseLanguage(request.event, defaultLanguage);
48
- localize[language](inputSchema.errors);
44
+ const language = chooseLanguage(request.event, defaultLanguage)
45
+ localize[language](inputSchema.errors)
49
46
  }
50
47
 
51
- error.details = inputSchema.errors;
52
- throw error;
48
+ // Bad Request
49
+ throw createError(400, 'Event object failed validation', { cause: inputSchema.errors })
53
50
  }
54
- };
51
+ }
55
52
 
56
- const validatorMiddlewareAfter = async request => {
57
- const valid = outputSchema(request.response);
53
+ const validatorMiddlewareAfter = async (request) => {
54
+ const valid = outputSchema(request.response)
58
55
 
59
56
  if (!valid) {
60
- const error = createError(500, 'Response object failed validation');
61
- error.details = outputSchema.errors;
62
- error.response = request.response;
63
- throw error;
57
+ // Internal Server Error
58
+ throw createError(500, 'Response object failed validation', { cause: outputSchema.errors })
64
59
  }
65
- };
66
-
60
+ }
67
61
  return {
68
62
  before: inputSchema ? validatorMiddlewareBefore : undefined,
69
63
  after: outputSchema ? validatorMiddlewareAfter : undefined
70
- };
71
- };
64
+ }
65
+ }
72
66
 
67
+ // This is pulled out due to it's performance cost (50-100ms on cold start)
68
+ // Precompile your schema during a build step is recommended.
73
69
  const compile = (schema, ajvOptions, ajvInstance = null) => {
74
- if (typeof schema === 'function' || !schema) return schema;
75
- const options = { ...ajvDefaults,
76
- ...ajvOptions
77
- };
78
-
70
+ // Check if already compiled
71
+ if (typeof schema === 'function' || !schema) return schema
72
+ const options = { ...ajvDefaults, ...ajvOptions }
79
73
  if (!ajv) {
80
- ajv = ajvInstance ?? new Ajv(options);
81
- formats(ajv);
82
- formatsDraft2019(ajv);
74
+ ajv = ajvInstance ?? new Ajv(options)
75
+ formats(ajv)
76
+ formatsDraft2019(ajv)
83
77
  } else if (!ajvInstance) {
84
- ajv.opts = { ...ajv.opts,
85
- ...options
86
- };
78
+ // Update options when initializing the middleware multiple times
79
+ ajv.opts = { ...ajv.opts, ...options }
87
80
  }
81
+ return ajv.compile(schema)
82
+ }
88
83
 
89
- return ajv.compile(schema);
90
- };
91
-
84
+ /* in ajv-i18n Portuguese is represented as pt-BR */
92
85
  const languageNormalizationMap = {
93
86
  pt: 'pt-BR',
94
87
  'pt-br': 'pt-BR',
@@ -97,24 +90,21 @@ const languageNormalizationMap = {
97
90
  'zh-tw': 'zh-TW',
98
91
  zh_TW: 'zh-TW',
99
92
  zh_tw: 'zh-TW'
100
- };
101
-
102
- const normalizePreferredLanguage = lang => languageNormalizationMap[lang] ?? lang;
93
+ }
103
94
 
104
- const availableLanguages = Object.keys(localize);
95
+ const normalizePreferredLanguage = (lang) =>
96
+ languageNormalizationMap[lang] ?? lang
105
97
 
106
- const chooseLanguage = ({
107
- preferredLanguage
108
- }, defaultLanguage) => {
98
+ const availableLanguages = Object.keys(localize)
99
+ const chooseLanguage = ({ preferredLanguage }, defaultLanguage) => {
109
100
  if (preferredLanguage) {
110
- const lang = normalizePreferredLanguage(preferredLanguage);
111
-
101
+ const lang = normalizePreferredLanguage(preferredLanguage)
112
102
  if (availableLanguages.includes(lang)) {
113
- return lang;
103
+ return lang
114
104
  }
115
105
  }
116
106
 
117
- return defaultLanguage;
118
- };
107
+ return defaultLanguage
108
+ }
119
109
 
120
- export default validatorMiddleware;
110
+ export default validatorMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/validator",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "Validator middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -18,7 +18,8 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "test": "npm run test:unit",
21
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
22
23
  },
23
24
  "license": "MIT",
24
25
  "keywords": [
@@ -46,16 +47,16 @@
46
47
  },
47
48
  "homepage": "https://github.com/middyjs/middy#readme",
48
49
  "dependencies": {
49
- "@middy/util": "^3.0.0-alpha.2",
50
+ "@middy/util": "^3.0.0-alpha.3",
50
51
  "ajv": "8.8.2",
51
52
  "ajv-formats": "2.1.1",
52
53
  "ajv-formats-draft2019": "1.6.1",
53
54
  "ajv-i18n": "4.2.0"
54
55
  },
55
56
  "devDependencies": {
56
- "@middy/core": "^3.0.0-alpha.2",
57
+ "@middy/core": "^3.0.0-alpha.3",
57
58
  "@types/http-errors": "^1.8.1",
58
59
  "ajv-bsontype": "^1.0.7"
59
60
  },
60
- "gitHead": "de30419273ecbff08f367f47c7e320ec981cf145"
61
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156"
61
62
  }