Skip to content

feat(parser): implement safeParse option #2244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bump coverage
  • Loading branch information
am29d committed Mar 15, 2024
commit bdb46cc887e4cddd92cb5f42a4869cdd889347e2
2 changes: 1 addition & 1 deletion packages/parser/tests/unit/envelopes/apigwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { generateMock } from '@anatine/zod-mock';
import { TestEvents, TestSchema } from '../schema/utils.js';
import { APIGatewayProxyEvent } from '../../../src/types/';
import { ApiGatewayEnvelope } from '../../../src/envelopes/apigw.js';
import { ApiGatewayEnvelope } from '../../../src/envelopes/index.js';
import { ZodError } from 'zod';

describe('ApigwEnvelope ', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/tests/unit/envelopes/apigwv2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { TestEvents, TestSchema } from '../schema/utils.js';
import { generateMock } from '@anatine/zod-mock';
import { APIGatewayProxyEventV2 } from 'aws-lambda';
import { ApiGatewayV2Envelope } from '../../../src/envelopes/apigwv2.js';
import { ApiGatewayV2Envelope } from '../../../src/envelopes/index.js';

describe('ApiGwV2Envelope ', () => {
describe('parse', () => {
Expand Down
42 changes: 37 additions & 5 deletions packages/parser/tests/unit/envelopes/dynamodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { generateMock } from '@anatine/zod-mock';
import { TestEvents } from '../schema/utils.js';
import { DynamoDBStreamEvent } from 'aws-lambda';
import { z, ZodError } from 'zod';
import { DynamoDBStreamEnvelope } from '../../../src/envelopes/dynamodb.js';
import { DynamoDBStreamEnvelope } from '../../../src/envelopes/index.js';

describe('DynamoDB', () => {
const schema = z.object({
Expand Down Expand Up @@ -70,12 +70,44 @@ describe('DynamoDB', () => {
],
});
});
it('safeParse should return error if image is invalid', () => {
it('safeParse should return error if NewImage is invalid', () => {
const invalidDDBEvent =
TestEvents.dynamoStreamEvent as DynamoDBStreamEvent;
(
invalidDDBEvent.Records[0].dynamodb!.NewImage as typeof mockNewImage
).Id = { N: 101 };

(invalidDDBEvent.Records[0].dynamodb!.NewImage as typeof mockNewImage) = {
Id: { N: 101 },
Message: { S: 'foo' },
};
(invalidDDBEvent.Records[1].dynamodb!.NewImage as typeof mockNewImage) =
mockNewImage;
(invalidDDBEvent.Records[0].dynamodb!.OldImage as typeof mockOldImage) =
mockOldImage;
(invalidDDBEvent.Records[1].dynamodb!.OldImage as typeof mockOldImage) =
mockOldImage;

const parsed = DynamoDBStreamEnvelope.safeParse(invalidDDBEvent, schema);
expect(parsed).toEqual({
success: false,
error: expect.any(ZodError),
originalEvent: invalidDDBEvent,
});
});

it('safeParse should return error if OldImage is invalid', () => {
const invalidDDBEvent =
TestEvents.dynamoStreamEvent as DynamoDBStreamEvent;

(invalidDDBEvent.Records[0].dynamodb!.OldImage as typeof mockNewImage) = {
Id: { N: 101 },
Message: { S: 'foo' },
};
(invalidDDBEvent.Records[1].dynamodb!.NewImage as typeof mockNewImage) =
mockNewImage;
(invalidDDBEvent.Records[0].dynamodb!.OldImage as typeof mockOldImage) =
mockOldImage;
(invalidDDBEvent.Records[0].dynamodb!.NewImage as typeof mockNewImage) =
mockNewImage;

const parsed = DynamoDBStreamEnvelope.safeParse(invalidDDBEvent, schema);
expect(parsed).toEqual({
success: false,
Expand Down