@aws-lambda-powertools/validation 0.0.0 → 2.18.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
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validate = void 0;
|
|
4
|
+
const jmespath_1 = require("@aws-lambda-powertools/jmespath");
|
|
5
|
+
const ajv_1 = require("ajv");
|
|
6
|
+
const errors_js_1 = require("./errors.js");
|
|
7
|
+
/**
|
|
8
|
+
* Validates a payload against a JSON schema using Ajv.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
13
|
+
*
|
|
14
|
+
* const schema = {
|
|
15
|
+
* type: 'object',
|
|
16
|
+
* properties: {
|
|
17
|
+
* name: { type: 'string' },
|
|
18
|
+
* age: { type: 'number' },
|
|
19
|
+
* },
|
|
20
|
+
* required: ['name', 'age'],
|
|
21
|
+
* additionalProperties: false,
|
|
22
|
+
* } as const;
|
|
23
|
+
*
|
|
24
|
+
* const payload = { name: 'John', age: 30 };
|
|
25
|
+
*
|
|
26
|
+
* const validatedData = validate({
|
|
27
|
+
* payload,
|
|
28
|
+
* schema,
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* When validating, you can also provide an optional JMESPath expression to extract a specific part of the payload
|
|
33
|
+
* before validation using the `envelope` parameter. This is useful when the payload is nested or when you want to
|
|
34
|
+
* validate only a specific part of it.
|
|
35
|
+
*
|
|
36
|
+
* ```typescript
|
|
37
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
38
|
+
*
|
|
39
|
+
* const schema = {
|
|
40
|
+
* type: 'object',
|
|
41
|
+
* properties: {
|
|
42
|
+
* user: { type: 'string' },
|
|
43
|
+
* },
|
|
44
|
+
* required: ['user'],
|
|
45
|
+
* additionalProperties: false,
|
|
46
|
+
* } as const;
|
|
47
|
+
*
|
|
48
|
+
* const payload = {
|
|
49
|
+
* data: {
|
|
50
|
+
* user: 'Alice',
|
|
51
|
+
* },
|
|
52
|
+
* };
|
|
53
|
+
*
|
|
54
|
+
* const validatedData = validate({
|
|
55
|
+
* payload,
|
|
56
|
+
* schema,
|
|
57
|
+
* envelope: 'data',
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* Since the Validation utility is built on top of Ajv, you can also provide custom formats and external references
|
|
62
|
+
* to the validation process. This allows you to extend the validation capabilities of Ajv to suit your specific needs.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
67
|
+
*
|
|
68
|
+
* const formats = {
|
|
69
|
+
* ageRange: (value: number) => return value >= 0 && value <= 120,
|
|
70
|
+
* };
|
|
71
|
+
*
|
|
72
|
+
* const definitionSchema = {
|
|
73
|
+
* $id: 'https://example.com/schemas/definitions.json',
|
|
74
|
+
* definitions: {
|
|
75
|
+
* user: {
|
|
76
|
+
* type: 'object',
|
|
77
|
+
* properties: {
|
|
78
|
+
* name: { type: 'string' },
|
|
79
|
+
* age: { type: 'number', format: 'ageRange' },
|
|
80
|
+
* },
|
|
81
|
+
* required: ['name', 'age'],
|
|
82
|
+
* additionalProperties: false,
|
|
83
|
+
* }
|
|
84
|
+
* }
|
|
85
|
+
* } as const;
|
|
86
|
+
*
|
|
87
|
+
* const schema = {
|
|
88
|
+
* $id: 'https://example.com/schemas/user.json',
|
|
89
|
+
* type: 'object',
|
|
90
|
+
* properties: {
|
|
91
|
+
* user: { $ref: 'definitions.json#/definitions/user' },
|
|
92
|
+
* },
|
|
93
|
+
* required: ['user'],
|
|
94
|
+
* additionalProperties: false,
|
|
95
|
+
* } as const;
|
|
96
|
+
*
|
|
97
|
+
* const payload = {
|
|
98
|
+
* user: {
|
|
99
|
+
* name: 'Alice',
|
|
100
|
+
* age: 25,
|
|
101
|
+
* },
|
|
102
|
+
* };
|
|
103
|
+
*
|
|
104
|
+
* const validatedData = validate({
|
|
105
|
+
* payload,
|
|
106
|
+
* schema,
|
|
107
|
+
* externalRefs: [definitionSchema],
|
|
108
|
+
* formats,
|
|
109
|
+
* });
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* Additionally, you can provide an existing Ajv instance to reuse the same instance across multiple validations. If
|
|
113
|
+
* you don't provide an Ajv instance, a new one will be created for each validation.
|
|
114
|
+
*
|
|
115
|
+
* @param params.payload - The payload to validate.
|
|
116
|
+
* @param params.schema - The JSON schema to validate against.
|
|
117
|
+
* @param params.envelope - Optional JMESPath expression to use as envelope for the payload.
|
|
118
|
+
* @param params.formats - Optional formats for validation.
|
|
119
|
+
* @param params.externalRefs - Optional external references for validation.
|
|
120
|
+
* @param params.ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
121
|
+
*/
|
|
122
|
+
const validate = (params) => {
|
|
123
|
+
const { payload, schema, envelope, formats, externalRefs, ajv } = params;
|
|
124
|
+
const ajvInstance = ajv || new ajv_1.Ajv({ allErrors: true });
|
|
125
|
+
if (formats) {
|
|
126
|
+
for (const key of Object.keys(formats)) {
|
|
127
|
+
ajvInstance.addFormat(key, formats[key]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (externalRefs) {
|
|
131
|
+
ajvInstance.addSchema(externalRefs);
|
|
132
|
+
}
|
|
133
|
+
let validateFn;
|
|
134
|
+
try {
|
|
135
|
+
validateFn = ajvInstance.compile(schema);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
throw new errors_js_1.SchemaCompilationError('Failed to compile schema', {
|
|
139
|
+
cause: error,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
const trimmedEnvelope = envelope?.trim();
|
|
143
|
+
const dataToValidate = trimmedEnvelope
|
|
144
|
+
? (0, jmespath_1.search)(trimmedEnvelope, payload)
|
|
145
|
+
: payload;
|
|
146
|
+
const valid = validateFn(dataToValidate);
|
|
147
|
+
if (!valid) {
|
|
148
|
+
throw new errors_js_1.SchemaValidationError('Schema validation failed', {
|
|
149
|
+
cause: validateFn.errors,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return dataToValidate;
|
|
153
|
+
};
|
|
154
|
+
exports.validate = validate;
|
|
@@ -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,189 @@
|
|
|
1
|
+
import { SchemaValidationError } from './errors.js';
|
|
2
|
+
import { getErrorCause } from './utils.js';
|
|
3
|
+
import { validate } from './validate.js';
|
|
4
|
+
/**
|
|
5
|
+
* Class method decorator to validate the input and output of a method using JSON Schema.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { validator } from '@aws-lambda-powertools/validation/decorator';
|
|
10
|
+
* import type { Context } from 'aws-lambda';
|
|
11
|
+
*
|
|
12
|
+
* const inboundSchema = {
|
|
13
|
+
* type: 'object',
|
|
14
|
+
* properties: {
|
|
15
|
+
* value: { type: 'number' },
|
|
16
|
+
* },
|
|
17
|
+
* required: ['value'],
|
|
18
|
+
* additionalProperties: false,
|
|
19
|
+
* };
|
|
20
|
+
*
|
|
21
|
+
* const outboundSchema = {
|
|
22
|
+
* type: 'object',
|
|
23
|
+
* properties: {
|
|
24
|
+
* result: { type: 'number' },
|
|
25
|
+
* },
|
|
26
|
+
* required: ['result'],
|
|
27
|
+
* additionalProperties: false,
|
|
28
|
+
* };
|
|
29
|
+
*
|
|
30
|
+
* class Lambda {
|
|
31
|
+
* @validator({
|
|
32
|
+
* inboundSchema,
|
|
33
|
+
* outboundSchema,
|
|
34
|
+
* })
|
|
35
|
+
* async handler(event: { value: number }, _context: Context) {
|
|
36
|
+
* // Your handler logic here
|
|
37
|
+
* return { result: event.value * 2 };
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* const lambda = new Lambda();
|
|
42
|
+
* export const handler = lambda.handler.bind(lambda);
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* When validating nested payloads, you can also provide an optional JMESPath expression to extract a specific part of the payload
|
|
46
|
+
* 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.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* import { validator } from '@aws-lambda-powertools/validation/decorator';
|
|
51
|
+
*
|
|
52
|
+
* class Lambda {
|
|
53
|
+
* @validator({
|
|
54
|
+
* inboundSchema: {
|
|
55
|
+
* type: 'number',
|
|
56
|
+
* },
|
|
57
|
+
* envelope: 'nested',
|
|
58
|
+
* })
|
|
59
|
+
* async handler(event: number, _context: Context) {
|
|
60
|
+
* return { result: event * 2 };
|
|
61
|
+
* }
|
|
62
|
+
* }
|
|
63
|
+
*
|
|
64
|
+
* const lambda = new Lambda();
|
|
65
|
+
* export const handler = lambda.handler.bind(lambda);
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* Since the Validation utility is built on top of Ajv, you can also provide custom formats and external references
|
|
69
|
+
* to the validation process. This allows you to extend the validation capabilities of Ajv to suit your specific needs.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
74
|
+
*
|
|
75
|
+
* const formats = {
|
|
76
|
+
* ageRange: (value: number) => return value >= 0 && value <= 120,
|
|
77
|
+
* };
|
|
78
|
+
*
|
|
79
|
+
* const definitionSchema = {
|
|
80
|
+
* $id: 'https://example.com/schemas/definitions.json',
|
|
81
|
+
* definitions: {
|
|
82
|
+
* user: {
|
|
83
|
+
* type: 'object',
|
|
84
|
+
* properties: {
|
|
85
|
+
* name: { type: 'string' },
|
|
86
|
+
* age: { type: 'number', format: 'ageRange' },
|
|
87
|
+
* },
|
|
88
|
+
* required: ['name', 'age'],
|
|
89
|
+
* additionalProperties: false,
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
* } as const;
|
|
93
|
+
*
|
|
94
|
+
* const schema = {
|
|
95
|
+
* $id: 'https://example.com/schemas/user.json',
|
|
96
|
+
* type: 'object',
|
|
97
|
+
* properties: {
|
|
98
|
+
* user: { $ref: 'definitions.json#/definitions/user' },
|
|
99
|
+
* },
|
|
100
|
+
* required: ['user'],
|
|
101
|
+
* additionalProperties: false,
|
|
102
|
+
* } as const;
|
|
103
|
+
*
|
|
104
|
+
* const payload = {
|
|
105
|
+
* user: {
|
|
106
|
+
* name: 'Alice',
|
|
107
|
+
* age: 25,
|
|
108
|
+
* },
|
|
109
|
+
* };
|
|
110
|
+
*
|
|
111
|
+
* class Lambda {
|
|
112
|
+
* @validator({
|
|
113
|
+
* inboundSchema: schema,
|
|
114
|
+
* externalRefs: [definitionSchema],
|
|
115
|
+
* formats,
|
|
116
|
+
* })
|
|
117
|
+
* async handler(event: { value: number }, _context: Context) {
|
|
118
|
+
* // Your handler logic here
|
|
119
|
+
* return { result: event.value * 2 };
|
|
120
|
+
* }
|
|
121
|
+
* }
|
|
122
|
+
*
|
|
123
|
+
* const lambda = new Lambda();
|
|
124
|
+
* export const handler = lambda.handler.bind(lambda);
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* Additionally, you can provide an existing Ajv instance to reuse the same instance across multiple validations. If
|
|
128
|
+
* you don't provide an Ajv instance, a new one will be created for each validation.
|
|
129
|
+
*
|
|
130
|
+
* @param options - The validation options
|
|
131
|
+
* @param options.inboundSchema - The JSON schema for inbound validation.
|
|
132
|
+
* @param options.outboundSchema - The JSON schema for outbound validation.
|
|
133
|
+
* @param options.envelope - Optional JMESPath expression to use as envelope for the payload.
|
|
134
|
+
* @param options.formats - Optional formats for validation.
|
|
135
|
+
* @param options.externalRefs - Optional external references for validation.
|
|
136
|
+
* @param options.ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
137
|
+
*/
|
|
138
|
+
function validator(options) {
|
|
139
|
+
return (_target, _propertyKey, descriptor) => {
|
|
140
|
+
const { inboundSchema, outboundSchema, envelope, formats, externalRefs, ajv, } = options;
|
|
141
|
+
if (!options.inboundSchema && !outboundSchema) {
|
|
142
|
+
return descriptor;
|
|
143
|
+
}
|
|
144
|
+
const originalMethod = descriptor.value;
|
|
145
|
+
descriptor.value = async function (...args) {
|
|
146
|
+
let validatedInput = args[0];
|
|
147
|
+
if (inboundSchema) {
|
|
148
|
+
try {
|
|
149
|
+
validatedInput = validate({
|
|
150
|
+
payload: validatedInput,
|
|
151
|
+
schema: inboundSchema,
|
|
152
|
+
envelope: envelope,
|
|
153
|
+
formats: formats,
|
|
154
|
+
externalRefs: externalRefs,
|
|
155
|
+
ajv: ajv,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
throw new SchemaValidationError('Inbound schema validation failed', {
|
|
160
|
+
cause: getErrorCause(error),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const result = await originalMethod.apply(this, [
|
|
165
|
+
validatedInput,
|
|
166
|
+
...args.slice(1),
|
|
167
|
+
]);
|
|
168
|
+
if (outboundSchema) {
|
|
169
|
+
try {
|
|
170
|
+
return validate({
|
|
171
|
+
payload: result,
|
|
172
|
+
schema: outboundSchema,
|
|
173
|
+
formats: formats,
|
|
174
|
+
externalRefs: externalRefs,
|
|
175
|
+
ajv: ajv,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
throw new SchemaValidationError('Outbound schema validation failed', {
|
|
180
|
+
cause: getErrorCause(error),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
185
|
+
};
|
|
186
|
+
return descriptor;
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
export { validator };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for all validation errors.
|
|
3
|
+
*
|
|
4
|
+
* This error is usually not thrown directly, but it's used as a base class for
|
|
5
|
+
* other errors thrown by the Validation utility. You can use it to catch all
|
|
6
|
+
* validation errors in a single catch block.
|
|
7
|
+
*/
|
|
8
|
+
declare class ValidationError extends Error {
|
|
9
|
+
constructor(message: string, options?: ErrorOptions);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Error thrown when a schema validation fails.
|
|
13
|
+
*
|
|
14
|
+
* This error is thrown when the validation of a payload against a schema fails,
|
|
15
|
+
* the `cause` property contains the original Ajv issues.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
20
|
+
* import { ValidationError } from '@aws-lambda-powertools/validation/errors';
|
|
21
|
+
*
|
|
22
|
+
* const schema = {
|
|
23
|
+
* type: 'number',
|
|
24
|
+
* minimum: 0,
|
|
25
|
+
* maximum: 100,
|
|
26
|
+
* };
|
|
27
|
+
*
|
|
28
|
+
* const payload = -1;
|
|
29
|
+
*
|
|
30
|
+
* try {
|
|
31
|
+
* validate({ payload, schema });
|
|
32
|
+
* } catch (error) {
|
|
33
|
+
* if (error instanceof ValidationError) {
|
|
34
|
+
* // cause includes the original Ajv issues
|
|
35
|
+
* const { message, cause } = error;
|
|
36
|
+
* // ... handle the error
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* // handle other errors
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare class SchemaValidationError extends ValidationError {
|
|
44
|
+
constructor(message: string, options?: ErrorOptions);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Error thrown when a schema compilation fails.
|
|
48
|
+
*
|
|
49
|
+
* This error is thrown when you pass an invalid schema to the validator.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
54
|
+
*
|
|
55
|
+
* const schema = {
|
|
56
|
+
* invalid: 'schema',
|
|
57
|
+
* };
|
|
58
|
+
*
|
|
59
|
+
* try {
|
|
60
|
+
* validate({ payload: {}, schema });
|
|
61
|
+
* } catch (error) {
|
|
62
|
+
* if (error instanceof SchemaCompilationError) {
|
|
63
|
+
* // handle the error
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* // handle other errors
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
declare class SchemaCompilationError extends ValidationError {
|
|
71
|
+
constructor(message: string, options?: ErrorOptions);
|
|
72
|
+
}
|
|
73
|
+
export { ValidationError, SchemaValidationError, SchemaCompilationError };
|
|
74
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAM,eAAgB,SAAQ,KAAK;gBACrB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,cAAM,qBAAsB,SAAQ,eAAe;gBACrC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,cAAM,sBAAuB,SAAQ,eAAe;gBACtC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD;AAED,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for all validation errors.
|
|
3
|
+
*
|
|
4
|
+
* This error is usually not thrown directly, but it's used as a base class for
|
|
5
|
+
* other errors thrown by the Validation utility. You can use it to catch all
|
|
6
|
+
* validation errors in a single catch block.
|
|
7
|
+
*/
|
|
8
|
+
class ValidationError extends Error {
|
|
9
|
+
constructor(message, options) {
|
|
10
|
+
super(message, options);
|
|
11
|
+
this.name = 'ValidationError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Error thrown when a schema validation fails.
|
|
16
|
+
*
|
|
17
|
+
* This error is thrown when the validation of a payload against a schema fails,
|
|
18
|
+
* the `cause` property contains the original Ajv issues.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
23
|
+
* import { ValidationError } from '@aws-lambda-powertools/validation/errors';
|
|
24
|
+
*
|
|
25
|
+
* const schema = {
|
|
26
|
+
* type: 'number',
|
|
27
|
+
* minimum: 0,
|
|
28
|
+
* maximum: 100,
|
|
29
|
+
* };
|
|
30
|
+
*
|
|
31
|
+
* const payload = -1;
|
|
32
|
+
*
|
|
33
|
+
* try {
|
|
34
|
+
* validate({ payload, schema });
|
|
35
|
+
* } catch (error) {
|
|
36
|
+
* if (error instanceof ValidationError) {
|
|
37
|
+
* // cause includes the original Ajv issues
|
|
38
|
+
* const { message, cause } = error;
|
|
39
|
+
* // ... handle the error
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* // handle other errors
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
class SchemaValidationError extends ValidationError {
|
|
47
|
+
constructor(message, options) {
|
|
48
|
+
super(message, options);
|
|
49
|
+
this.name = 'SchemaValidationError';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Error thrown when a schema compilation fails.
|
|
54
|
+
*
|
|
55
|
+
* This error is thrown when you pass an invalid schema to the validator.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
60
|
+
*
|
|
61
|
+
* const schema = {
|
|
62
|
+
* invalid: 'schema',
|
|
63
|
+
* };
|
|
64
|
+
*
|
|
65
|
+
* try {
|
|
66
|
+
* validate({ payload: {}, schema });
|
|
67
|
+
* } catch (error) {
|
|
68
|
+
* if (error instanceof SchemaCompilationError) {
|
|
69
|
+
* // handle the error
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* // handle other errors
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
class SchemaCompilationError extends ValidationError {
|
|
77
|
+
constructor(message, options) {
|
|
78
|
+
super(message, options);
|
|
79
|
+
this.name = 'SchemaCompilationError';
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export { ValidationError, SchemaValidationError, SchemaCompilationError };
|