diff --git a/README.md b/README.md index ccb8edf..cc6a6a0 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,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 | | `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 | diff --git a/spec/validators/common.test.ts b/spec/validators/common.test.ts index d40f5e6..cf366e6 100644 --- a/spec/validators/common.test.ts +++ b/spec/validators/common.test.ts @@ -20,6 +20,7 @@ import { IsDecimal, IsDivisibleBy, IsEmpty, + IsFullWidth, IsHalfWidth, IsHexadecimal, IsHexColor, @@ -81,6 +82,8 @@ class BodyPayload { @IsEmpty() public isEmpty!: string; + @IsFullWidth() + public isFullWidth!: string; @IsLowerCase() public isLowerCase!: string; @@ -126,6 +129,7 @@ Deno.test('Common validators errors', async (ctx) => { failingPayload.isDate = 'nonDate'; failingPayload.isDecimal = 'nondecimal'; failingPayload.isEmpty = 'nonEmpty'; + failingPayload.isFullWidth = 'hello'; failingPayload.isLowerCase = 'UPPERCASE'; failingPayload.isHalfWidth = 'Hello'; failingPayload.IsHexColor = 'nonHexcolor'; @@ -283,6 +287,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 a65fb87..3d767e5 100644 --- a/validators/common.ts +++ b/validators/common.ts @@ -18,6 +18,7 @@ import { isDecimal, isDivisibleBy, isEmpty, + isFullWidth, isHalfWidth, isHexadecimal, isHexColor, @@ -198,6 +199,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. */