@aws-lambda-powertools/validation 0.0.0 → 2.17.0
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/LICENSE +14 -0
- package/README.md +276 -5
- package/lib/cjs/decorator.d.ts +138 -0
- package/lib/cjs/decorator.d.ts.map +1 -0
- package/lib/cjs/decorator.js +191 -0
- package/lib/cjs/errors.d.ts +74 -0
- package/lib/cjs/errors.d.ts.map +1 -0
- package/lib/cjs/errors.js +87 -0
- package/lib/cjs/middleware.d.ts +105 -0
- package/lib/cjs/middleware.d.ts.map +1 -0
- package/lib/cjs/middleware.js +148 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/types.d.ts +59 -0
- package/lib/cjs/types.d.ts.map +1 -0
- package/lib/cjs/types.js +2 -0
- package/lib/cjs/utils.d.ts +10 -0
- package/lib/cjs/utils.d.ts.map +1 -0
- package/lib/cjs/utils.js +18 -0
- package/lib/cjs/validate.d.ts +119 -0
- package/lib/cjs/validate.d.ts.map +1 -0
- package/lib/cjs/validate.js +154 -0
- package/lib/esm/decorator.d.ts +138 -0
- package/lib/esm/decorator.d.ts.map +1 -0
- package/lib/esm/decorator.js +189 -0
- package/lib/esm/errors.d.ts +74 -0
- package/lib/esm/errors.d.ts.map +1 -0
- package/lib/esm/errors.js +82 -0
- package/lib/esm/middleware.d.ts +105 -0
- package/lib/esm/middleware.d.ts.map +1 -0
- package/lib/esm/middleware.js +145 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/types.d.ts +59 -0
- package/lib/esm/types.d.ts.map +1 -0
- package/lib/esm/types.js +1 -0
- package/lib/esm/utils.d.ts +10 -0
- package/lib/esm/utils.d.ts.map +1 -0
- package/lib/esm/utils.js +15 -0
- package/lib/esm/validate.d.ts +119 -0
- package/lib/esm/validate.d.ts.map +1 -0
- package/lib/esm/validate.js +151 -0
- package/package.json +80 -37
- package/lib/index.d.ts +0 -0
- package/lib/index.js +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
5
|
+
the Software without restriction, including without limitation the rights to
|
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
7
|
+
the Software, and to permit persons to whom the Software is furnished to do so.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
10
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
11
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
12
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
13
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
14
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,9 +1,280 @@
|
|
|
1
1
|
# Powertools for AWS Lambda (TypeScript) - Validation Utility
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
5
|
-
| **This package is currently released as placeholder for the imminent launch**. <br /> The package is not intended for production usage, for additional info please reach out via our GitHub repository. |
|
|
3
|
+
This utility provides JSON Schema validation for events and responses, including JMESPath support to unwrap events before validation.
|
|
6
4
|
|
|
7
|
-
Powertools for AWS Lambda (TypeScript) is a developer toolkit to implement Serverless [best practices and increase developer velocity](https://docs.powertools.aws.dev/lambda
|
|
5
|
+
Powertools for AWS Lambda (TypeScript) is a developer toolkit to implement Serverless [best practices and increase developer velocity](https://docs.powertools.aws.dev/lambda/typescript/latest/#features). You can use the library in both TypeScript and JavaScript code bases.
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
To get started, install the package by running:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i @aws-lambda-powertools/validation
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
You can validate inbound and outbound payloads using the `@validator` class method decorator or `validator` Middy.js middleware.
|
|
16
|
+
|
|
17
|
+
You can also use the standalone `validate` function, if you want more control over the validation process such as handling a validation error.
|
|
18
|
+
|
|
19
|
+
### Validator decorator
|
|
20
|
+
|
|
21
|
+
The `@validator` decorator is a TypeScript class method decorator that you can use to validate both the incoming event and the response payload.
|
|
22
|
+
|
|
23
|
+
If the validation fails, we will throw a `SchemaValidationError`.
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { validator } from '@aws-lambda-powertools/validation/decorator';
|
|
27
|
+
import type { Context } from 'aws-lambda';
|
|
28
|
+
|
|
29
|
+
const inboundSchema = {
|
|
30
|
+
type: 'object',
|
|
31
|
+
properties: {
|
|
32
|
+
value: { type: 'number' },
|
|
33
|
+
},
|
|
34
|
+
required: ['value'],
|
|
35
|
+
additionalProperties: false,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const outboundSchema = {
|
|
39
|
+
type: 'object',
|
|
40
|
+
properties: {
|
|
41
|
+
result: { type: 'number' },
|
|
42
|
+
},
|
|
43
|
+
required: ['result'],
|
|
44
|
+
additionalProperties: false,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
class Lambda {
|
|
48
|
+
@validator({
|
|
49
|
+
inboundSchema,
|
|
50
|
+
outboundSchema,
|
|
51
|
+
})
|
|
52
|
+
async handler(event: { value: number }, _context: Context) {
|
|
53
|
+
// Your handler logic here
|
|
54
|
+
return { result: event.value * 2 };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const lambda = new Lambda();
|
|
59
|
+
export const handler = lambda.handler.bind(lambda);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
It's not mandatory to validate both the inbound and outbound payloads. You can either use one, the other, or both.
|
|
63
|
+
|
|
64
|
+
### Validator middleware
|
|
65
|
+
|
|
66
|
+
If you are using Middy.js, you can instead use the `validator` middleware to validate the incoming event and response payload.
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { validator } from '@aws-lambda-powertools/validation/middleware';
|
|
70
|
+
import middy from '@middy/core';
|
|
71
|
+
|
|
72
|
+
const inboundSchema = {
|
|
73
|
+
type: 'object',
|
|
74
|
+
properties: {
|
|
75
|
+
foo: { type: 'string' },
|
|
76
|
+
},
|
|
77
|
+
required: ['foo'],
|
|
78
|
+
additionalProperties: false,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const outboundSchema = {
|
|
82
|
+
type: 'object',
|
|
83
|
+
properties: {
|
|
84
|
+
bar: { type: 'number' },
|
|
85
|
+
},
|
|
86
|
+
required: ['bar'],
|
|
87
|
+
additionalProperties: false,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const handler = middy()
|
|
91
|
+
.use(validation({ inboundSchema, outboundSchema }))
|
|
92
|
+
.handler(async (event) => {
|
|
93
|
+
// Your handler logic here
|
|
94
|
+
return { bar: 42 };
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Like the `@validator` decorator, you can choose to validate only the inbound or outbound payload.
|
|
99
|
+
|
|
100
|
+
### Standalone validate function
|
|
101
|
+
|
|
102
|
+
The `validate` function gives you more control over the validation process, and is typically used within the Lambda handler, or any other function that needs to validate data.
|
|
103
|
+
|
|
104
|
+
When using the standalone function, you can gracefully handle schema validation errors by catching `SchemaValidationError` errors.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { validate } from '@aws-lambda-powertools/validation';
|
|
108
|
+
import { SchemaValidationError } from '@aws-lambda-powertools/validation/errors';
|
|
109
|
+
|
|
110
|
+
const schema = {
|
|
111
|
+
type: 'object',
|
|
112
|
+
properties: {
|
|
113
|
+
name: { type: 'string' },
|
|
114
|
+
age: { type: 'number' },
|
|
115
|
+
},
|
|
116
|
+
required: ['name', 'age'],
|
|
117
|
+
additionalProperties: false,
|
|
118
|
+
} as const;
|
|
119
|
+
|
|
120
|
+
const payload = { name: 'John', age: 30 };
|
|
121
|
+
|
|
122
|
+
export const handler = async (event: unknown) => {
|
|
123
|
+
try {
|
|
124
|
+
const validatedData = validate({
|
|
125
|
+
payload,
|
|
126
|
+
schema,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Your handler logic here
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (error instanceof SchemaValidationError) {
|
|
132
|
+
// Handle the validation error
|
|
133
|
+
return {
|
|
134
|
+
statusCode: 400,
|
|
135
|
+
body: JSON.stringify({ message: error.message }),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
// Handle other errors
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### JMESPath support
|
|
145
|
+
|
|
146
|
+
In some cases you might want to validate only a portion of the event payload - this is what the `envelope` option is for.
|
|
147
|
+
|
|
148
|
+
You can use JMESPath expressions to specify the path to the property you want to validate. The validator will unwrap the event before validating it.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { validate } from '@aws-lambda-powertools/validation';
|
|
152
|
+
|
|
153
|
+
const schema = {
|
|
154
|
+
type: 'object',
|
|
155
|
+
properties: {
|
|
156
|
+
user: { type: 'string' },
|
|
157
|
+
},
|
|
158
|
+
required: ['user'],
|
|
159
|
+
additionalProperties: false,
|
|
160
|
+
} as const;
|
|
161
|
+
|
|
162
|
+
const payload = {
|
|
163
|
+
data: {
|
|
164
|
+
user: 'Alice',
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const validatedData = validate({
|
|
169
|
+
payload,
|
|
170
|
+
schema,
|
|
171
|
+
envelope: 'data',
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Extending the validator
|
|
176
|
+
|
|
177
|
+
Since the validator is built on top of [Ajv](https://ajv.js.org/), you can extend it with custom formats and external schemas, as well as bringing your own `ajv` instance.
|
|
178
|
+
|
|
179
|
+
The example below shows how to pass additional options to the `validate` function, but you can also pass them to the `@validator` decorator and `validator` middleware.
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import { validate } from '@aws-lambda-powertools/validation';
|
|
183
|
+
|
|
184
|
+
const formats = {
|
|
185
|
+
ageRange: (value: number) => return value >= 0 && value <= 120,
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const definitionSchema = {
|
|
189
|
+
$id: 'https://example.com/schemas/definitions.json',
|
|
190
|
+
definitions: {
|
|
191
|
+
user: {
|
|
192
|
+
type: 'object',
|
|
193
|
+
properties: {
|
|
194
|
+
name: { type: 'string' },
|
|
195
|
+
age: { type: 'number', format: 'ageRange' },
|
|
196
|
+
},
|
|
197
|
+
required: ['name', 'age'],
|
|
198
|
+
additionalProperties: false,
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
} as const;
|
|
202
|
+
|
|
203
|
+
const schema = {
|
|
204
|
+
$id: 'https://example.com/schemas/user.json',
|
|
205
|
+
type: 'object',
|
|
206
|
+
properties: {
|
|
207
|
+
user: { $ref: 'definitions.json#/definitions/user' },
|
|
208
|
+
},
|
|
209
|
+
required: ['user'],
|
|
210
|
+
additionalProperties: false,
|
|
211
|
+
} as const;
|
|
212
|
+
|
|
213
|
+
const payload = {
|
|
214
|
+
user: {
|
|
215
|
+
name: 'Alice',
|
|
216
|
+
age: 25,
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const validatedData = validate({
|
|
221
|
+
payload,
|
|
222
|
+
schema,
|
|
223
|
+
externalRefs: [definitionSchema],
|
|
224
|
+
formats,
|
|
225
|
+
});
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
For more information on how to use the `validate` function, please refer to the [documentation](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/validation).
|
|
229
|
+
|
|
230
|
+
## Contribute
|
|
231
|
+
|
|
232
|
+
If you are interested in contributing to this project, please refer to our [Contributing Guidelines](https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/CONTRIBUTING.md).
|
|
233
|
+
|
|
234
|
+
## Roadmap
|
|
235
|
+
|
|
236
|
+
The roadmap of Powertools for AWS Lambda (TypeScript) is driven by customers’ demand.
|
|
237
|
+
Help us prioritize upcoming functionalities or utilities by [upvoting existing RFCs and feature requests](https://github.com/aws-powertools/powertools-lambda-typescript/issues), or [creating new ones](https://github.com/aws-powertools/powertools-lambda-typescript/issues/new/choose), in this GitHub repository.
|
|
238
|
+
|
|
239
|
+
## Connect
|
|
240
|
+
|
|
241
|
+
- **Powertools for AWS Lambda on Discord**: `#typescript` - **[Invite link](https://discord.gg/B8zZKbbyET)**
|
|
242
|
+
- **Email**: <aws-lambda-powertools-feedback@amazon.com>
|
|
243
|
+
|
|
244
|
+
## How to support Powertools for AWS Lambda (TypeScript)?
|
|
245
|
+
|
|
246
|
+
### Becoming a reference customer
|
|
247
|
+
|
|
248
|
+
Knowing which companies are using this library is important to help prioritize the project internally. If your company is using Powertools for AWS Lambda (TypeScript), you can request to have your name and logo added to the README file by raising a [Support Powertools for AWS Lambda (TypeScript) (become a reference)](https://s12d.com/become-reference-pt-ts) issue.
|
|
249
|
+
|
|
250
|
+
The following companies, among others, use Powertools:
|
|
251
|
+
|
|
252
|
+
- [Alma Media](https://www.almamedia.fi)
|
|
253
|
+
- [AppYourself](https://appyourself.net)
|
|
254
|
+
- [Bailey Nelson](https://www.baileynelson.com.au)
|
|
255
|
+
- [Banxware](https://www.banxware.com)
|
|
256
|
+
- [Caylent](https://caylent.com/)
|
|
257
|
+
- [Certible](https://www.certible.com/)
|
|
258
|
+
- [Elva](https://elva-group.com)
|
|
259
|
+
- [Flyweight](https://flyweight.io/)
|
|
260
|
+
- [globaldatanet](https://globaldatanet.com/)
|
|
261
|
+
- [Guild](https://guild.com)
|
|
262
|
+
- [Hashnode](https://hashnode.com/)
|
|
263
|
+
- [LocalStack](https://localstack.cloud/)
|
|
264
|
+
- [Perfect Post](https://www.perfectpost.fr)
|
|
265
|
+
- [Sennder](https://sennder.com/)
|
|
266
|
+
- [tecRacer GmbH & Co. KG](https://www.tecracer.com/)
|
|
267
|
+
- [Trek10](https://www.trek10.com/)
|
|
268
|
+
- [WeSchool](https://www.weschool.com)
|
|
269
|
+
|
|
270
|
+
### Sharing your work
|
|
271
|
+
|
|
272
|
+
Share what you did with Powertools for AWS Lambda (TypeScript) 💞💞. Blog post, workshops, presentation, sample apps and others. Check out what the community has already shared about Powertools for AWS Lambda (TypeScript) [here](https://docs.powertools.aws.dev/lambda/typescript/latest/we_made_this).
|
|
273
|
+
|
|
274
|
+
### Using Lambda Layer
|
|
275
|
+
|
|
276
|
+
This helps us understand who uses Powertools for AWS Lambda (TypeScript) in a non-intrusive way, and helps us gain future investments for other Powertools for AWS Lambda languages. When [using Layers](https://docs.powertools.aws.dev/lambda/typescript/latest/#lambda-layer), you can add Powertools as a dev dependency to not impact the development process.
|
|
277
|
+
|
|
278
|
+
## License
|
|
279
|
+
|
|
280
|
+
This library is licensed under the MIT-0 License. See the LICENSE file.
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { ValidatorOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Class method decorator to validate the input and output of a method using JSON Schema.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { validator } from '@aws-lambda-powertools/validation/decorator';
|
|
8
|
+
* import type { Context } from 'aws-lambda';
|
|
9
|
+
*
|
|
10
|
+
* const inboundSchema = {
|
|
11
|
+
* type: 'object',
|
|
12
|
+
* properties: {
|
|
13
|
+
* value: { type: 'number' },
|
|
14
|
+
* },
|
|
15
|
+
* required: ['value'],
|
|
16
|
+
* additionalProperties: false,
|
|
17
|
+
* };
|
|
18
|
+
*
|
|
19
|
+
* const outboundSchema = {
|
|
20
|
+
* type: 'object',
|
|
21
|
+
* properties: {
|
|
22
|
+
* result: { type: 'number' },
|
|
23
|
+
* },
|
|
24
|
+
* required: ['result'],
|
|
25
|
+
* additionalProperties: false,
|
|
26
|
+
* };
|
|
27
|
+
*
|
|
28
|
+
* class Lambda {
|
|
29
|
+
* @validator({
|
|
30
|
+
* inboundSchema,
|
|
31
|
+
* outboundSchema,
|
|
32
|
+
* })
|
|
33
|
+
* async handler(event: { value: number }, _context: Context) {
|
|
34
|
+
* // Your handler logic here
|
|
35
|
+
* return { result: event.value * 2 };
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* const lambda = new Lambda();
|
|
40
|
+
* export const handler = lambda.handler.bind(lambda);
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* When validating nested payloads, you can also provide an optional JMESPath expression to extract a specific part of the payload
|
|
44
|
+
* before validation using the `envelope` parameter. This is useful when the payload is nested or when you want to validate only a specific part of it.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* import { validator } from '@aws-lambda-powertools/validation/decorator';
|
|
49
|
+
*
|
|
50
|
+
* class Lambda {
|
|
51
|
+
* @validator({
|
|
52
|
+
* inboundSchema: {
|
|
53
|
+
* type: 'number',
|
|
54
|
+
* },
|
|
55
|
+
* envelope: 'nested',
|
|
56
|
+
* })
|
|
57
|
+
* async handler(event: number, _context: Context) {
|
|
58
|
+
* return { result: event * 2 };
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
*
|
|
62
|
+
* const lambda = new Lambda();
|
|
63
|
+
* export const handler = lambda.handler.bind(lambda);
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* Since the Validation utility is built on top of Ajv, you can also provide custom formats and external references
|
|
67
|
+
* to the validation process. This allows you to extend the validation capabilities of Ajv to suit your specific needs.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
72
|
+
*
|
|
73
|
+
* const formats = {
|
|
74
|
+
* ageRange: (value: number) => return value >= 0 && value <= 120,
|
|
75
|
+
* };
|
|
76
|
+
*
|
|
77
|
+
* const definitionSchema = {
|
|
78
|
+
* $id: 'https://example.com/schemas/definitions.json',
|
|
79
|
+
* definitions: {
|
|
80
|
+
* user: {
|
|
81
|
+
* type: 'object',
|
|
82
|
+
* properties: {
|
|
83
|
+
* name: { type: 'string' },
|
|
84
|
+
* age: { type: 'number', format: 'ageRange' },
|
|
85
|
+
* },
|
|
86
|
+
* required: ['name', 'age'],
|
|
87
|
+
* additionalProperties: false,
|
|
88
|
+
* }
|
|
89
|
+
* }
|
|
90
|
+
* } as const;
|
|
91
|
+
*
|
|
92
|
+
* const schema = {
|
|
93
|
+
* $id: 'https://example.com/schemas/user.json',
|
|
94
|
+
* type: 'object',
|
|
95
|
+
* properties: {
|
|
96
|
+
* user: { $ref: 'definitions.json#/definitions/user' },
|
|
97
|
+
* },
|
|
98
|
+
* required: ['user'],
|
|
99
|
+
* additionalProperties: false,
|
|
100
|
+
* } as const;
|
|
101
|
+
*
|
|
102
|
+
* const payload = {
|
|
103
|
+
* user: {
|
|
104
|
+
* name: 'Alice',
|
|
105
|
+
* age: 25,
|
|
106
|
+
* },
|
|
107
|
+
* };
|
|
108
|
+
*
|
|
109
|
+
* class Lambda {
|
|
110
|
+
* @validator({
|
|
111
|
+
* inboundSchema: schema,
|
|
112
|
+
* externalRefs: [definitionSchema],
|
|
113
|
+
* formats,
|
|
114
|
+
* })
|
|
115
|
+
* async handler(event: { value: number }, _context: Context) {
|
|
116
|
+
* // Your handler logic here
|
|
117
|
+
* return { result: event.value * 2 };
|
|
118
|
+
* }
|
|
119
|
+
* }
|
|
120
|
+
*
|
|
121
|
+
* const lambda = new Lambda();
|
|
122
|
+
* export const handler = lambda.handler.bind(lambda);
|
|
123
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* Additionally, you can provide an existing Ajv instance to reuse the same instance across multiple validations. If
|
|
126
|
+
* you don't provide an Ajv instance, a new one will be created for each validation.
|
|
127
|
+
*
|
|
128
|
+
* @param options - The validation options
|
|
129
|
+
* @param options.inboundSchema - The JSON schema for inbound validation.
|
|
130
|
+
* @param options.outboundSchema - The JSON schema for outbound validation.
|
|
131
|
+
* @param options.envelope - Optional JMESPath expression to use as envelope for the payload.
|
|
132
|
+
* @param options.formats - Optional formats for validation.
|
|
133
|
+
* @param options.externalRefs - Optional external references for validation.
|
|
134
|
+
* @param options.ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
135
|
+
*/
|
|
136
|
+
declare function validator(options: ValidatorOptions): (_target: unknown, _propertyKey: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
137
|
+
export { validator };
|
|
138
|
+
//# sourceMappingURL=decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorator.d.ts","sourceRoot":"","sources":["../../src/decorator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAInD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqIG;AACH,iBAAS,SAAS,CAAC,OAAO,EAAE,gBAAgB,IAExC,SAAS,OAAO,EAChB,cAAc,MAAM,GAAG,MAAM,EAC7B,YAAY,kBAAkB,wBAuDjC;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validator = validator;
|
|
4
|
+
const errors_js_1 = require("./errors.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const validate_js_1 = require("./validate.js");
|
|
7
|
+
/**
|
|
8
|
+
* Class method decorator to validate the input and output of a method using JSON Schema.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { validator } from '@aws-lambda-powertools/validation/decorator';
|
|
13
|
+
* import type { Context } from 'aws-lambda';
|
|
14
|
+
*
|
|
15
|
+
* const inboundSchema = {
|
|
16
|
+
* type: 'object',
|
|
17
|
+
* properties: {
|
|
18
|
+
* value: { type: 'number' },
|
|
19
|
+
* },
|
|
20
|
+
* required: ['value'],
|
|
21
|
+
* additionalProperties: false,
|
|
22
|
+
* };
|
|
23
|
+
*
|
|
24
|
+
* const outboundSchema = {
|
|
25
|
+
* type: 'object',
|
|
26
|
+
* properties: {
|
|
27
|
+
* result: { type: 'number' },
|
|
28
|
+
* },
|
|
29
|
+
* required: ['result'],
|
|
30
|
+
* additionalProperties: false,
|
|
31
|
+
* };
|
|
32
|
+
*
|
|
33
|
+
* class Lambda {
|
|
34
|
+
* @validator({
|
|
35
|
+
* inboundSchema,
|
|
36
|
+
* outboundSchema,
|
|
37
|
+
* })
|
|
38
|
+
* async handler(event: { value: number }, _context: Context) {
|
|
39
|
+
* // Your handler logic here
|
|
40
|
+
* return { result: event.value * 2 };
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* const lambda = new Lambda();
|
|
45
|
+
* export const handler = lambda.handler.bind(lambda);
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* When validating nested payloads, you can also provide an optional JMESPath expression to extract a specific part of the payload
|
|
49
|
+
* before validation using the `envelope` parameter. This is useful when the payload is nested or when you want to validate only a specific part of it.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* import { validator } from '@aws-lambda-powertools/validation/decorator';
|
|
54
|
+
*
|
|
55
|
+
* class Lambda {
|
|
56
|
+
* @validator({
|
|
57
|
+
* inboundSchema: {
|
|
58
|
+
* type: 'number',
|
|
59
|
+
* },
|
|
60
|
+
* envelope: 'nested',
|
|
61
|
+
* })
|
|
62
|
+
* async handler(event: number, _context: Context) {
|
|
63
|
+
* return { result: event * 2 };
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* const lambda = new Lambda();
|
|
68
|
+
* export const handler = lambda.handler.bind(lambda);
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* Since the Validation utility is built on top of Ajv, you can also provide custom formats and external references
|
|
72
|
+
* to the validation process. This allows you to extend the validation capabilities of Ajv to suit your specific needs.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
77
|
+
*
|
|
78
|
+
* const formats = {
|
|
79
|
+
* ageRange: (value: number) => return value >= 0 && value <= 120,
|
|
80
|
+
* };
|
|
81
|
+
*
|
|
82
|
+
* const definitionSchema = {
|
|
83
|
+
* $id: 'https://example.com/schemas/definitions.json',
|
|
84
|
+
* definitions: {
|
|
85
|
+
* user: {
|
|
86
|
+
* type: 'object',
|
|
87
|
+
* properties: {
|
|
88
|
+
* name: { type: 'string' },
|
|
89
|
+
* age: { type: 'number', format: 'ageRange' },
|
|
90
|
+
* },
|
|
91
|
+
* required: ['name', 'age'],
|
|
92
|
+
* additionalProperties: false,
|
|
93
|
+
* }
|
|
94
|
+
* }
|
|
95
|
+
* } as const;
|
|
96
|
+
*
|
|
97
|
+
* const schema = {
|
|
98
|
+
* $id: 'https://example.com/schemas/user.json',
|
|
99
|
+
* type: 'object',
|
|
100
|
+
* properties: {
|
|
101
|
+
* user: { $ref: 'definitions.json#/definitions/user' },
|
|
102
|
+
* },
|
|
103
|
+
* required: ['user'],
|
|
104
|
+
* additionalProperties: false,
|
|
105
|
+
* } as const;
|
|
106
|
+
*
|
|
107
|
+
* const payload = {
|
|
108
|
+
* user: {
|
|
109
|
+
* name: 'Alice',
|
|
110
|
+
* age: 25,
|
|
111
|
+
* },
|
|
112
|
+
* };
|
|
113
|
+
*
|
|
114
|
+
* class Lambda {
|
|
115
|
+
* @validator({
|
|
116
|
+
* inboundSchema: schema,
|
|
117
|
+
* externalRefs: [definitionSchema],
|
|
118
|
+
* formats,
|
|
119
|
+
* })
|
|
120
|
+
* async handler(event: { value: number }, _context: Context) {
|
|
121
|
+
* // Your handler logic here
|
|
122
|
+
* return { result: event.value * 2 };
|
|
123
|
+
* }
|
|
124
|
+
* }
|
|
125
|
+
*
|
|
126
|
+
* const lambda = new Lambda();
|
|
127
|
+
* export const handler = lambda.handler.bind(lambda);
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* Additionally, you can provide an existing Ajv instance to reuse the same instance across multiple validations. If
|
|
131
|
+
* you don't provide an Ajv instance, a new one will be created for each validation.
|
|
132
|
+
*
|
|
133
|
+
* @param options - The validation options
|
|
134
|
+
* @param options.inboundSchema - The JSON schema for inbound validation.
|
|
135
|
+
* @param options.outboundSchema - The JSON schema for outbound validation.
|
|
136
|
+
* @param options.envelope - Optional JMESPath expression to use as envelope for the payload.
|
|
137
|
+
* @param options.formats - Optional formats for validation.
|
|
138
|
+
* @param options.externalRefs - Optional external references for validation.
|
|
139
|
+
* @param options.ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
140
|
+
*/
|
|
141
|
+
function validator(options) {
|
|
142
|
+
return (_target, _propertyKey, descriptor) => {
|
|
143
|
+
const { inboundSchema, outboundSchema, envelope, formats, externalRefs, ajv, } = options;
|
|
144
|
+
if (!options.inboundSchema && !outboundSchema) {
|
|
145
|
+
return descriptor;
|
|
146
|
+
}
|
|
147
|
+
const originalMethod = descriptor.value;
|
|
148
|
+
descriptor.value = async function (...args) {
|
|
149
|
+
let validatedInput = args[0];
|
|
150
|
+
if (inboundSchema) {
|
|
151
|
+
try {
|
|
152
|
+
validatedInput = (0, validate_js_1.validate)({
|
|
153
|
+
payload: validatedInput,
|
|
154
|
+
schema: inboundSchema,
|
|
155
|
+
envelope: envelope,
|
|
156
|
+
formats: formats,
|
|
157
|
+
externalRefs: externalRefs,
|
|
158
|
+
ajv: ajv,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
throw new errors_js_1.SchemaValidationError('Inbound schema validation failed', {
|
|
163
|
+
cause: (0, utils_js_1.getErrorCause)(error),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const result = await originalMethod.apply(this, [
|
|
168
|
+
validatedInput,
|
|
169
|
+
...args.slice(1),
|
|
170
|
+
]);
|
|
171
|
+
if (outboundSchema) {
|
|
172
|
+
try {
|
|
173
|
+
return (0, validate_js_1.validate)({
|
|
174
|
+
payload: result,
|
|
175
|
+
schema: outboundSchema,
|
|
176
|
+
formats: formats,
|
|
177
|
+
externalRefs: externalRefs,
|
|
178
|
+
ajv: ajv,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
throw new errors_js_1.SchemaValidationError('Outbound schema validation failed', {
|
|
183
|
+
cause: (0, utils_js_1.getErrorCause)(error),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return result;
|
|
188
|
+
};
|
|
189
|
+
return descriptor;
|
|
190
|
+
};
|
|
191
|
+
}
|