From 8a88e654f7f9b2c838fc599cb20d7bd8702a0b16 Mon Sep 17 00:00:00 2001 From: maplecfiv Date: Mon, 7 Oct 2024 19:23:19 +0800 Subject: [PATCH] feat: IsHalfWidth decorator + test + readme --- README.md | 1 + spec/validators/common.test.ts | 11 +++++++++++ validators/common.ts | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index d23efe2..fe155fc 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ | `IsRegex(regex: RegExp)` | Checks if the string match the given RegExp. | | `IsEmail()` | Checks if the string is an email. | | `IsPhoneNumber(country: string)` | Checks if the string is a local phone number for a country. Country must be ISO 3166 c.a.d 2 letter code. Example: FR, US, UK, DE | +| `IsHalfWidth()` | Checks if the string is half-width string. Example: A, b, c. | | `IsInternationalPhoneNumber()` | Checks if the string is an international phone number with extension and with or without leading 0. Example: +330XXXXXXXXX or +33XXXXXXXXX | | `IsLowerCase()` | Checks if the string is in lower case. | | `IsUpperCase()` | Checks if the string is in uppercase. | diff --git a/spec/validators/common.test.ts b/spec/validators/common.test.ts index 126207e..d50f0f9 100644 --- a/spec/validators/common.test.ts +++ b/spec/validators/common.test.ts @@ -20,6 +20,7 @@ import { IsDecimal, IsDivisibleBy, IsEmpty, + IsHalfWidth, IsHexColor, IsIP, IsLowerCase, @@ -81,6 +82,8 @@ class BodyPayload { @IsLowerCase() public isLowerCase!: string; + @IsHalfWidth() + public isHalfWidth!: string; @IsHexColor() public IsHexColor!: string; @IsIP() @@ -117,6 +120,7 @@ Deno.test('Common validators errors', async (ctx) => { failingPayload.isDecimal = 'nondecimal'; failingPayload.isEmpty = 'nonEmpty'; failingPayload.isLowerCase = 'UPPERCASE'; + failingPayload.isHalfWidth = 'Hello'; failingPayload.IsHexColor = 'nonHexcolor'; failingPayload.isIP = '192.168.0.256'; failingPayload.isDivisibleBy = '7'; @@ -277,6 +281,13 @@ Deno.test('Common validators errors', async (ctx) => { property: 'isLowerCase', }]); }); + await ctx.step('IsHalfWidth', () => { + assertArrayIncludes(errors, [{ + errorMessage: `Property must be a half-width string`, + constraints: [], + property: 'isHalfWidth', + }]); + }); await ctx.step('IsHexColor', () => { assertArrayIncludes(errors, [{ errorMessage: `Property must be a hexcolor string`, diff --git a/validators/common.ts b/validators/common.ts index 2f9e98c..3ad6063 100644 --- a/validators/common.ts +++ b/validators/common.ts @@ -18,6 +18,7 @@ import { isDecimal, isDivisibleBy, isEmpty, + isHalfWidth, isHexColor, isIP, isLowerCase, @@ -212,6 +213,15 @@ export function IsLowerCase(): PropertyDecorator { constraints: [], }); } +/** + * Decorator that checks if the property is a half-width string. + */ +export function IsHalfWidth(): PropertyDecorator { + return createDecorator((prop: string) => isHalfWidth(prop), { + errorMessage: 'Property must be a half-width string', + constraints: [], + }); +} /** * Decorator that checks if the property is a hex color string. */