@middy/validator 4.0.0 → 4.0.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/README.md +0 -124
- package/index.d.ts +4 -10
- package/package.json +6 -6
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "4.0.1",
|
|
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.
|
|
77
|
-
"ajv-cmd": "0.1.
|
|
76
|
+
"@middy/util": "4.0.1",
|
|
77
|
+
"ajv-cmd": "0.1.13"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
|
-
"@middy/core": "4.0.
|
|
81
|
-
"@types/http-errors": "^
|
|
80
|
+
"@middy/core": "4.0.1",
|
|
81
|
+
"@types/http-errors": "^2.0.0",
|
|
82
82
|
"ajv-bsontype": "^1.0.7"
|
|
83
83
|
},
|
|
84
|
-
"gitHead": "
|
|
84
|
+
"gitHead": "c5ece2bfbb0d607dcdea5685bf194a6cc19acc8d"
|
|
85
85
|
}
|