From 13c6872ef11f369511462017b268c89561808c87 Mon Sep 17 00:00:00 2001 From: maplecfiv Date: Tue, 8 Oct 2024 20:52:11 +0800 Subject: [PATCH] feat: IsFullWidth 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..0737e7b 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ | `LengthLower(num: number)` | Checks if the length is lower than the specified number. | | `IsRegex(regex: RegExp)` | Checks if the string match the given RegExp. | | `IsEmail()` | Checks if the string is an email. | +| `IsFullWidth()` | Checks if the string is a full-width string. Example a, b, c. | | `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 | | `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. | diff --git a/spec/validators/common.test.ts b/spec/validators/common.test.ts index 126207e..e4d49ca 100644 --- a/spec/validators/common.test.ts +++ b/spec/validators/common.test.ts @@ -20,6 +20,7 @@ import { IsDecimal, IsDivisibleBy, IsEmpty, + IsFullWidth, IsHexColor, IsIP, IsLowerCase, @@ -78,6 +79,8 @@ class BodyPayload { @IsEmpty() public isEmpty!: string; + @IsFullWidth() + public isFullWidth!: string; @IsLowerCase() public isLowerCase!: string; @@ -116,6 +119,7 @@ Deno.test('Common validators errors', async (ctx) => { failingPayload.isDate = 'nonDate'; failingPayload.isDecimal = 'nondecimal'; failingPayload.isEmpty = 'nonEmpty'; + failingPayload.isFullWidth = 'hello'; failingPayload.isLowerCase = 'UPPERCASE'; failingPayload.IsHexColor = 'nonHexcolor'; failingPayload.isIP = '192.168.0.256'; @@ -270,6 +274,13 @@ Deno.test('Common validators errors', async (ctx) => { property: 'isEmpty', }]); }); + await ctx.step('IsFullWidth', () => { + assertArrayIncludes(errors, [{ + errorMessage: `Property must be a full-width string`, + constraints: [], + property: 'isFullWidth', + }]); + }); await ctx.step('IsLowerCase', () => { assertArrayIncludes(errors, [{ errorMessage: `Property must be a string in lower case`, diff --git a/validators/common.ts b/validators/common.ts index 2f9e98c..b60c07b 100644 --- a/validators/common.ts +++ b/validators/common.ts @@ -18,6 +18,7 @@ import { isDecimal, isDivisibleBy, isEmpty, + isFullWidth, isHexColor, isIP, isLowerCase, @@ -194,6 +195,15 @@ export function IsDecimal(options = defaultDecimalOptions): PropertyDecorator { constraints: [options], }); } +/** + * Decorator that checks if the property is full-width. + */ +export function IsFullWidth(): PropertyDecorator { + return createDecorator((prop: string) => isFullWidth(prop), { + errorMessage: `Property must be a full-width string`, + constraints: [], + }); +} /** * Decorator that checks if the property is empty. */