Skip to content
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

improv(parser): correctly infer helper return type #2946

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions packages/parser/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ZodSchema, z } from 'zod';
import { type ZodTypeAny, z } from 'zod';

/**
* A helper function to parse a JSON string and validate it against a schema.
Expand All @@ -23,7 +23,7 @@ import { type ZodSchema, z } from 'zod';
*
* @param schema - The schema to validate the JSON string against
*/
const JSONStringified = (schema: ZodSchema) =>
const JSONStringified = <T extends ZodTypeAny>(schema: T) =>
z
.string()
.transform((str, ctx) => {
Expand Down
219 changes: 108 additions & 111 deletions packages/parser/tests/unit/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,159 +3,156 @@
*
* @group unit/parser
*/

import { z } from 'zod';
import { JSONStringified } from '../../src/helpers.js';
import { AlbSchema } from '../../src/schemas/alb.js';
import {
AlbSchema,
SnsNotificationSchema,
SnsRecordSchema,
SqsRecordSchema,
SqsSchema,
} from '../../src/schemas';
import type { SnsEvent, SqsEvent } from '../../src/types';
import { getTestEvent } from './schema/utils';
} from '../../src/schemas/sns.js';
import { SqsRecordSchema, SqsSchema } from '../../src/schemas/sqs.js';
import type { SnsEvent, SqsEvent } from '../../src/types/schema.js';
import { getTestEvent } from './schema/utils.js';

const bodySchema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email(),
});
const envelopeSchema = z.object({
body: z.string(),
});
const basePayload = {
id: 1,
name: 'John Doe',
email: 'foo@bar.baz',
};

describe('JSONStringified', () => {
const schema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email(),
});
const baseSchema = z.object({
body: z.string(),
});
it('should return a valid JSON', () => {
// Prepare
const data = {
body: JSON.stringify({
id: 1,
name: 'John Doe',
email: 'foo@example.com',
}),
body: JSON.stringify(structuredClone(basePayload)),
};

const extendedSchema = baseSchema.extend({
body: JSONStringified(schema),
// Act
const extendedSchema = envelopeSchema.extend({
body: JSONStringified(bodySchema),
});

const result = extendedSchema.parse(data);
expect(result).toEqual({
body: { id: 1, name: 'John Doe', email: 'foo@example.com' },
// Assess
expect(extendedSchema.parse(data)).toStrictEqual({
body: basePayload,
});
});

it('should throw an error if the JSON payload is invalid', () => {
// Prepare
const data = {
body: JSON.stringify({
id: 1,
name: 'John Doe',
email: 'foo',
}),
body: JSON.stringify({ ...basePayload, email: 'invalid' }),
};

const extendedSchema = baseSchema.extend({
body: JSONStringified(schema),
// Act
const extendedSchema = envelopeSchema.extend({
body: JSONStringified(bodySchema),
});

// Assess
expect(() => extendedSchema.parse(data)).toThrow();
});

it('should throw an error if the JSON is malformed', () => {
// Prepare
const data = {
body: 'invalid',
};

const extendedSchema = baseSchema.extend({
body: JSONStringified(schema),
// Act
const extendedSchema = envelopeSchema.extend({
body: JSONStringified(bodySchema),
});

// Assess
expect(() => extendedSchema.parse(data)).toThrow();
});

describe('should parse common built-in schemas', () => {
const customSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email(),
it('should parse extended AlbSchema', () => {
// Prepare
const testEvent = getTestEvent({
eventsPath: '.',
filename: 'albEvent',
});
testEvent.body = JSON.stringify(structuredClone(basePayload));

const payload = {
id: 1,
name: 'John Doe',
email: 'foo@bar.baz',
};
// Act
const extendedSchema = AlbSchema.extend({
body: JSONStringified(bodySchema),
});

it('should parse extended AlbSchema', () => {
const extendedSchema = AlbSchema.extend({
body: JSONStringified(customSchema),
});

const testEvent = getTestEvent({
eventsPath: '.',
filename: 'albEvent',
});
testEvent.body = JSON.stringify(payload);

const result = extendedSchema.parse(testEvent);
expect(result).toEqual({
...testEvent,
body: payload,
});
// Assess
expect(extendedSchema.parse(testEvent)).toStrictEqual({
...testEvent,
body: basePayload,
});
});

it('should parse extended SqsSchema', () => {
const extendedSchema = SqsSchema.extend({
Records: z.array(
SqsRecordSchema.extend({
body: JSONStringified(customSchema),
})
),
});

const testEvent = getTestEvent<SqsEvent>({
eventsPath: '.',
filename: 'sqsEvent',
});
testEvent.Records[0].body = JSON.stringify(payload);
testEvent.Records[1].body = JSON.stringify(payload);

const result = extendedSchema.parse(testEvent);
expect(result).toEqual({
...testEvent,
Records: [
{ ...testEvent.Records[0], body: payload },
{ ...testEvent.Records[1], body: payload },
],
});
it('should parse extended SqsSchema', () => {
// Prepare
const testEvent = getTestEvent<SqsEvent>({
eventsPath: '.',
filename: 'sqsEvent',
});
const stringifiedBody = JSON.stringify(basePayload);
testEvent.Records[0].body = stringifiedBody;
testEvent.Records[1].body = stringifiedBody;

// Act
const extendedSchema = SqsSchema.extend({
Records: z.array(
SqsRecordSchema.extend({
body: JSONStringified(bodySchema),
})
),
});

// Assess
expect(extendedSchema.parse(testEvent)).toStrictEqual({
...testEvent,
Records: [
{ ...testEvent.Records[0], body: basePayload },
{ ...testEvent.Records[1], body: basePayload },
],
});
});

it('should parse extended SnsSchema', () => {
// Prepare
const testEvent = getTestEvent<SnsEvent>({
eventsPath: '.',
filename: 'snsEvent',
});
testEvent.Records[0].Sns.Message = JSON.stringify(basePayload);

// Act
const extendedSchema = SqsSchema.extend({
Records: z.array(
SnsRecordSchema.extend({
Sns: SnsNotificationSchema.extend({
Message: JSONStringified(bodySchema),
}),
})
),
});

it('should parse extended SnsSchema', () => {
const extendedSchema = SqsSchema.extend({
Records: z.array(
SnsRecordSchema.extend({
Sns: SnsNotificationSchema.extend({
Message: JSONStringified(customSchema),
}),
})
),
});

const testEvent = getTestEvent<SnsEvent>({
eventsPath: '.',
filename: 'snsEvent',
});
testEvent.Records[0].Sns.Message = JSON.stringify(payload);

const result = extendedSchema.parse(testEvent);
expect(result).toEqual({
...testEvent,
Records: [
{
...testEvent.Records[0],
Sns: { ...testEvent.Records[0].Sns, Message: payload },
},
],
});
// Assess
expect(extendedSchema.parse(testEvent)).toStrictEqual({
...testEvent,
Records: [
{
...testEvent.Records[0],
Sns: { ...testEvent.Records[0].Sns, Message: basePayload },
},
],
});
});
});