Repository navigation
Expand file tree
/
Copy pathvalidateInput.spec.ts
More file actions
81 lines (78 loc) · 2.05 KB
/
Copy pathvalidateInput.spec.ts
File metadata and controls
81 lines (78 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import middy from '@middy/core'
import { Type } from '@sinclair/typebox'
import type {
APIGatewayProxyEvent,
APIGatewayProxyEventV2,
Context,
} from 'aws-lambda'
import assert from 'node:assert'
import { describe, it } from 'node:test'
import {
validateInput,
ValidationFailedError,
type ValidInput,
} from './validateInput.ts'
void describe('validateInput()', () => {
void it('should validate the default input from path, query, and body', async () => {
const Schema = Type.Object({
id: Type.String(),
filter: Type.String(),
value: Type.Boolean(),
})
assert.deepEqual(
await middy()
.use(validateInput(Schema))
.handler(
async (_, context: ValidInput<typeof Schema> & Context) =>
context.decodedInput,
)(
{
headers: { 'Content-Type': 'application/json' },
pathParameters: { id: '42' },
queryStringParameters: { filter: 'foo' },
body: JSON.stringify({ value: true }),
} as unknown as APIGatewayProxyEventV2,
{} as never, // middy augments the Context
),
{ id: '42', filter: 'foo', value: true },
)
})
void it('should pass the event to mapInput()', async () => {
const Schema = Type.Object({
method: Type.Literal('DELETE'),
})
assert.deepEqual(
await middy()
.use(
validateInput(Schema, (event: APIGatewayProxyEvent) => ({
method: event.httpMethod,
})),
)
.handler(
async (_, context: ValidInput<typeof Schema> & Context) =>
context.decodedInput,
)(
{
headers: {},
httpMethod: 'DELETE',
} as unknown as APIGatewayProxyEvent,
{} as never, // middy augments the Context
),
{ method: 'DELETE' },
)
})
void it('should throw an Error in case the input is invalid', async () =>
assert.rejects(
async () =>
middy()
.use(validateInput(Type.Object({ id: Type.Number() })))
.handler(async () => undefined)(
{
headers: {},
pathParameters: { id: 'not-a-number' },
} as unknown as APIGatewayProxyEventV2,
{} as never, // middy augments the Context
),
ValidationFailedError,
))
})