From 1ff97cb758b2e44a76ce108d8e54c9335088abba Mon Sep 17 00:00:00 2001 From: Alexander Schueren Date: Fri, 25 Oct 2024 22:44:53 +0200 Subject: [PATCH] fix(parser): fix cause errors nested structure (#3250) --- packages/parser/src/envelopes/apigw.ts | 7 ++--- packages/parser/src/envelopes/apigwv2.ts | 1 + packages/parser/src/envelopes/envelope.ts | 20 ++++--------- packages/parser/src/envelopes/kafka.ts | 2 +- packages/parser/src/types/parser.ts | 2 +- packages/parser/tests/unit/envelope.test.ts | 7 ++--- .../parser/tests/unit/envelopes/apigw.test.ts | 9 ++++++ .../tests/unit/envelopes/apigwv2.test.ts | 5 ++++ .../tests/unit/envelopes/cloudwatch.test.ts | 10 +++++-- .../tests/unit/envelopes/dynamodb.test.ts | 13 +++++++-- .../tests/unit/envelopes/eventbridge.test.ts | 15 +++++++--- .../parser/tests/unit/envelopes/kafka.test.ts | 13 ++++++--- .../unit/envelopes/kinesis-firehose.test.ts | 28 +++++++++---------- .../tests/unit/envelopes/kinesis.test.ts | 8 ++++-- .../tests/unit/envelopes/lambda.test.ts | 18 ++++++++---- .../parser/tests/unit/envelopes/sns.test.ts | 22 +++++++++++---- .../parser/tests/unit/envelopes/sqs.test.ts | 8 +++++- .../tests/unit/envelopes/vpc-lattice.test.ts | 14 +++++++--- .../unit/envelopes/vpc-latticev2.test.ts | 14 +++++++--- .../parser/tests/unit/parser.middy.test.ts | 5 ++-- 20 files changed, 142 insertions(+), 79 deletions(-) diff --git a/packages/parser/src/envelopes/apigw.ts b/packages/parser/src/envelopes/apigw.ts index 5eda5168c5..8bfba6cdf6 100644 --- a/packages/parser/src/envelopes/apigw.ts +++ b/packages/parser/src/envelopes/apigw.ts @@ -1,7 +1,7 @@ import type { ZodSchema, z } from 'zod'; import { ParseError } from '../errors.js'; import { APIGatewayProxyEventSchema } from '../schemas/apigw.js'; -import type { ParsedResult } from '../types/parser.js'; +import type { ParsedResult } from '../types'; import { Envelope } from './envelope.js'; /** @@ -12,10 +12,7 @@ export const ApiGatewayEnvelope = { return Envelope.parse(APIGatewayProxyEventSchema.parse(data).body, schema); }, - safeParse( - data: unknown, - schema: T - ): ParsedResult> { + safeParse(data: unknown, schema: T): ParsedResult { const parsedEnvelope = APIGatewayProxyEventSchema.safeParse(data); if (!parsedEnvelope.success) { return { diff --git a/packages/parser/src/envelopes/apigwv2.ts b/packages/parser/src/envelopes/apigwv2.ts index 10a461687f..db69794649 100644 --- a/packages/parser/src/envelopes/apigwv2.ts +++ b/packages/parser/src/envelopes/apigwv2.ts @@ -39,6 +39,7 @@ export const ApiGatewayV2Envelope = { }; } + // use type assertion to avoid type check, we know it's success here return parsedBody; }, }; diff --git a/packages/parser/src/envelopes/envelope.ts b/packages/parser/src/envelopes/envelope.ts index 2291412174..8a33e5d30b 100644 --- a/packages/parser/src/envelopes/envelope.ts +++ b/packages/parser/src/envelopes/envelope.ts @@ -1,6 +1,6 @@ import type { ZodSchema, z } from 'zod'; import { ParseError } from '../errors.js'; -import type { ParsedResult } from '../types/parser.js'; +import type { ParsedResult } from '../types'; export const Envelope = { /** @@ -35,18 +35,14 @@ export const Envelope = { * @param input * @param schema */ - safeParse( - input: unknown, - schema: T - ): ParsedResult> { + safeParse(input: unknown, schema: T): ParsedResult { try { if (typeof input !== 'object' && typeof input !== 'string') { return { success: false, - error: new ParseError( + error: new Error( `Invalid data type for envelope. Expected string or object, got ${typeof input}` ), - originalEvent: input, }; } @@ -61,18 +57,12 @@ export const Envelope = { } : { success: false, - error: new ParseError('Failed to parse envelope', { - cause: parsed.error, - }), - originalEvent: input, + error: parsed.error, }; } catch (e) { return { success: false, - error: new ParseError('Failed to parse envelope', { - cause: e as Error, - }), - originalEvent: input, + error: e as Error, }; } }, diff --git a/packages/parser/src/envelopes/kafka.ts b/packages/parser/src/envelopes/kafka.ts index 252ff3ea55..4442834aff 100644 --- a/packages/parser/src/envelopes/kafka.ts +++ b/packages/parser/src/envelopes/kafka.ts @@ -18,7 +18,7 @@ import { Envelope } from './envelope.js'; export const KafkaEnvelope = { parse(data: unknown, schema: T): z.infer[] { - // manually fetch event source to deside between Msk or SelfManaged + // manually fetch event source to decide between Msk or SelfManaged const eventSource = (data as KafkaMskEvent).eventSource; const parsedEnvelope: diff --git a/packages/parser/src/types/parser.ts b/packages/parser/src/types/parser.ts index b0e4f79018..c7fe891c6a 100644 --- a/packages/parser/src/types/parser.ts +++ b/packages/parser/src/types/parser.ts @@ -28,7 +28,7 @@ type ParsedResultSuccess = { type ParsedResultError = { success: false; error: ZodError | Error; - originalEvent: Input; + originalEvent?: Input; }; /** diff --git a/packages/parser/tests/unit/envelope.test.ts b/packages/parser/tests/unit/envelope.test.ts index 3b314bb6de..9d4fea59db 100644 --- a/packages/parser/tests/unit/envelope.test.ts +++ b/packages/parser/tests/unit/envelope.test.ts @@ -6,7 +6,6 @@ import { ZodError, z } from 'zod'; import { Envelope } from '../../src/envelopes/envelope.js'; -import { ParseError } from '../../src/errors.js'; describe('envelope: ', () => { describe('parseSafe', () => { @@ -37,8 +36,7 @@ describe('envelope: ', () => { ); expect(result).toEqual({ success: false, - error: expect.any(ParseError), - originalEvent: { name: 123 }, + error: expect.any(Error), }); }); @@ -49,8 +47,7 @@ describe('envelope: ', () => { ); expect(result).toEqual({ success: false, - error: expect.any(ParseError), - originalEvent: '{name: "John"}', + error: expect.any(Error), }); }); }); diff --git a/packages/parser/tests/unit/envelopes/apigw.test.ts b/packages/parser/tests/unit/envelopes/apigw.test.ts index 43ceda3a91..8c0486884d 100644 --- a/packages/parser/tests/unit/envelopes/apigw.test.ts +++ b/packages/parser/tests/unit/envelopes/apigw.test.ts @@ -4,6 +4,7 @@ * @group unit/parser/envelopes/apigw */ +import { ZodError } from 'zod'; import { ApiGatewayEnvelope } from '../../../src/envelopes/index.js'; import { ParseError } from '../../../src/errors.js'; import type { APIGatewayProxyEvent } from '../../../src/types/schema.js'; @@ -68,6 +69,10 @@ describe('API Gateway REST Envelope', () => { error: expect.any(ParseError), originalEvent: event, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('should not throw if the body is null', () => { @@ -84,6 +89,10 @@ describe('API Gateway REST Envelope', () => { error: expect.any(ParseError), originalEvent: event, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('should not throw if the event is invalid', () => { diff --git a/packages/parser/tests/unit/envelopes/apigwv2.test.ts b/packages/parser/tests/unit/envelopes/apigwv2.test.ts index 0d4907380b..b3f97512c8 100644 --- a/packages/parser/tests/unit/envelopes/apigwv2.test.ts +++ b/packages/parser/tests/unit/envelopes/apigwv2.test.ts @@ -4,6 +4,7 @@ * @group unit/parser/envelopes/apigwv2 */ +import { ZodError } from 'zod'; import { ApiGatewayV2Envelope } from '../../../src/envelopes/index.js'; import { ParseError } from '../../../src/errors.js'; import type { APIGatewayProxyEventV2 } from '../../../src/types/schema.js'; @@ -68,6 +69,10 @@ describe('API Gateway HTTP Envelope', () => { error: expect.any(ParseError), originalEvent: event, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('should not throw if the body is undefined', () => { diff --git a/packages/parser/tests/unit/envelopes/cloudwatch.test.ts b/packages/parser/tests/unit/envelopes/cloudwatch.test.ts index bc562ef13f..4a62d28b93 100644 --- a/packages/parser/tests/unit/envelopes/cloudwatch.test.ts +++ b/packages/parser/tests/unit/envelopes/cloudwatch.test.ts @@ -6,6 +6,7 @@ import { gzipSync } from 'node:zlib'; import { generateMock } from '@anatine/zod-mock'; +import { ZodError } from 'zod'; import { ParseError } from '../../../src'; import { CloudWatchEnvelope } from '../../../src/envelopes/index.js'; import { @@ -113,11 +114,16 @@ describe('CloudWatch', () => { Buffer.from(JSON.stringify(logMock), 'utf8') ).toString('base64'); - expect(CloudWatchEnvelope.safeParse(testEvent, TestSchema)).toEqual({ + const parseResult = CloudWatchEnvelope.safeParse(testEvent, TestSchema); + expect(parseResult).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: testEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('should return success false when envelope does not match', () => { diff --git a/packages/parser/tests/unit/envelopes/dynamodb.test.ts b/packages/parser/tests/unit/envelopes/dynamodb.test.ts index df3720ae69..b04b01b4a5 100644 --- a/packages/parser/tests/unit/envelopes/dynamodb.test.ts +++ b/packages/parser/tests/unit/envelopes/dynamodb.test.ts @@ -6,7 +6,7 @@ import { generateMock } from '@anatine/zod-mock'; import type { AttributeValue, DynamoDBStreamEvent } from 'aws-lambda'; -import { z } from 'zod'; +import { ZodError, z } from 'zod'; import { DynamoDBStreamEnvelope } from '../../../src/envelopes/index.js'; import { ParseError } from '../../../src/errors.js'; import { TestEvents } from '../schema/utils.js'; @@ -93,12 +93,19 @@ describe('DynamoDB', () => { (invalidDDBEvent.Records[1].dynamodb!.OldImage as typeof mockOldImage) = mockOldImage; - const parsed = DynamoDBStreamEnvelope.safeParse(invalidDDBEvent, schema); - expect(parsed).toEqual({ + const parseResult = DynamoDBStreamEnvelope.safeParse( + invalidDDBEvent, + schema + ); + expect(parseResult).toEqual({ success: false, error: expect.any(ParseError), originalEvent: invalidDDBEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('safeParse should return error if OldImage is invalid', () => { diff --git a/packages/parser/tests/unit/envelopes/eventbridge.test.ts b/packages/parser/tests/unit/envelopes/eventbridge.test.ts index d713273544..8761c23e7a 100644 --- a/packages/parser/tests/unit/envelopes/eventbridge.test.ts +++ b/packages/parser/tests/unit/envelopes/eventbridge.test.ts @@ -6,6 +6,7 @@ import { generateMock } from '@anatine/zod-mock'; import type { EventBridgeEvent } from 'aws-lambda'; +import { ZodError } from 'zod'; import { EventBridgeEnvelope } from '../../../src/envelopes/index.js'; import { ParseError } from '../../../src/errors.js'; import { TestEvents, TestSchema } from '../schema/utils.js'; @@ -85,13 +86,19 @@ describe('EventBridgeEnvelope ', () => { foo: 'bar', }; - expect( - EventBridgeEnvelope.safeParse(eventBridgeEvent, TestSchema) - ).toEqual({ + const parseResult = EventBridgeEnvelope.safeParse( + eventBridgeEvent, + TestSchema + ); + expect(parseResult).toEqual({ success: false, error: expect.any(ParseError), originalEvent: eventBridgeEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('should safe parse eventbridge event and return original event if invalid data type', () => { @@ -106,7 +113,7 @@ describe('EventBridgeEnvelope ', () => { EventBridgeEnvelope.safeParse(eventBridgeEvent, TestSchema) ).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: eventBridgeEvent, }); }); diff --git a/packages/parser/tests/unit/envelopes/kafka.test.ts b/packages/parser/tests/unit/envelopes/kafka.test.ts index e47712b911..ff750f48e0 100644 --- a/packages/parser/tests/unit/envelopes/kafka.test.ts +++ b/packages/parser/tests/unit/envelopes/kafka.test.ts @@ -6,6 +6,7 @@ import { generateMock } from '@anatine/zod-mock'; import type { MSKEvent, SelfManagedKafkaEvent } from 'aws-lambda'; +import { ParseError } from '../../../src'; import { KafkaEnvelope } from '../../../src/envelopes/index.js'; import { TestEvents, TestSchema } from '../schema/utils.js'; @@ -76,18 +77,22 @@ describe('Kafka', () => { const kafkaEvent = TestEvents.kafkaEventMsk as MSKEvent; kafkaEvent.records['mytopic-0'][0].value = 'not a valid json'; - const result = KafkaEnvelope.safeParse(kafkaEvent, TestSchema); + const parseResult = KafkaEnvelope.safeParse(kafkaEvent, TestSchema); - expect(result).toEqual({ + expect(parseResult).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: kafkaEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(SyntaxError); + } }); it('should return original event and error if envelope is invalid', () => { expect(KafkaEnvelope.safeParse({ foo: 'bar' }, TestSchema)).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: { foo: 'bar' }, }); }); diff --git a/packages/parser/tests/unit/envelopes/kinesis-firehose.test.ts b/packages/parser/tests/unit/envelopes/kinesis-firehose.test.ts index 0137fa92e5..8e67e7d3ea 100644 --- a/packages/parser/tests/unit/envelopes/kinesis-firehose.test.ts +++ b/packages/parser/tests/unit/envelopes/kinesis-firehose.test.ts @@ -5,7 +5,8 @@ */ import { generateMock } from '@anatine/zod-mock'; -import type { z } from 'zod'; +import { ZodError, type z } from 'zod'; +import { ParseError } from '../../../src'; import { KinesisFirehoseEnvelope } from '../../../src/envelopes/index.js'; import type { KinesisFirehoseSchema } from '../../../src/schemas/'; import { TestEvents, TestSchema } from '../schema/utils.js'; @@ -128,13 +129,19 @@ describe('Kinesis Firehose Envelope', () => { expect(resp).toEqual({ success: true, data: [mock, mock] }); }); it('should return original event if envelope is invalid', () => { - expect( - KinesisFirehoseEnvelope.safeParse({ foo: 'bar' }, TestSchema) - ).toEqual({ + const parseResult = KinesisFirehoseEnvelope.safeParse( + { foo: 'bar' }, + TestSchema + ); + expect(parseResult).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: { foo: 'bar' }, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('should return original event if record is not base64 encoded', () => { const testEvent = TestEvents.kinesisFirehosePutEvent as z.infer< @@ -147,18 +154,9 @@ describe('Kinesis Firehose Envelope', () => { expect(KinesisFirehoseEnvelope.safeParse(testEvent, TestSchema)).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: testEvent, }); }); - it('should return original event envelope is invalid', () => { - expect( - KinesisFirehoseEnvelope.safeParse({ foo: 'bar' }, TestSchema) - ).toEqual({ - success: false, - error: expect.any(Error), - originalEvent: { foo: 'bar' }, - }); - }); }); }); diff --git a/packages/parser/tests/unit/envelopes/kinesis.test.ts b/packages/parser/tests/unit/envelopes/kinesis.test.ts index e84e74f528..f5350b3173 100644 --- a/packages/parser/tests/unit/envelopes/kinesis.test.ts +++ b/packages/parser/tests/unit/envelopes/kinesis.test.ts @@ -61,12 +61,16 @@ describe('KinesisEnvelope', () => { it('should return original event if record is invalid', () => { const testEvent = TestEvents.kinesisStreamEvent as KinesisStreamEvent; testEvent.Records[0].kinesis.data = 'invalid'; - const resp = KinesisEnvelope.safeParse(testEvent, TestSchema); - expect(resp).toEqual({ + const parseResult = KinesisEnvelope.safeParse(testEvent, TestSchema); + expect(parseResult).toEqual({ success: false, error: expect.any(ParseError), originalEvent: testEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(SyntaxError); + } }); }); }); diff --git a/packages/parser/tests/unit/envelopes/lambda.test.ts b/packages/parser/tests/unit/envelopes/lambda.test.ts index e9d0301969..d8ed4ef50a 100644 --- a/packages/parser/tests/unit/envelopes/lambda.test.ts +++ b/packages/parser/tests/unit/envelopes/lambda.test.ts @@ -6,6 +6,8 @@ import { generateMock } from '@anatine/zod-mock'; import type { APIGatewayProxyEventV2 } from 'aws-lambda'; +import { ZodError } from 'zod'; +import { ParseError } from '../../../src'; import { LambdaFunctionUrlEnvelope } from '../../../src/envelopes/index.js'; import { TestEvents, TestSchema } from '../schema/utils.js'; @@ -70,7 +72,7 @@ describe('Lambda Functions Url ', () => { LambdaFunctionUrlEnvelope.safeParse({ foo: 'bar' }, TestSchema) ).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: { foo: 'bar' }, }); }); @@ -80,13 +82,19 @@ describe('Lambda Functions Url ', () => { TestEvents.lambdaFunctionUrlEvent as APIGatewayProxyEventV2; testEvent.body = JSON.stringify({ foo: 'bar' }); - expect( - LambdaFunctionUrlEnvelope.safeParse(testEvent, TestSchema) - ).toEqual({ + const parseResult = LambdaFunctionUrlEnvelope.safeParse( + testEvent, + TestSchema + ); + expect(parseResult).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: testEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); }); }); diff --git a/packages/parser/tests/unit/envelopes/sns.test.ts b/packages/parser/tests/unit/envelopes/sns.test.ts index 0e26511238..f741d50a78 100644 --- a/packages/parser/tests/unit/envelopes/sns.test.ts +++ b/packages/parser/tests/unit/envelopes/sns.test.ts @@ -6,7 +6,7 @@ import { generateMock } from '@anatine/zod-mock'; import type { SNSEvent, SQSEvent } from 'aws-lambda'; -import type { z } from 'zod'; +import { ZodError, type z } from 'zod'; import { SnsEnvelope, SnsSqsEnvelope } from '../../../src/envelopes/index.js'; import { ParseError } from '../../../src/errors.js'; import { TestEvents, TestSchema } from '../schema/utils.js'; @@ -43,7 +43,7 @@ describe('Sns and SQS Envelope', () => { it('should return error when envelope is not valid', () => { expect(SnsSqsEnvelope.safeParse({ foo: 'bar' }, TestSchema)).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: { foo: 'bar' }, }); }); @@ -57,11 +57,16 @@ describe('Sns and SQS Envelope', () => { snsSqsTestEvent.Records[0].body = JSON.stringify(snsEvent); - expect(SnsSqsEnvelope.safeParse(snsSqsTestEvent, TestSchema)).toEqual({ + const parseResult = SnsSqsEnvelope.safeParse(snsSqsTestEvent, TestSchema); + expect(parseResult).toEqual({ success: false, error: expect.any(ParseError), originalEvent: snsSqsTestEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('should return error if sns message is not valid', () => { const snsSqsTestEvent = TestEvents.snsSqsEvent as SQSEvent; @@ -83,7 +88,7 @@ describe('Sns and SQS Envelope', () => { expect(SnsSqsEnvelope.safeParse(snsSqsTestEvent, TestSchema)).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(SyntaxError), originalEvent: snsSqsTestEvent, }); }); @@ -147,16 +152,21 @@ describe('SnsEnvelope', () => { }); }); - expect(SnsEnvelope.safeParse(testEvent, TestSchema)).toEqual({ + const parseResult = SnsEnvelope.safeParse(testEvent, TestSchema); + expect(parseResult).toEqual({ success: false, error: expect.any(ParseError), originalEvent: testEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('should return error when envelope is not valid', () => { expect(SnsEnvelope.safeParse({ foo: 'bar' }, TestSchema)).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: { foo: 'bar' }, }); }); diff --git a/packages/parser/tests/unit/envelopes/sqs.test.ts b/packages/parser/tests/unit/envelopes/sqs.test.ts index 4979bccfcf..61bf8cdf0d 100644 --- a/packages/parser/tests/unit/envelopes/sqs.test.ts +++ b/packages/parser/tests/unit/envelopes/sqs.test.ts @@ -6,6 +6,7 @@ import { generateMock } from '@anatine/zod-mock'; import type { SQSEvent } from 'aws-lambda'; +import { ZodError } from 'zod'; import { SqsEnvelope } from '../../../src/envelopes/sqs.js'; import { ParseError } from '../../../src/errors.js'; import { TestEvents, TestSchema } from '../schema/utils.js'; @@ -52,11 +53,16 @@ describe('SqsEnvelope ', () => { it('should return error if event does not match schema', () => { const sqsEvent = TestEvents.sqsEvent as SQSEvent; sqsEvent.Records[0].body = JSON.stringify({ foo: 'bar' }); - expect(SqsEnvelope.safeParse(sqsEvent, TestSchema)).toEqual({ + const parseResult = SqsEnvelope.safeParse(sqsEvent, TestSchema); + expect(parseResult).toEqual({ success: false, error: expect.any(ParseError), originalEvent: sqsEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); it('should return error if envelope is invalid', () => { diff --git a/packages/parser/tests/unit/envelopes/vpc-lattice.test.ts b/packages/parser/tests/unit/envelopes/vpc-lattice.test.ts index a3eec5be00..6fee771a98 100644 --- a/packages/parser/tests/unit/envelopes/vpc-lattice.test.ts +++ b/packages/parser/tests/unit/envelopes/vpc-lattice.test.ts @@ -5,6 +5,8 @@ */ import { generateMock } from '@anatine/zod-mock'; +import { ZodError } from 'zod'; +import { ParseError } from '../../../src'; import { VpcLatticeEnvelope } from '../../../src/envelopes/index.js'; import type { VpcLatticeEvent } from '../../../src/types/index.js'; import { TestEvents, TestSchema } from '../schema/utils.js'; @@ -76,7 +78,7 @@ describe('VpcLatticeEnvelope', () => { expect(resp).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: { foo: 'bar' }, }); }); @@ -86,12 +88,16 @@ describe('VpcLatticeEnvelope', () => { testEvent.body = JSON.stringify({ foo: 'bar' }); - const resp = VpcLatticeEnvelope.safeParse(testEvent, TestSchema); - expect(resp).toEqual({ + const parseResult = VpcLatticeEnvelope.safeParse(testEvent, TestSchema); + expect(parseResult).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: testEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); }); }); diff --git a/packages/parser/tests/unit/envelopes/vpc-latticev2.test.ts b/packages/parser/tests/unit/envelopes/vpc-latticev2.test.ts index a3412d8fe4..6c06f642f9 100644 --- a/packages/parser/tests/unit/envelopes/vpc-latticev2.test.ts +++ b/packages/parser/tests/unit/envelopes/vpc-latticev2.test.ts @@ -5,6 +5,8 @@ */ import { generateMock } from '@anatine/zod-mock'; +import { ZodError } from 'zod'; +import { ParseError } from '../../../src'; import { VpcLatticeV2Envelope } from '../../../src/envelopes/index.js'; import type { VpcLatticeEventV2 } from '../../../src/types/index.js'; import { TestEvents, TestSchema } from '../schema/utils.js'; @@ -76,7 +78,7 @@ describe('VpcLatticeV2Envelope2', () => { expect(resp).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: { foo: 'bar' }, }); }); @@ -86,12 +88,16 @@ describe('VpcLatticeV2Envelope2', () => { testEvent.body = JSON.stringify({ foo: 'bar' }); - const resp = VpcLatticeV2Envelope.safeParse(testEvent, TestSchema); - expect(resp).toEqual({ + const parseResult = VpcLatticeV2Envelope.safeParse(testEvent, TestSchema); + expect(parseResult).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: testEvent, }); + + if (!parseResult.success && parseResult.error) { + expect(parseResult.error.cause).toBeInstanceOf(ZodError); + } }); }); }); diff --git a/packages/parser/tests/unit/parser.middy.test.ts b/packages/parser/tests/unit/parser.middy.test.ts index 686566b38c..32d13ad05e 100644 --- a/packages/parser/tests/unit/parser.middy.test.ts +++ b/packages/parser/tests/unit/parser.middy.test.ts @@ -8,6 +8,7 @@ import { generateMock } from '@anatine/zod-mock'; import middy from '@middy/core'; import type { Context } from 'aws-lambda'; import type { ZodSchema, z } from 'zod'; +import { ParseError } from '../../src'; import { EventBridgeEnvelope, SqsEnvelope } from '../../src/envelopes'; import { parser } from '../../src/middleware/parser.js'; import { SqsSchema } from '../../src/schemas'; @@ -179,7 +180,7 @@ describe('Middleware: parser', () => { ) ).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: 42, }); }); @@ -238,7 +239,7 @@ describe('Middleware: parser', () => { ) ).toEqual({ success: false, - error: expect.any(Error), + error: expect.any(ParseError), originalEvent: event, }); });