@middy/validator 4.0.0 → 4.0.2

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 CHANGED
@@ -36,130 +36,6 @@
36
36
  <p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/validator">https://middy.js.org/docs/middlewares/validator</a></p>
37
37
  </div>
38
38
 
39
- This middleware automatically validates incoming events and outgoing responses against custom
40
- schemas defined with the [JSON schema syntax](https://json-schema.org/).
41
-
42
- If an incoming event fails validation a `BadRequest` error is raised.
43
- If an outgoing response fails validation a `InternalServerError` error is
44
- raised.
45
-
46
- This middleware can be used in combination with
47
- [`httpErrorHandler`](#httperrorhandler) to automatically return the right
48
- response to the user.
49
-
50
- 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).
51
-
52
- 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.
53
-
54
- ## Install
55
-
56
- To install this middleware you can use NPM:
57
-
58
- ```bash
59
- npm install --save @middy/validator
60
- ```
61
-
62
-
63
- ## Options
64
-
65
- - `eventSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
66
- to validate the input (`request.event`) of the Lambda handler. Supports alias `inputSchema`
67
- - `contextSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
68
- to validate the input (`request.context`) of the Lambda handler. Has additional support for `typeof` keyword to allow validation of `"typeof":"function"`.
69
- - `responseSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
70
- to validate the output (`request.response`) of the Lambda handler. Supports alias `inputSchema`
71
- - `ajvOptions` (object) (default `undefined`): Options to pass to [ajv](https://ajv.js.org/docs/api.html#options)
72
- class constructor. Defaults are `{ strict: true, coerceTypes: 'array', allErrors: true, useDefaults: 'empty', messages: false, defaultLanguage: 'en' }`.
73
- - `i18nEnabled` (boolean) (default `true`): Option to disable i18n default package.
74
-
75
- NOTES:
76
- - At least one of `eventSchema` or `responseSchema` is required.
77
- - **Important** Compiling schemas on the fly will cause a 50-100ms performance hit during cold start for simple JSON Schemas. Precompiling is highly recommended.
78
- - Default ajv plugins used: `ajv-i18n`, `ajv-formats`, `ajv-formats-draft2019`
79
- - 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`.
80
-
81
- ## Sample usage
82
-
83
- Example for input validation:
84
-
85
- ```javascript
86
- import middy from '@middy/core'
87
- import validator from '@middy/validator'
88
-
89
- const handler = middy((event, context) => {
90
- return {}
91
- })
92
-
93
- const schema = {
94
- required: ['body', 'foo'],
95
- properties: {
96
- // this will pass validation
97
- body: {
98
- type: 'string'
99
- },
100
- // this won't as it won't be in the event
101
- foo: {
102
- type: 'string'
103
- }
104
- }
105
- }
106
-
107
- handler.use(validator({
108
- inputSchema: schema
109
- }))
110
-
111
- // invokes the handler, note that property foo is missing
112
- const event = {
113
- body: JSON.stringify({something: 'somethingelse'})
114
- }
115
- handler(event, {}, (err, res) => {
116
- t.is(err.message,'Event object failed validation')
117
- })
118
- ```
119
-
120
- Example for output validation:
121
-
122
- ```javascript
123
- import middy from '@middy/core'
124
- import validator from '@middy/validator'
125
-
126
- const handler = middy((event, context) => {
127
- return {}
128
- })
129
-
130
- const schema = {
131
- required: ['body', 'statusCode'],
132
- properties: {
133
- body: {
134
- type: 'object'
135
- },
136
- statusCode: {
137
- type: 'number'
138
- }
139
- }
140
- }
141
-
142
- handler.use(validator({responseSchema: schema}))
143
-
144
- handler({}, {}, (err, response) => {
145
- t.not(err, null)
146
- t.is(err.message,'Response object failed validation')
147
- expect(response).not.toBe(null) // it doesn't destroy the response so it can be used by other middlewares
148
- })
149
- ```
150
-
151
-
152
-
153
- ## Middy documentation and examples
154
-
155
- For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).
156
-
157
-
158
- ## Contributing
159
-
160
- Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
161
-
162
-
163
39
  ## License
164
40
 
165
41
  Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
package/index.d.ts CHANGED
@@ -1,17 +1,11 @@
1
1
  import middy from '@middy/core'
2
2
 
3
- import Ajv, { Options as AjvOptions } from 'ajv'
4
-
5
3
  interface Options {
6
- inputSchema?: object | any // Deprecate v4
7
- outputSchema?: object | any // Deprecate v4
8
- eventSchema?: object | any
9
- contextSchema?: object | any
10
- responseSchema?: object | any
11
- ajvOptions?: Partial<AjvOptions>
12
- ajvInstance?: Ajv
4
+ eventSchema?: Function | any
5
+ contextSchema?: Function | any
6
+ responseSchema?: Function | any
13
7
  defaultLanguage?: string
14
- i18nEnabled?: boolean
8
+ languages?: object | any
15
9
  }
16
10
 
17
11
  declare function validator (options?: Options): middy.MiddlewareObj
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/validator",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "description": "Validator middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -73,13 +73,13 @@
73
73
  },
74
74
  "homepage": "https://middy.js.org",
75
75
  "dependencies": {
76
- "@middy/util": "4.0.0",
77
- "ajv-cmd": "0.1.10"
76
+ "@middy/util": "4.0.2",
77
+ "ajv-cmd": "0.1.14"
78
78
  },
79
79
  "devDependencies": {
80
- "@middy/core": "4.0.0",
81
- "@types/http-errors": "^1.8.1",
80
+ "@middy/core": "4.0.2",
81
+ "@types/http-errors": "^2.0.0",
82
82
  "ajv-bsontype": "^1.0.7"
83
83
  },
84
- "gitHead": "582286144bcd79968a8c7c2f8867a23c80079a47"
84
+ "gitHead": "c77c9413ecb80999a71b67ff97edac1fed2ca754"
85
85
  }
package/transpile.cjs CHANGED
@@ -12,7 +12,13 @@ _export(exports, {
12
12
  transpileSchema: ()=>transpileSchema,
13
13
  transpileLocale: ()=>transpileLocale
14
14
  });
15
- const _ajvCmd = require("ajv-cmd");
15
+ const _compileJs = _interopRequireDefault(require("ajv-cmd/compile.js"));
16
+ const _ftlJs = _interopRequireDefault(require("ajv-cmd/ftl.js"));
17
+ function _interopRequireDefault(obj) {
18
+ return obj && obj.__esModule ? obj : {
19
+ default: obj
20
+ };
21
+ }
16
22
  const ajvDefaults = {
17
23
  strict: true,
18
24
  coerceTypes: 'array',
@@ -25,9 +31,9 @@ const transpileSchema = (schema, ajvOptions)=>{
25
31
  ...ajvDefaults,
26
32
  ...ajvOptions
27
33
  };
28
- return (0, _ajvCmd.compile)(schema, options);
34
+ return (0, _compileJs.default)(schema, options);
29
35
  };
30
- const transpileLocale = _ajvCmd.ftl;
36
+ const transpileLocale = _ftlJs.default;
31
37
 
32
38
 
33
39
  //# sourceMappingURL=transpile.cjs.map
package/transpile.js CHANGED
@@ -1,4 +1,5 @@
1
- import { compile, ftl } from 'ajv-cmd';
1
+ import compileSchema from 'ajv-cmd/compile.js';
2
+ import transpileFTL from 'ajv-cmd/ftl.js';
2
3
  const ajvDefaults = {
3
4
  strict: true,
4
5
  coerceTypes: 'array',
@@ -11,9 +12,9 @@ export const transpileSchema = (schema, ajvOptions)=>{
11
12
  ...ajvDefaults,
12
13
  ...ajvOptions
13
14
  };
14
- return compile(schema, options);
15
+ return compileSchema(schema, options);
15
16
  };
16
- export const transpileLocale = ftl;
17
+ export const transpileLocale = transpileFTL;
17
18
 
18
19
 
19
20
  //# sourceMappingURL=transpile.js.map