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

Add CreateAddressBookItemDto schema tests #2151

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('AddressBookSchema', () => {
code: 'too_small',
exact: false,
inclusive: true,
message: 'Address Books items names must be at least 3 characters long',
message: 'Address book entry names must be at least 3 characters long',
minimum: 3,
path: ['data', 0, 'name'],
type: 'string',
Expand All @@ -137,7 +137,7 @@ describe('AddressBookSchema', () => {
code: 'too_big',
exact: false,
inclusive: true,
message: 'Address Books items names must be at most 50 characters long',
message: 'Address book entry names must be at most 50 characters long',
maximum: 50,
path: ['data', 0, 'name'],
type: 'string',
Expand All @@ -155,7 +155,7 @@ describe('AddressBookSchema', () => {
{
code: 'invalid_string',
message:
'Address Books items names must start with a letter or number and can contain alphanumeric characters, periods, underscores, or hyphens',
'Address book entry names must start with a letter or number and can contain alphanumeric characters, periods, underscores, or hyphens',
path: ['data', 0, 'name'],
validation: 'regex',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { createAddressBookItemDtoBuilder } from '@/domain/accounts/address-books/entities/__tests__/create-address-book-item.dto.builder';
import { CreateAddressBookItemDtoSchema } from '@/domain/accounts/address-books/entities/create-address-book-item.dto.entity';
import { faker } from '@faker-js/faker/.';
import { getAddress } from 'viem';

describe('CreateAddressBookItemDtoSchema', () => {
it('should verify a CreateAddressBookItemDto', () => {
const createAddressBookItemDto = createAddressBookItemDtoBuilder().build();

const result = CreateAddressBookItemDtoSchema.safeParse(
createAddressBookItemDto,
);

expect(result.success).toBe(true);
});

it('should not verify a CreateAddressBookItemDto with a shorter name', () => {
const createAddressBookItemDto = createAddressBookItemDtoBuilder()
.with('name', faker.string.alphanumeric({ length: 2 }))
.build();

const result = CreateAddressBookItemDtoSchema.safeParse(
createAddressBookItemDto,
);

expect(!result.success && result.error.issues).toStrictEqual([
{
code: 'too_small',
inclusive: true,
exact: false,
message: 'Address book entry names must be at least 3 characters long',
minimum: 3,
path: ['name'],
type: 'string',
},
]);
});

it('should not verify a CreateAddressBookItemDto with a number name', () => {
const createAddressBookItemDto = createAddressBookItemDtoBuilder()
// @ts-expect-error - should be strings
.with('name', faker.number.int())
.build();

const result = CreateAddressBookItemDtoSchema.safeParse(
createAddressBookItemDto,
);

expect(!result.success && result.error.issues).toStrictEqual([
{
code: 'invalid_type',
expected: 'string',
message: 'Expected string, received number',
path: ['name'],
received: 'number',
},
]);
});

it('should not verify a CreateAddressBookItemDto with a longer name', () => {
const createAddressBookItemDto = createAddressBookItemDtoBuilder()
.with('name', faker.string.alphanumeric({ length: 51 }))
.build();

const result = CreateAddressBookItemDtoSchema.safeParse(
createAddressBookItemDto,
);

expect(!result.success && result.error.issues).toStrictEqual([
{
code: 'too_big',
inclusive: true,
exact: false,
message: 'Address book entry names must be at most 50 characters long',
maximum: 50,
path: ['name'],
type: 'string',
},
]);
});

it('should not verify a CreateAddressBookItemDto with a malformed name', () => {
const createAddressBookItemDto = createAddressBookItemDtoBuilder()
.with('name', '////')
.build();

const result = CreateAddressBookItemDtoSchema.safeParse(
createAddressBookItemDto,
);

expect(!result.success && result.error.issues).toStrictEqual([
{
code: 'invalid_string',
message:
'Address book entry names must start with a letter or number and can contain alphanumeric characters, periods, underscores, or hyphens',
path: ['name'],
validation: 'regex',
},
]);
});

it('should not verify a CreateAddressBookItemDto with a malformed address', () => {
const createAddressBookItemDto = createAddressBookItemDtoBuilder()
.with('address', '0x123')
.build();

const result = CreateAddressBookItemDtoSchema.safeParse(
createAddressBookItemDto,
);

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

it('should checksum the address of a CreateAddressBookItemDto', () => {
const createAddressBookItemDto = createAddressBookItemDtoBuilder().build();
// @ts-expect-error - address should be `0x${string}`
createAddressBookItemDto.address =
createAddressBookItemDto.address.toLowerCase();

const result = CreateAddressBookItemDtoSchema.safeParse(
createAddressBookItemDto,
);

expect(result.success && result.data.address).toBe(
getAddress(createAddressBookItemDto.address),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('AddressBookItemNameSchema', () => {
inclusive: true,
exact: false,
message:
'Address Books items names must be at least 3 characters long',
'Address book entry names must be at least 3 characters long',
path: [],
},
]),
Expand All @@ -80,7 +80,7 @@ describe('AddressBookItemNameSchema', () => {
inclusive: true,
exact: false,
message:
'Address Books items names must be at most 50 characters long',
'Address book entry names must be at most 50 characters long',
path: [],
},
]),
Expand All @@ -99,7 +99,7 @@ describe('AddressBookItemNameSchema', () => {
validation: 'regex',
code: 'invalid_string',
message:
'Address Books items names must start with a letter or number and can contain alphanumeric characters, periods, underscores, or hyphens',
'Address book entry names must start with a letter or number and can contain alphanumeric characters, periods, underscores, or hyphens',
path: [],
},
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { z } from 'zod';
export const AddressBookItemNameSchema = z
.string()
.min(3, {
message: 'Address Books items names must be at least 3 characters long',
message: 'Address book entry names must be at least 3 characters long',
})
.max(50, {
message: 'Address Books items names must be at most 50 characters long',
message: 'Address book entry names must be at most 50 characters long',
})
.regex(/^[a-zA-Z0-9]+(?:[._-][a-zA-Z0-9]+)*$/, {
message:
'Address Books items names must start with a letter or number and can contain alphanumeric characters, periods, underscores, or hyphens',
'Address book entry names must start with a letter or number and can contain alphanumeric characters, periods, underscores, or hyphens',
});
Loading