Skip to content

Commit 4f8b15a

Browse files
authored
docs(validation): add main docs page (#3717)
1 parent 16208c2 commit 4f8b15a

16 files changed

+410
-531
lines changed

docs/utilities/validation.md

Lines changed: 51 additions & 531 deletions
Large diffs are not rendered by default.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { validate } from '@aws-lambda-powertools/validation';
3+
import { SchemaValidationError } from '@aws-lambda-powertools/validation/errors';
4+
import Ajv2019 from 'ajv/dist/2019';
5+
import { inboundSchema } from './schemas.js';
6+
7+
const logger = new Logger();
8+
9+
const ajv = new Ajv2019();
10+
11+
export const handler = async (event: unknown) => {
12+
try {
13+
await validate({
14+
payload: event,
15+
schema: inboundSchema,
16+
ajv, // (1)!
17+
});
18+
19+
return {
20+
message: 'ok',
21+
};
22+
} catch (error) {
23+
if (error instanceof SchemaValidationError) {
24+
logger.error('Schema validation failed', error);
25+
throw new Error('Invalid event payload');
26+
}
27+
28+
throw error;
29+
}
30+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { validate } from '@aws-lambda-powertools/validation';
3+
import { SchemaValidationError } from '@aws-lambda-powertools/validation/errors';
4+
import schemaWithCustomFormat from './samples/schemaWithCustomFormat.json';
5+
6+
const logger = new Logger();
7+
8+
const customFormats = {
9+
awsaccountid: /^\d{12}$/,
10+
creditcard: (value: string) => {
11+
// Luhn algorithm (for demonstration purposes only - do not use in production)
12+
const sum = value
13+
.split('')
14+
.reverse()
15+
.reduce((acc, digit, index) => {
16+
const num = Number.parseInt(digit, 10);
17+
return acc + (index % 2 === 0 ? num : num < 5 ? num * 2 : num * 2 - 9);
18+
}, 0);
19+
20+
return sum % 10 === 0;
21+
},
22+
};
23+
24+
export const handler = async (event: unknown) => {
25+
try {
26+
await validate({
27+
payload: event,
28+
schema: schemaWithCustomFormat,
29+
formats: customFormats,
30+
});
31+
32+
return {
33+
message: 'ok',
34+
};
35+
} catch (error) {
36+
if (error instanceof SchemaValidationError) {
37+
logger.error('Schema validation failed', error);
38+
throw new Error('Invalid event payload');
39+
}
40+
41+
throw error;
42+
}
43+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { validator } from '@aws-lambda-powertools/validation/decorator';
2+
import type { Context } from 'aws-lambda';
3+
import {
4+
type InboundSchema,
5+
defsSchema,
6+
inboundSchema,
7+
outboundSchema,
8+
} from './schemasWithExternalRefs.js';
9+
10+
class Lambda {
11+
@validator({
12+
inboundSchema,
13+
outboundSchema,
14+
externalRefs: [defsSchema],
15+
})
16+
async handler(event: InboundSchema, _context: Context) {
17+
return {
18+
message: `processed ${event.userId}`,
19+
success: true,
20+
};
21+
}
22+
}
23+
24+
const lambda = new Lambda();
25+
export const handler = lambda.handler.bind(lambda);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { validator } from '@aws-lambda-powertools/validation/decorator';
2+
import type { Context } from 'aws-lambda';
3+
import {
4+
type InboundSchema,
5+
type OutboundSchema,
6+
inboundSchema,
7+
outboundSchema,
8+
} from './schemas.js';
9+
10+
class Lambda {
11+
@validator({
12+
inboundSchema,
13+
outboundSchema,
14+
})
15+
async handler(
16+
event: InboundSchema,
17+
_context: Context
18+
): Promise<OutboundSchema> {
19+
return {
20+
statusCode: 200,
21+
body: `Hello from ${event.userId}`,
22+
};
23+
}
24+
}
25+
26+
const lambda = new Lambda();
27+
export const handler = lambda.handler.bind(lambda);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { validator } from '@aws-lambda-powertools/validation/decorator';
2+
import type { Context } from 'aws-lambda';
3+
import { type InboundSchema, inboundSchema } from './schemas.js';
4+
5+
class Lambda {
6+
@validator({
7+
inboundSchema,
8+
envelope: 'detail',
9+
})
10+
async handler(event: InboundSchema, context: Context) {
11+
return {
12+
message: `processed ${event.userId}`,
13+
success: true,
14+
};
15+
}
16+
}
17+
18+
const lambda = new Lambda();
19+
export const handler = lambda.handler.bind(lambda);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { SQS } from '@aws-lambda-powertools/jmespath/envelopes';
2+
import { Logger } from '@aws-lambda-powertools/logger';
3+
import { validator } from '@aws-lambda-powertools/validation/middleware';
4+
import middy from '@middy/core';
5+
import { type InboundSchema, inboundSchema } from './schemas.js';
6+
7+
const logger = new Logger();
8+
9+
export const handler = middy()
10+
.use(
11+
validator({
12+
inboundSchema,
13+
envelope: SQS,
14+
})
15+
)
16+
.handler(async (event: Array<InboundSchema>) => {
17+
for (const record of event) {
18+
logger.info(`Processing message ${record.userId}`);
19+
}
20+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { validator } from '@aws-lambda-powertools/validation/middleware';
2+
import middy from '@middy/core';
3+
import {
4+
type InboundSchema,
5+
type OutboundSchema,
6+
inboundSchema,
7+
outboundSchema,
8+
} from './schemas.js';
9+
10+
export const handler = middy()
11+
.use(
12+
validator({
13+
inboundSchema,
14+
outboundSchema,
15+
})
16+
)
17+
.handler(
18+
async (event: InboundSchema): Promise<OutboundSchema> => ({
19+
statusCode: 200,
20+
body: `Hello from ${event.userId}`,
21+
})
22+
);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { validate } from '@aws-lambda-powertools/validation';
3+
import { SchemaValidationError } from '@aws-lambda-powertools/validation/errors';
4+
import { type InboundSchema, inboundSchema } from './schemas.js';
5+
6+
const logger = new Logger();
7+
8+
export const handler = async (event: InboundSchema) => {
9+
try {
10+
validate({
11+
payload: event,
12+
schema: inboundSchema,
13+
});
14+
15+
return {
16+
message: 'ok', // (1)!
17+
};
18+
} catch (error) {
19+
if (error instanceof SchemaValidationError) {
20+
logger.error('Schema validation failed', error);
21+
throw new Error('Invalid event payload');
22+
}
23+
24+
throw error;
25+
}
26+
};

0 commit comments

Comments
 (0)