diff --git a/README.md b/README.md index 273e784..b55c296 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,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 780d170..7ca8cf6 100644 --- a/spec/validators/common.test.ts +++ b/spec/validators/common.test.ts @@ -20,6 +20,7 @@ import { IsDecimal, IsDivisibleBy, IsEmpty, + IsHalfWidth, IsHexadecimal, IsHexColor, IsIP, @@ -82,6 +83,8 @@ class BodyPayload { @IsLowerCase() public isLowerCase!: string; + @IsHalfWidth() + public isHalfWidth!: string; @IsHexColor() public IsHexColor!: string; @IsHexadecimal() @@ -120,6 +123,7 @@ Deno.test('Common validators errors', async (ctx) => { failingPayload.isDecimal = 'nondecimal'; failingPayload.isEmpty = 'nonEmpty'; failingPayload.isLowerCase = 'UPPERCASE'; + failingPayload.isHalfWidth = 'Hello'; failingPayload.IsHexColor = 'nonHexcolor'; failingPayload.isHexadecimal = 'GG'; failingPayload.isIP = '192.168.0.256'; @@ -281,6 +285,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 688ea48..de7e313 100644 --- a/validators/common.ts +++ b/validators/common.ts @@ -18,6 +18,7 @@ import { isDecimal, isDivisibleBy, isEmpty, + isHalfWidth, isHexadecimal, isHexColor, isIP, @@ -213,6 +214,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. */