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

Migrate RelayLegacyDto to zod #1362

Merged
merged 3 commits into from
Apr 5, 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
18 changes: 15 additions & 3 deletions src/routes/relay/entities/relay.legacy.dto.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
export class RelayLegacyDto {
import { AddressSchema } from '@/validation/entities/schemas/address.schema';
import { HexSchema } from '@/validation/entities/schemas/hex.schema';
import { NumericStringSchema } from '@/validation/entities/schemas/numeric-string.schema';
import { z } from 'zod';

export class RelayLegacyDto implements z.infer<typeof RelayLegacyDtoSchema> {
chainId!: string;
to!: string;
data!: string;
to!: `0x${string}`;
data!: `0x${string}`;
gasLimit!: string | null;
}

export const RelayLegacyDtoSchema = z.object({
chainId: NumericStringSchema,
to: AddressSchema,
data: HexSchema,
gasLimit: z.string().nullish().default(null),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { RelayLegacyDtoSchema } from '@/routes/relay/entities/relay.legacy.dto.entity';
import { faker } from '@faker-js/faker';
import { getAddress } from 'viem';

describe('RelayLegacyDtoSchema', () => {
it('should validate a valid legacy relay DTO with a gasLimit', () => {
const relayLegacyDto = {
chainId: faker.string.numeric(),
to: getAddress(faker.finance.ethereumAddress()),
data: faker.string.hexadecimal(),
gasLimit: faker.string.numeric(),
};

const result = RelayLegacyDtoSchema.safeParse(relayLegacyDto);

expect(result.success && result.data.chainId).toBe(relayLegacyDto.chainId);
expect(result.success && result.data.to).toBe(relayLegacyDto.to);
expect(result.success && result.data.data).toBe(relayLegacyDto.data);
expect(result.success && result.data.gasLimit).toBe(
relayLegacyDto.gasLimit,
);
});

it('should validate a valid legacy relay DTO without a gasLimit and coerce it to null', () => {
const relayLegacyDto = {
chainId: faker.string.numeric(),
to: getAddress(faker.finance.ethereumAddress()),
data: faker.string.hexadecimal(),
};

const result = RelayLegacyDtoSchema.safeParse(relayLegacyDto);

expect(result.success && result.data.chainId).toBe(relayLegacyDto.chainId);
expect(result.success && result.data.to).toBe(relayLegacyDto.to);
expect(result.success && result.data.data).toBe(relayLegacyDto.data);
expect(result.success && result.data.gasLimit).toBeNull(); // Coerced to null
});

it('should checksum a non-checksummed to address', () => {
const relayLegacyDto = {
chainId: faker.string.numeric(),
to: faker.finance.ethereumAddress().toLowerCase(),
data: faker.string.hexadecimal(),
};

const result = RelayLegacyDtoSchema.safeParse(relayLegacyDto);

expect(result.success && result.data.to).toBe(
getAddress(relayLegacyDto.to),
);
});

it('should throw for a non-numeric chainId', () => {
const relayLegacyDto = {
chainId: faker.string.alphanumeric(),
to: getAddress(faker.finance.ethereumAddress()),
data: faker.string.hexadecimal(),
};

const result = RelayLegacyDtoSchema.safeParse(relayLegacyDto);

expect(!result.success && result.error.issues).toStrictEqual([
{
code: 'custom',
message: 'Invalid input',
path: ['chainId'],
},
]);
});

it('should throw for a non-address to address', () => {
const relayLegacyDto = {
chainId: faker.string.numeric(),
to: faker.string.numeric(),
data: faker.string.hexadecimal(),
};

const result = RelayLegacyDtoSchema.safeParse(relayLegacyDto);

expect(!result.success && result.error.issues).toStrictEqual([
{
code: 'custom',
message: 'Invalid input',
path: ['to'],
},
]);
});

it('should throw for non-hex data', () => {
const relayLegacyDto = {
chainId: faker.string.numeric(),
to: getAddress(faker.finance.ethereumAddress()),
data: faker.string.numeric(),
};

const result = RelayLegacyDtoSchema.safeParse(relayLegacyDto);

expect(!result.success && result.error.issues).toStrictEqual([
{
code: 'custom',
message: 'Invalid input',
path: ['data'],
},
]);
});

it('should throw for an invalid gasLimit', () => {
const relayLegacyDto = {
chainId: faker.string.numeric(),
to: getAddress(faker.finance.ethereumAddress()),
data: faker.string.hexadecimal(),
gasLimit: faker.number.int(),
};

const result = RelayLegacyDtoSchema.safeParse(relayLegacyDto);

expect(!result.success && result.error.issues).toStrictEqual([
{
code: 'invalid_type',
expected: 'string',
message: 'Expected string, received number',
path: ['gasLimit'],
received: 'number',
},
]);
});
});
17 changes: 0 additions & 17 deletions src/routes/relay/entities/schemas/relay.legacy.dto.schema.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/routes/relay/pipes/relay.legacy.validation.pipe.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/routes/relay/relay.legacy.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RelayLegacyDto } from '@/routes/relay/entities/relay.legacy.dto.entity';
import { RelayLegacyDtoValidationPipe } from '@/routes/relay/pipes/relay.legacy.validation.pipe';
import { RelayLegacyDtoSchema } from '@/routes/relay/entities/relay.legacy.dto.entity';
import { ValidationPipe } from '@/validation/pipes/validation.pipe';
import {
Body,
Controller,
Expand All @@ -18,7 +19,7 @@ export class RelayLegacyController {
@Post()
@Redirect(undefined, HttpStatus.PERMANENT_REDIRECT)
relay(
@Body(RelayLegacyDtoValidationPipe)
@Body(new ValidationPipe(RelayLegacyDtoSchema))
relayLegacyDto: RelayLegacyDto,
): { url: string } {
return { url: `/v1/chains/${relayLegacyDto.chainId}/relay` };
Expand Down