@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,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,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SchemaCompilationError = exports.SchemaValidationError = exports.ValidationError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Base error class for all validation errors.
|
|
6
|
+
*
|
|
7
|
+
* This error is usually not thrown directly, but it's used as a base class for
|
|
8
|
+
* other errors thrown by the Validation utility. You can use it to catch all
|
|
9
|
+
* validation errors in a single catch block.
|
|
10
|
+
*/
|
|
11
|
+
class ValidationError extends Error {
|
|
12
|
+
constructor(message, options) {
|
|
13
|
+
super(message, options);
|
|
14
|
+
this.name = 'ValidationError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.ValidationError = ValidationError;
|
|
18
|
+
/**
|
|
19
|
+
* Error thrown when a schema validation fails.
|
|
20
|
+
*
|
|
21
|
+
* This error is thrown when the validation of a payload against a schema fails,
|
|
22
|
+
* the `cause` property contains the original Ajv issues.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
27
|
+
* import { ValidationError } from '@aws-lambda-powertools/validation/errors';
|
|
28
|
+
*
|
|
29
|
+
* const schema = {
|
|
30
|
+
* type: 'number',
|
|
31
|
+
* minimum: 0,
|
|
32
|
+
* maximum: 100,
|
|
33
|
+
* };
|
|
34
|
+
*
|
|
35
|
+
* const payload = -1;
|
|
36
|
+
*
|
|
37
|
+
* try {
|
|
38
|
+
* validate({ payload, schema });
|
|
39
|
+
* } catch (error) {
|
|
40
|
+
* if (error instanceof ValidationError) {
|
|
41
|
+
* // cause includes the original Ajv issues
|
|
42
|
+
* const { message, cause } = error;
|
|
43
|
+
* // ... handle the error
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* // handle other errors
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
class SchemaValidationError extends ValidationError {
|
|
51
|
+
constructor(message, options) {
|
|
52
|
+
super(message, options);
|
|
53
|
+
this.name = 'SchemaValidationError';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.SchemaValidationError = SchemaValidationError;
|
|
57
|
+
/**
|
|
58
|
+
* Error thrown when a schema compilation fails.
|
|
59
|
+
*
|
|
60
|
+
* This error is thrown when you pass an invalid schema to the validator.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
65
|
+
*
|
|
66
|
+
* const schema = {
|
|
67
|
+
* invalid: 'schema',
|
|
68
|
+
* };
|
|
69
|
+
*
|
|
70
|
+
* try {
|
|
71
|
+
* validate({ payload: {}, schema });
|
|
72
|
+
* } catch (error) {
|
|
73
|
+
* if (error instanceof SchemaCompilationError) {
|
|
74
|
+
* // handle the error
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* // handle other errors
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
class SchemaCompilationError extends ValidationError {
|
|
82
|
+
constructor(message, options) {
|
|
83
|
+
super(message, options);
|
|
84
|
+
this.name = 'SchemaCompilationError';
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.SchemaCompilationError = SchemaCompilationError;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { MiddlewareFn, MiddyLikeRequest } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { ValidatorOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Middy.js middleware to validate your event and response payloads using JSON schema.
|
|
5
|
+
*
|
|
6
|
+
* Both inbound and outbound schemas are optional. If only one is provided, only that one will be validated.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { validation } from '@aws-lambda-powertools/validation/middleware';
|
|
11
|
+
* import middy from '@middy/core';
|
|
12
|
+
*
|
|
13
|
+
* const inboundSchema = {
|
|
14
|
+
* type: 'object',
|
|
15
|
+
* properties: {
|
|
16
|
+
* foo: { type: 'string' },
|
|
17
|
+
* },
|
|
18
|
+
* required: ['foo'],
|
|
19
|
+
* additionalProperties: false,
|
|
20
|
+
* };
|
|
21
|
+
*
|
|
22
|
+
* const outboundSchema = {
|
|
23
|
+
* type: 'object',
|
|
24
|
+
* properties: {
|
|
25
|
+
* bar: { type: 'number' },
|
|
26
|
+
* },
|
|
27
|
+
* required: ['bar'],
|
|
28
|
+
* additionalProperties: false,
|
|
29
|
+
* };
|
|
30
|
+
*
|
|
31
|
+
* export const handler = middy()
|
|
32
|
+
* .use(validation({ inboundSchema, outboundSchema }))
|
|
33
|
+
* .handler(async (event) => {
|
|
34
|
+
* // Your handler logic here
|
|
35
|
+
* return { bar: 42 };
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Since the Validation utility is built on top of Ajv, you can also provide custom formats and external references
|
|
40
|
+
* to the validation process. This allows you to extend the validation capabilities of Ajv to suit your specific needs.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { validator } from '@aws-lambda-powertools/validation/middleware';
|
|
45
|
+
* import middy from '@middy/core';
|
|
46
|
+
*
|
|
47
|
+
* const formats = {
|
|
48
|
+
* ageRange: (value: number) => return value >= 0 && value <= 120,
|
|
49
|
+
* };
|
|
50
|
+
*
|
|
51
|
+
* const definitionSchema = {
|
|
52
|
+
* $id: 'https://example.com/schemas/definitions.json',
|
|
53
|
+
* definitions: {
|
|
54
|
+
* user: {
|
|
55
|
+
* type: 'object',
|
|
56
|
+
* properties: {
|
|
57
|
+
* name: { type: 'string' },
|
|
58
|
+
* age: { type: 'number', format: 'ageRange' },
|
|
59
|
+
* },
|
|
60
|
+
* required: ['name', 'age'],
|
|
61
|
+
* additionalProperties: false,
|
|
62
|
+
* }
|
|
63
|
+
* }
|
|
64
|
+
* } as const;
|
|
65
|
+
*
|
|
66
|
+
* const schema = {
|
|
67
|
+
* $id: 'https://example.com/schemas/user.json',
|
|
68
|
+
* type: 'object',
|
|
69
|
+
* properties: {
|
|
70
|
+
* user: { $ref: 'definitions.json#/definitions/user' },
|
|
71
|
+
* },
|
|
72
|
+
* required: ['user'],
|
|
73
|
+
* additionalProperties: false,
|
|
74
|
+
* } as const;
|
|
75
|
+
*
|
|
76
|
+
* export const handler = middy()
|
|
77
|
+
* .use(validation({
|
|
78
|
+
* inboundSchema,
|
|
79
|
+
* outboundSchema,
|
|
80
|
+
* externalRefs: [definitionSchema],
|
|
81
|
+
* formats,
|
|
82
|
+
* }))
|
|
83
|
+
* .handler(async (event) => {
|
|
84
|
+
* // Your handler logic here
|
|
85
|
+
* return { bar: 42 };
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* Additionally, you can provide an existing Ajv instance to reuse the same instance across multiple validations. If
|
|
90
|
+
* you don't provide an Ajv instance, a new one will be created for each validation.
|
|
91
|
+
*
|
|
92
|
+
* @param options - The validation options
|
|
93
|
+
* @param options.inboundSchema - The JSON schema for inbound validation.
|
|
94
|
+
* @param options.outboundSchema - The JSON schema for outbound validation.
|
|
95
|
+
* @param options.envelope - Optional JMESPath expression to use as envelope for the payload.
|
|
96
|
+
* @param options.formats - Optional formats for validation.
|
|
97
|
+
* @param options.externalRefs - Optional external references for validation.
|
|
98
|
+
* @param options.ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
99
|
+
*/
|
|
100
|
+
declare const validator: (options: ValidatorOptions) => {
|
|
101
|
+
before: MiddlewareFn;
|
|
102
|
+
after: (handler: MiddyLikeRequest) => Promise<void>;
|
|
103
|
+
};
|
|
104
|
+
export { validator };
|
|
105
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EACjB,MAAM,sCAAsC,CAAC;AAE9C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAInD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgGG;AACH,QAAA,MAAM,SAAS,GAAI,SAAS,gBAAgB;;qBAqBZ,gBAAgB;CAsB/C,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validator = void 0;
|
|
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
|
+
* Middy.js middleware to validate your event and response payloads using JSON schema.
|
|
9
|
+
*
|
|
10
|
+
* Both inbound and outbound schemas are optional. If only one is provided, only that one will be validated.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { validation } from '@aws-lambda-powertools/validation/middleware';
|
|
15
|
+
* import middy from '@middy/core';
|
|
16
|
+
*
|
|
17
|
+
* const inboundSchema = {
|
|
18
|
+
* type: 'object',
|
|
19
|
+
* properties: {
|
|
20
|
+
* foo: { type: 'string' },
|
|
21
|
+
* },
|
|
22
|
+
* required: ['foo'],
|
|
23
|
+
* additionalProperties: false,
|
|
24
|
+
* };
|
|
25
|
+
*
|
|
26
|
+
* const outboundSchema = {
|
|
27
|
+
* type: 'object',
|
|
28
|
+
* properties: {
|
|
29
|
+
* bar: { type: 'number' },
|
|
30
|
+
* },
|
|
31
|
+
* required: ['bar'],
|
|
32
|
+
* additionalProperties: false,
|
|
33
|
+
* };
|
|
34
|
+
*
|
|
35
|
+
* export const handler = middy()
|
|
36
|
+
* .use(validation({ inboundSchema, outboundSchema }))
|
|
37
|
+
* .handler(async (event) => {
|
|
38
|
+
* // Your handler logic here
|
|
39
|
+
* return { bar: 42 };
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* Since the Validation utility is built on top of Ajv, you can also provide custom formats and external references
|
|
44
|
+
* to the validation process. This allows you to extend the validation capabilities of Ajv to suit your specific needs.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* import { validator } from '@aws-lambda-powertools/validation/middleware';
|
|
49
|
+
* import middy from '@middy/core';
|
|
50
|
+
*
|
|
51
|
+
* const formats = {
|
|
52
|
+
* ageRange: (value: number) => return value >= 0 && value <= 120,
|
|
53
|
+
* };
|
|
54
|
+
*
|
|
55
|
+
* const definitionSchema = {
|
|
56
|
+
* $id: 'https://example.com/schemas/definitions.json',
|
|
57
|
+
* definitions: {
|
|
58
|
+
* user: {
|
|
59
|
+
* type: 'object',
|
|
60
|
+
* properties: {
|
|
61
|
+
* name: { type: 'string' },
|
|
62
|
+
* age: { type: 'number', format: 'ageRange' },
|
|
63
|
+
* },
|
|
64
|
+
* required: ['name', 'age'],
|
|
65
|
+
* additionalProperties: false,
|
|
66
|
+
* }
|
|
67
|
+
* }
|
|
68
|
+
* } as const;
|
|
69
|
+
*
|
|
70
|
+
* const schema = {
|
|
71
|
+
* $id: 'https://example.com/schemas/user.json',
|
|
72
|
+
* type: 'object',
|
|
73
|
+
* properties: {
|
|
74
|
+
* user: { $ref: 'definitions.json#/definitions/user' },
|
|
75
|
+
* },
|
|
76
|
+
* required: ['user'],
|
|
77
|
+
* additionalProperties: false,
|
|
78
|
+
* } as const;
|
|
79
|
+
*
|
|
80
|
+
* export const handler = middy()
|
|
81
|
+
* .use(validation({
|
|
82
|
+
* inboundSchema,
|
|
83
|
+
* outboundSchema,
|
|
84
|
+
* externalRefs: [definitionSchema],
|
|
85
|
+
* formats,
|
|
86
|
+
* }))
|
|
87
|
+
* .handler(async (event) => {
|
|
88
|
+
* // Your handler logic here
|
|
89
|
+
* return { bar: 42 };
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* Additionally, you can provide an existing Ajv instance to reuse the same instance across multiple validations. If
|
|
94
|
+
* you don't provide an Ajv instance, a new one will be created for each validation.
|
|
95
|
+
*
|
|
96
|
+
* @param options - The validation options
|
|
97
|
+
* @param options.inboundSchema - The JSON schema for inbound validation.
|
|
98
|
+
* @param options.outboundSchema - The JSON schema for outbound validation.
|
|
99
|
+
* @param options.envelope - Optional JMESPath expression to use as envelope for the payload.
|
|
100
|
+
* @param options.formats - Optional formats for validation.
|
|
101
|
+
* @param options.externalRefs - Optional external references for validation.
|
|
102
|
+
* @param options.ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
103
|
+
*/
|
|
104
|
+
const validator = (options) => {
|
|
105
|
+
const before = async (request) => {
|
|
106
|
+
if (options.inboundSchema) {
|
|
107
|
+
const originalEvent = structuredClone(request.event);
|
|
108
|
+
try {
|
|
109
|
+
request.event = (0, validate_js_1.validate)({
|
|
110
|
+
payload: originalEvent,
|
|
111
|
+
schema: options.inboundSchema,
|
|
112
|
+
envelope: options.envelope,
|
|
113
|
+
formats: options.formats,
|
|
114
|
+
externalRefs: options.externalRefs,
|
|
115
|
+
ajv: options.ajv,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
throw new errors_js_1.SchemaValidationError('Inbound schema validation failed', {
|
|
120
|
+
cause: (0, utils_js_1.getErrorCause)(error),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const after = async (handler) => {
|
|
126
|
+
if (options.outboundSchema) {
|
|
127
|
+
try {
|
|
128
|
+
handler.response = (0, validate_js_1.validate)({
|
|
129
|
+
payload: handler.response,
|
|
130
|
+
schema: options.outboundSchema,
|
|
131
|
+
formats: options.formats,
|
|
132
|
+
externalRefs: options.externalRefs,
|
|
133
|
+
ajv: options.ajv,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
throw new errors_js_1.SchemaValidationError('Outbound schema validation failed', {
|
|
138
|
+
cause: (0, utils_js_1.getErrorCause)(error),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
return {
|
|
144
|
+
before,
|
|
145
|
+
after,
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
exports.validator = validator;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Ajv, AnySchema, Format } from 'ajv';
|
|
2
|
+
/**
|
|
3
|
+
* Options to customize the JSON Schema validation.
|
|
4
|
+
*
|
|
5
|
+
* @param payload - The data to validate.
|
|
6
|
+
* @param schema - The JSON schema for validation.
|
|
7
|
+
* @param envelope - Optional JMESPATH expression to use as envelope for the payload.
|
|
8
|
+
* @param formats - Optional formats for validation.
|
|
9
|
+
* @param externalRefs - Optional external references for validation.
|
|
10
|
+
* @param ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
11
|
+
*/
|
|
12
|
+
type ValidateParams = {
|
|
13
|
+
/**
|
|
14
|
+
* The data to validate.
|
|
15
|
+
*/
|
|
16
|
+
payload: unknown;
|
|
17
|
+
/**
|
|
18
|
+
* The JSON schema for validation.
|
|
19
|
+
*/
|
|
20
|
+
schema: AnySchema;
|
|
21
|
+
/**
|
|
22
|
+
* Optional JMESPATH expression to use as envelope for the payload.
|
|
23
|
+
*/
|
|
24
|
+
envelope?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Optional formats for validation.
|
|
27
|
+
*/
|
|
28
|
+
formats?: Record<string, Format>;
|
|
29
|
+
/**
|
|
30
|
+
* Optional external references for validation.
|
|
31
|
+
*/
|
|
32
|
+
externalRefs?: AnySchema | AnySchema[];
|
|
33
|
+
/**
|
|
34
|
+
* Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
35
|
+
*/
|
|
36
|
+
ajv?: Ajv;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Options to customize the JSON Schema validation.
|
|
40
|
+
*
|
|
41
|
+
* @param inboundSchema - The JSON schema for inbound validation.
|
|
42
|
+
* @param outboundSchema - The JSON schema for outbound validation.
|
|
43
|
+
* @param envelope - Optional JMESPATH expression to use as envelope for the payload.
|
|
44
|
+
* @param formats - Optional formats for validation.
|
|
45
|
+
* @param externalRefs - Optional external references for validation.
|
|
46
|
+
* @param ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
47
|
+
*/
|
|
48
|
+
interface ValidatorOptions extends Omit<ValidateParams, 'payload' | 'schema'> {
|
|
49
|
+
/**
|
|
50
|
+
* The JSON schema for inbound validation.
|
|
51
|
+
*/
|
|
52
|
+
inboundSchema?: AnySchema;
|
|
53
|
+
/**
|
|
54
|
+
* The JSON schema for outbound validation.
|
|
55
|
+
*/
|
|
56
|
+
outboundSchema?: AnySchema;
|
|
57
|
+
}
|
|
58
|
+
export type { ValidateParams, ValidatorOptions };
|
|
59
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC;AAMlD;;;;;;;;;GASG;AACH,KAAK,cAAc,GAAG;IACpB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,MAAM,EAAE,SAAS,CAAC;IAClB;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC;;OAEG;IACH,YAAY,CAAC,EAAE,SAAS,GAAG,SAAS,EAAE,CAAC;IACvC;;OAEG;IACH,GAAG,CAAC,EAAE,GAAG,CAAC;CACX,CAAC;AAEF;;;;;;;;;GASG;AACH,UAAU,gBAAiB,SAAQ,IAAI,CAAC,cAAc,EAAE,SAAS,GAAG,QAAQ,CAAC;IAC3E;;OAEG;IACH,aAAa,CAAC,EAAE,SAAS,CAAC;IAC1B;;OAEG;IACH,cAAc,CAAC,EAAE,SAAS,CAAC;CAC5B;AAED,YAAY,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAC"}
|
package/lib/cjs/types.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the original cause of the error if it is a `SchemaValidationError`.
|
|
3
|
+
*
|
|
4
|
+
* This is useful so that we don't rethrow the same error type.
|
|
5
|
+
*
|
|
6
|
+
* @param error - The error to extract the cause from.
|
|
7
|
+
*/
|
|
8
|
+
declare const getErrorCause: (error: unknown) => unknown;
|
|
9
|
+
export { getErrorCause };
|
|
10
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,QAAA,MAAM,aAAa,GAAI,OAAO,OAAO,KAAG,OAMvC,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
package/lib/cjs/utils.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getErrorCause = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Get the original cause of the error if it is a `SchemaValidationError`.
|
|
6
|
+
*
|
|
7
|
+
* This is useful so that we don't rethrow the same error type.
|
|
8
|
+
*
|
|
9
|
+
* @param error - The error to extract the cause from.
|
|
10
|
+
*/
|
|
11
|
+
const getErrorCause = (error) => {
|
|
12
|
+
let cause = error;
|
|
13
|
+
if (error instanceof Error && error.name === 'SchemaValidationError') {
|
|
14
|
+
cause = error.cause;
|
|
15
|
+
}
|
|
16
|
+
return cause;
|
|
17
|
+
};
|
|
18
|
+
exports.getErrorCause = getErrorCause;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { ValidateParams } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates a payload against a JSON schema using Ajv.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
8
|
+
*
|
|
9
|
+
* const schema = {
|
|
10
|
+
* type: 'object',
|
|
11
|
+
* properties: {
|
|
12
|
+
* name: { type: 'string' },
|
|
13
|
+
* age: { type: 'number' },
|
|
14
|
+
* },
|
|
15
|
+
* required: ['name', 'age'],
|
|
16
|
+
* additionalProperties: false,
|
|
17
|
+
* } as const;
|
|
18
|
+
*
|
|
19
|
+
* const payload = { name: 'John', age: 30 };
|
|
20
|
+
*
|
|
21
|
+
* const validatedData = validate({
|
|
22
|
+
* payload,
|
|
23
|
+
* schema,
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* When validating, you can also provide an optional JMESPath expression to extract a specific part of the payload
|
|
28
|
+
* before validation using the `envelope` parameter. This is useful when the payload is nested or when you want to
|
|
29
|
+
* validate only a specific part of it.
|
|
30
|
+
*
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
33
|
+
*
|
|
34
|
+
* const schema = {
|
|
35
|
+
* type: 'object',
|
|
36
|
+
* properties: {
|
|
37
|
+
* user: { type: 'string' },
|
|
38
|
+
* },
|
|
39
|
+
* required: ['user'],
|
|
40
|
+
* additionalProperties: false,
|
|
41
|
+
* } as const;
|
|
42
|
+
*
|
|
43
|
+
* const payload = {
|
|
44
|
+
* data: {
|
|
45
|
+
* user: 'Alice',
|
|
46
|
+
* },
|
|
47
|
+
* };
|
|
48
|
+
*
|
|
49
|
+
* const validatedData = validate({
|
|
50
|
+
* payload,
|
|
51
|
+
* schema,
|
|
52
|
+
* envelope: 'data',
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* Since the Validation utility is built on top of Ajv, you can also provide custom formats and external references
|
|
57
|
+
* to the validation process. This allows you to extend the validation capabilities of Ajv to suit your specific needs.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import { validate } from '@aws-lambda-powertools/validation';
|
|
62
|
+
*
|
|
63
|
+
* const formats = {
|
|
64
|
+
* ageRange: (value: number) => return value >= 0 && value <= 120,
|
|
65
|
+
* };
|
|
66
|
+
*
|
|
67
|
+
* const definitionSchema = {
|
|
68
|
+
* $id: 'https://example.com/schemas/definitions.json',
|
|
69
|
+
* definitions: {
|
|
70
|
+
* user: {
|
|
71
|
+
* type: 'object',
|
|
72
|
+
* properties: {
|
|
73
|
+
* name: { type: 'string' },
|
|
74
|
+
* age: { type: 'number', format: 'ageRange' },
|
|
75
|
+
* },
|
|
76
|
+
* required: ['name', 'age'],
|
|
77
|
+
* additionalProperties: false,
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
* } as const;
|
|
81
|
+
*
|
|
82
|
+
* const schema = {
|
|
83
|
+
* $id: 'https://example.com/schemas/user.json',
|
|
84
|
+
* type: 'object',
|
|
85
|
+
* properties: {
|
|
86
|
+
* user: { $ref: 'definitions.json#/definitions/user' },
|
|
87
|
+
* },
|
|
88
|
+
* required: ['user'],
|
|
89
|
+
* additionalProperties: false,
|
|
90
|
+
* } as const;
|
|
91
|
+
*
|
|
92
|
+
* const payload = {
|
|
93
|
+
* user: {
|
|
94
|
+
* name: 'Alice',
|
|
95
|
+
* age: 25,
|
|
96
|
+
* },
|
|
97
|
+
* };
|
|
98
|
+
*
|
|
99
|
+
* const validatedData = validate({
|
|
100
|
+
* payload,
|
|
101
|
+
* schema,
|
|
102
|
+
* externalRefs: [definitionSchema],
|
|
103
|
+
* formats,
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* Additionally, you can provide an existing Ajv instance to reuse the same instance across multiple validations. If
|
|
108
|
+
* you don't provide an Ajv instance, a new one will be created for each validation.
|
|
109
|
+
*
|
|
110
|
+
* @param params.payload - The payload to validate.
|
|
111
|
+
* @param params.schema - The JSON schema to validate against.
|
|
112
|
+
* @param params.envelope - Optional JMESPath expression to use as envelope for the payload.
|
|
113
|
+
* @param params.formats - Optional formats for validation.
|
|
114
|
+
* @param params.externalRefs - Optional external references for validation.
|
|
115
|
+
* @param params.ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
|
|
116
|
+
*/
|
|
117
|
+
declare const validate: <T = unknown>(params: ValidateParams) => T;
|
|
118
|
+
export { validate };
|
|
119
|
+
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../src/validate.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkHG;AACH,QAAA,MAAM,QAAQ,GAAI,CAAC,GAAG,OAAO,EAAE,QAAQ,cAAc,KAAG,CAoCvD,CAAC;AAEF,OAAO,EAAE,QAAQ,EAAE,CAAC"}
|