Skip to content

Commit 22404fc

Browse files
committed
add tests to verify missing properties in the schema
1 parent 64285f3 commit 22404fc

16 files changed

+351
-17
lines changed

Diff for: packages/parser/tests/unit/schema/alb.test.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @group unit/parser/schema/
55
*/
66
import { AlbMultiValueHeadersSchema, AlbSchema } from '../../../src/schemas/';
7-
import { TestEvents } from './utils.js';
7+
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';
88

99
describe('ALB ', () => {
1010
it('should parse alb event', () => {
@@ -24,4 +24,19 @@ describe('ALB ', () => {
2424
albMultiValueHeadersEvent
2525
);
2626
});
27+
28+
describe('should detect missing properties in schema for ', () => {
29+
it('alb event', () => {
30+
const albEvent = TestEvents.albEvent;
31+
const strictSchema = AlbSchema.strict();
32+
expect(() => strictSchema.parse(albEvent)).not.toThrow();
33+
});
34+
it('alb event with multi value headers', () => {
35+
const albMultiValueHeadersEvent = TestEvents.albMultiValueHeadersEvent;
36+
const strictSchema = makeSchemaStrictForTesting(
37+
AlbMultiValueHeadersSchema
38+
);
39+
expect(() => strictSchema.parse(albMultiValueHeadersEvent)).not.toThrow();
40+
});
41+
});
2742
});

Diff for: packages/parser/tests/unit/schema/apigw.test.ts

+52-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
APIGatewayRequestAuthorizerEventSchema,
99
APIGatewayTokenAuthorizerEventSchema,
1010
} from '../../../src/schemas/index.js';
11-
import { getTestEvent } from './utils.js';
11+
import { getTestEvent, makeSchemaStrictForTesting } from './utils.js';
1212

1313
describe('API Gateway REST Schemas', () => {
1414
const eventsPath = 'apigw-rest';
@@ -150,4 +150,55 @@ describe('API Gateway REST Schemas', () => {
150150
expect(parsedEvent).toEqual(event);
151151
});
152152
});
153+
154+
describe('should detect missing properties in schema for ', () => {
155+
it.each([
156+
'console-test-ui',
157+
'iam-auth',
158+
'jwt-authorizer-auth',
159+
'lambda-authorizer-auth',
160+
'no-auth',
161+
'websocket',
162+
])(' %p example event', (filename) => {
163+
// Prepare
164+
const event = getTestEvent({ eventsPath, filename: filename });
165+
166+
const strictSchema = makeSchemaStrictForTesting(
167+
APIGatewayProxyEventSchema
168+
);
169+
170+
// Act & Assess
171+
expect(() => strictSchema.parse(event)).not.toThrow();
172+
});
173+
174+
it('authorizer-request example event', () => {
175+
// Prepare
176+
const event = getTestEvent({
177+
eventsPath,
178+
filename: 'authorizer-request',
179+
});
180+
181+
const strictSchema = makeSchemaStrictForTesting(
182+
APIGatewayRequestAuthorizerEventSchema
183+
);
184+
185+
// Act & Assess
186+
expect(() => strictSchema.parse(event)).not.toThrow();
187+
});
188+
189+
it('authorizer-token example event', () => {
190+
// Prepare
191+
const event = getTestEvent({
192+
eventsPath,
193+
filename: 'authorizer-token',
194+
});
195+
196+
const strictSchema = makeSchemaStrictForTesting(
197+
APIGatewayTokenAuthorizerEventSchema
198+
);
199+
200+
// Act & Assess
201+
expect(() => strictSchema.parse(event)).not.toThrow();
202+
});
203+
});
153204
});

Diff for: packages/parser/tests/unit/schema/apigwv2.test.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
APIGatewayProxyEventV2Schema,
88
APIGatewayRequestAuthorizerEventV2Schema,
99
} from '../../../src/schemas/index.js';
10-
import { getTestEvent } from './utils.js';
10+
import { getTestEvent, makeSchemaStrictForTesting } from './utils.js';
1111

1212
describe('API Gateway HTTP (v2) Schemas', () => {
1313
const eventsPath = 'apigw-http';
@@ -100,4 +100,34 @@ describe('API Gateway HTTP (v2) Schemas', () => {
100100
expect(parsedEvent).toEqual(event);
101101
});
102102
});
103+
104+
describe('should detect missing properties in schema for ', () => {
105+
it.each([
106+
'iam-auth',
107+
'jwt-authorizer-auth',
108+
'lambda-authorizer-auth',
109+
'no-auth',
110+
])('event %s', (filename) => {
111+
// Prepare
112+
const event = getTestEvent({ eventsPath, filename });
113+
const strictSchema = makeSchemaStrictForTesting(
114+
APIGatewayProxyEventV2Schema
115+
);
116+
// Act & Assess
117+
expect(() => strictSchema.parse(event)).not.toThrow();
118+
});
119+
120+
it('authorizer-request event', () => {
121+
// Prepare
122+
const event = getTestEvent({
123+
eventsPath,
124+
filename: 'authorizer-request',
125+
});
126+
const strictSchema = makeSchemaStrictForTesting(
127+
APIGatewayRequestAuthorizerEventV2Schema
128+
);
129+
// Act & Assess
130+
expect(() => strictSchema.parse(event)).not.toThrow();
131+
});
132+
});
103133
});

Diff for: packages/parser/tests/unit/schema/cloudformation-custom-resource.test.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
CloudFormationCustomResourceDeleteSchema,
1010
CloudFormationCustomResourceUpdateSchema,
1111
} from '../../../src/schemas/';
12-
import { TestEvents } from './utils.js';
12+
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';
1313

1414
describe('CloudFormationCustomResource ', () => {
1515
it('should parse create event', () => {
@@ -42,4 +42,45 @@ describe('CloudFormationCustomResource ', () => {
4242
)
4343
).toEqual(cloudFormationCustomResourceDeleteEvent);
4444
});
45+
46+
describe('should detect missing properties in schema for ', () => {
47+
it('CloudFormationCustomResourceCreateSchema', () => {
48+
const cloudFormationCustomResourceCreateEvent =
49+
TestEvents.cloudFormationCustomResourceCreateEvent;
50+
51+
const strictSchema = makeSchemaStrictForTesting(
52+
CloudFormationCustomResourceCreateSchema
53+
);
54+
55+
expect(() =>
56+
strictSchema.parse(cloudFormationCustomResourceCreateEvent)
57+
).not.toThrow();
58+
});
59+
60+
it('CloudFormationCustomResourceUpdateSchema', () => {
61+
const cloudFormationCustomResourceUpdateEvent =
62+
TestEvents.cloudFormationCustomResourceUpdateEvent;
63+
64+
const strictSchema = makeSchemaStrictForTesting(
65+
CloudFormationCustomResourceUpdateSchema
66+
);
67+
68+
expect(() =>
69+
strictSchema.parse(cloudFormationCustomResourceUpdateEvent)
70+
).not.toThrow();
71+
});
72+
73+
it('CloudFormationCustomResourceDeleteSchema', () => {
74+
const cloudFormationCustomResourceDeleteEvent =
75+
TestEvents.cloudFormationCustomResourceDeleteEvent;
76+
77+
const strictSchema = makeSchemaStrictForTesting(
78+
CloudFormationCustomResourceDeleteSchema
79+
);
80+
81+
expect(() =>
82+
strictSchema.parse(cloudFormationCustomResourceDeleteEvent)
83+
).not.toThrow();
84+
});
85+
});
4586
});

Diff for: packages/parser/tests/unit/schema/dynamodb.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { DynamoDBStreamSchema } from '../../../src/schemas/';
8-
import { TestEvents } from './utils.js';
8+
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';
99

1010
describe('DynamoDB ', () => {
1111
const dynamoStreamEvent = TestEvents.dynamoStreamEvent;
@@ -14,4 +14,10 @@ describe('DynamoDB ', () => {
1414
dynamoStreamEvent
1515
);
1616
});
17+
18+
it('should detect missing properties in schema', () => {
19+
const strictSchema = makeSchemaStrictForTesting(DynamoDBStreamSchema);
20+
21+
expect(() => strictSchema.parse(dynamoStreamEvent)).not.toThrow();
22+
});
1723
});

Diff for: packages/parser/tests/unit/schema/eventbridge.test.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
*/
66

77
import { EventBridgeSchema } from '../../../src/schemas/';
8-
import { TestEvents } from './utils.js';
8+
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';
99

1010
describe('EventBridge ', () => {
1111
it('should parse eventbridge event', () => {
1212
const eventBridgeEvent = TestEvents.eventBridgeEvent;
1313

1414
expect(EventBridgeSchema.parse(eventBridgeEvent)).toEqual(eventBridgeEvent);
1515
});
16+
17+
it('should detect missing properties in schema', () => {
18+
const eventBridgeEvent = TestEvents.eventBridgeEvent;
19+
const strictSchema = makeSchemaStrictForTesting(EventBridgeSchema);
20+
21+
expect(() => strictSchema.parse(eventBridgeEvent)).not.toThrow();
22+
});
1623
});

Diff for: packages/parser/tests/unit/schema/kafka.test.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '../../../src/schemas/';
1212
import type { KafkaSelfManagedEvent } from '../../../src/types';
1313
import type { KafkaRecord } from '../../../src/types/schema';
14-
import { TestEvents } from './utils.js';
14+
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';
1515

1616
describe('Kafka ', () => {
1717
const expectedTestEvent = {
@@ -71,4 +71,22 @@ describe('Kafka ', () => {
7171
);
7272
expect(parsedRecord.topic).toEqual('mytopic');
7373
});
74+
75+
describe('should detect missing properties in schema for', () => {
76+
it('KafkaMskEventSchema', () => {
77+
const kafkaEventMsk = TestEvents.kafkaEventMsk;
78+
79+
const strictSchema = makeSchemaStrictForTesting(KafkaMskEventSchema);
80+
expect(() => strictSchema.parse(kafkaEventMsk)).not.toThrow();
81+
});
82+
83+
it('KafkaSelfManagedEventSchema', () => {
84+
const kafkaEventSelfManaged = TestEvents.kafkaEventSelfManaged;
85+
86+
const strictSchema = makeSchemaStrictForTesting(
87+
KafkaSelfManagedEventSchema
88+
);
89+
expect(() => strictSchema.parse(kafkaEventSelfManaged)).not.toThrow();
90+
});
91+
});
7492
});

Diff for: packages/parser/tests/unit/schema/kinesis.test.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
KinesisFirehoseRecord,
2222
KinesisFirehoseSqsRecord,
2323
} from '../../../src/types/schema';
24-
import { TestEvents } from './utils.js';
24+
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';
2525

2626
describe('Kinesis ', () => {
2727
it('should parse kinesis event', () => {
@@ -30,6 +30,7 @@ describe('Kinesis ', () => {
3030

3131
expect(parsed.Records[0].kinesis.data).toEqual('Hello, this is a test.');
3232
});
33+
3334
it('should parse single kinesis record', () => {
3435
const kinesisStreamEventOneRecord = TestEvents.kinesisStreamEventOneRecord;
3536
const parsed = KinesisDataStreamSchema.parse(kinesisStreamEventOneRecord);
@@ -39,18 +40,21 @@ describe('Kinesis ', () => {
3940
username: 'test',
4041
});
4142
});
43+
4244
it('should parse Firehose event', () => {
4345
const kinesisFirehoseKinesisEvent = TestEvents.kinesisFirehoseKinesisEvent;
4446
const parsed = KinesisFirehoseSchema.parse(kinesisFirehoseKinesisEvent);
4547
expect(parsed.records[0].data).toEqual('Hello World');
4648
});
49+
4750
it('should parse Kinesis Firehose PutEvents event', () => {
4851
const kinesisFirehosePutEvent = TestEvents.kinesisFirehosePutEvent;
4952
const parsed = KinesisFirehoseSchema.parse(kinesisFirehosePutEvent);
5053
expect(JSON.parse(parsed.records[1].data)).toEqual({
5154
Hello: 'World',
5255
});
5356
});
57+
5458
it('should parse Firehose event with SQS event', () => {
5559
const kinesisFirehoseSQSEvent = TestEvents.kinesisFirehoseSQSEvent;
5660
const parsed = KinesisFirehoseSqsSchema.parse(kinesisFirehoseSQSEvent);
@@ -59,6 +63,7 @@ describe('Kinesis ', () => {
5963
body: 'Test message.',
6064
});
6165
});
66+
6267
it('should parse Kinesis event with CloudWatch event', () => {
6368
const kinesisStreamCloudWatchLogsEvent =
6469
TestEvents.kinesisStreamCloudWatchLogsEvent;
@@ -73,6 +78,7 @@ describe('Kinesis ', () => {
7378
logStream: '2022/11/10/[$LATEST]26b6a45d574f442ea28438923cbf7bf7',
7479
});
7580
});
81+
7682
it('should return original value if cannot parse KinesisFirehoseSqsRecord', () => {
7783
const kinesisFirehoseSQSEvent = TestEvents.kinesisFirehoseSQSEvent as {
7884
records: { data: string }[];
@@ -81,13 +87,13 @@ describe('Kinesis ', () => {
8187
const parsed = KinesisFirehoseSqsSchema.parse(kinesisFirehoseSQSEvent);
8288
expect(parsed.records[0].data).toEqual('not a valid json');
8389
});
90+
8491
it('should parse a kinesis record from a kinesis event', () => {
8592
const kinesisStreamEvent: KinesisDataStreamEvent =
8693
TestEvents.kinesisStreamEvent as KinesisDataStreamEvent;
8794
const parsedRecord = KinesisDataStreamRecord.parse(
8895
kinesisStreamEvent.Records[0]
8996
);
90-
9197
expect(parsedRecord.eventName).toEqual('aws:kinesis:record');
9298
});
9399

@@ -110,4 +116,10 @@ describe('Kinesis ', () => {
110116
'49640912821178817833517986466168945147170627572855734274000000'
111117
);
112118
});
119+
120+
it('should catch any unknown fields in the example event', () => {
121+
const kinesisStreamEvent = TestEvents.kinesisStreamEvent;
122+
const strictSchema = makeSchemaStrictForTesting(KinesisDataStreamSchema);
123+
expect(() => strictSchema.parse(kinesisStreamEvent)).not.toThrow();
124+
});
113125
});

Diff for: packages/parser/tests/unit/schema/lambda.test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
*/
66

77
import { LambdaFunctionUrlSchema } from '../../../src/schemas/';
8-
import { TestEvents } from './utils.js';
8+
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';
99

1010
describe('Lambda ', () => {
1111
it('should parse lambda event', () => {
12-
const lambdaFunctionUrlEvent = TestEvents.apiGatewayProxyV2Event;
12+
const lambdaFunctionUrlEvent = TestEvents.lambdaFunctionUrlEvent;
1313

1414
expect(LambdaFunctionUrlSchema.parse(lambdaFunctionUrlEvent)).toEqual(
1515
lambdaFunctionUrlEvent
@@ -21,4 +21,12 @@ describe('Lambda ', () => {
2121

2222
expect(LambdaFunctionUrlSchema.parse(urlIAMEvent)).toEqual(urlIAMEvent);
2323
});
24+
25+
it('should detect missing properties in schema for lambda event', () => {
26+
const lambdaFunctionUrlEvent = TestEvents.lambdaFunctionUrlEvent;
27+
28+
const strictSchema = makeSchemaStrictForTesting(LambdaFunctionUrlSchema);
29+
30+
expect(() => strictSchema.parse(lambdaFunctionUrlEvent)).not.toThrow();
31+
});
2432
});

0 commit comments

Comments
 (0)