From 71acb174a505775d0cf1cad7a0377afdf03f9a2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hector=20G=C3=B3mez=20Varela?= Date: Fri, 22 Nov 2024 15:39:31 +0100 Subject: [PATCH 1/3] Add CreateAddressBookItemDto schema tests --- ...reate-address-book-item.dto.entity.spec.ts | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts diff --git a/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts b/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts new file mode 100644 index 0000000000..2c0449a6d4 --- /dev/null +++ b/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts @@ -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 an 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 Books items names must be at least 3 characters long', + minimum: 3, + path: ['name'], + type: 'string', + }, + ]); + }); + + it('should not verify an 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 an 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 Books items names must be at most 50 characters long', + maximum: 50, + path: ['name'], + type: 'string', + }, + ]); + }); + + it('should not verify an 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 Books items 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 an 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 an 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), + ); + }); +}); From 9797af083f8a5ee94262cc9674fb3dc3334c4e57 Mon Sep 17 00:00:00 2001 From: James Mealy Date: Mon, 23 Dec 2024 10:07:54 +0000 Subject: [PATCH 2/3] Change address book item name validation error message --- ...create-address-book-item.dto.entity.spec.ts | 18 +++++++++--------- .../address-book-item-name.schema.spec.ts | 6 +++--- .../schemas/address-book-item-name.schema.ts | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts b/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts index 2c0449a6d4..97f5e291ca 100644 --- a/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts +++ b/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts @@ -14,7 +14,7 @@ describe('CreateAddressBookItemDtoSchema', () => { expect(result.success).toBe(true); }); - it('should not verify an CreateAddressBookItemDto with a shorter name', () => { + it('should not verify a CreateAddressBookItemDto with a shorter name', () => { const createAddressBookItemDto = createAddressBookItemDtoBuilder() .with('name', faker.string.alphanumeric({ length: 2 })) .build(); @@ -28,7 +28,7 @@ describe('CreateAddressBookItemDtoSchema', () => { code: 'too_small', inclusive: true, exact: false, - 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: ['name'], type: 'string', @@ -36,7 +36,7 @@ describe('CreateAddressBookItemDtoSchema', () => { ]); }); - it('should not verify an CreateAddressBookItemDto with a number name', () => { + it('should not verify a CreateAddressBookItemDto with a number name', () => { const createAddressBookItemDto = createAddressBookItemDtoBuilder() // @ts-expect-error - should be strings .with('name', faker.number.int()) @@ -57,7 +57,7 @@ describe('CreateAddressBookItemDtoSchema', () => { ]); }); - it('should not verify an CreateAddressBookItemDto with a longer name', () => { + it('should not verify a CreateAddressBookItemDto with a longer name', () => { const createAddressBookItemDto = createAddressBookItemDtoBuilder() .with('name', faker.string.alphanumeric({ length: 51 })) .build(); @@ -71,7 +71,7 @@ describe('CreateAddressBookItemDtoSchema', () => { code: 'too_big', inclusive: true, exact: false, - 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: ['name'], type: 'string', @@ -79,7 +79,7 @@ describe('CreateAddressBookItemDtoSchema', () => { ]); }); - it('should not verify an CreateAddressBookItemDto with a malformed name', () => { + it('should not verify a CreateAddressBookItemDto with a malformed name', () => { const createAddressBookItemDto = createAddressBookItemDtoBuilder() .with('name', '////') .build(); @@ -92,14 +92,14 @@ describe('CreateAddressBookItemDtoSchema', () => { { 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 ca Contain alphanumeric characters, periods, underscores, or hyphens', path: ['name'], validation: 'regex', }, ]); }); - it('should not verify an CreateAddressBookItemDto with a malformed address', () => { + it('should not verify a CreateAddressBookItemDto with a malformed address', () => { const createAddressBookItemDto = createAddressBookItemDtoBuilder() .with('address', '0x123') .build(); @@ -117,7 +117,7 @@ describe('CreateAddressBookItemDtoSchema', () => { ]); }); - it('should checksum the address of an CreateAddressBookItemDto', () => { + it('should checksum the address of a CreateAddressBookItemDto', () => { const createAddressBookItemDto = createAddressBookItemDtoBuilder().build(); // @ts-expect-error - address should be `0x${string}` createAddressBookItemDto.address = diff --git a/src/domain/accounts/address-books/entities/schemas/address-book-item-name.schema.spec.ts b/src/domain/accounts/address-books/entities/schemas/address-book-item-name.schema.spec.ts index 034d6e31b5..05f7c1fe85 100644 --- a/src/domain/accounts/address-books/entities/schemas/address-book-item-name.schema.spec.ts +++ b/src/domain/accounts/address-books/entities/schemas/address-book-item-name.schema.spec.ts @@ -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: [], }, ]), @@ -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: [], }, ]), @@ -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: [], }, ]), diff --git a/src/domain/accounts/address-books/entities/schemas/address-book-item-name.schema.ts b/src/domain/accounts/address-books/entities/schemas/address-book-item-name.schema.ts index 975c44b748..299207078d 100644 --- a/src/domain/accounts/address-books/entities/schemas/address-book-item-name.schema.ts +++ b/src/domain/accounts/address-books/entities/schemas/address-book-item-name.schema.ts @@ -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', }); From 0b2f3f57448fde7b2c1e6cfb10ffc4198770c2a6 Mon Sep 17 00:00:00 2001 From: James Mealy Date: Mon, 23 Dec 2024 10:21:12 +0000 Subject: [PATCH 3/3] Update remaining tests with new error message --- .../address-books/entities/address-book.entity.spec.ts | 6 +++--- .../entities/create-address-book-item.dto.entity.spec.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/domain/accounts/address-books/entities/address-book.entity.spec.ts b/src/domain/accounts/address-books/entities/address-book.entity.spec.ts index 62a8ef05c8..f93a2413cc 100644 --- a/src/domain/accounts/address-books/entities/address-book.entity.spec.ts +++ b/src/domain/accounts/address-books/entities/address-book.entity.spec.ts @@ -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', @@ -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', @@ -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', }, diff --git a/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts b/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts index 97f5e291ca..c2901bc2dd 100644 --- a/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts +++ b/src/domain/accounts/address-books/entities/create-address-book-item.dto.entity.spec.ts @@ -92,7 +92,7 @@ describe('CreateAddressBookItemDtoSchema', () => { { code: 'invalid_string', message: - 'Address book entry names must start with a letter or number and ca 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: ['name'], validation: 'regex', },