Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
11 changes: 11 additions & 0 deletions spec/validators/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
IsDecimal,
IsDivisibleBy,
IsEmpty,
IsHalfWidth,
IsHexadecimal,
IsHexColor,
IsIP,
Expand Down Expand Up @@ -82,6 +83,8 @@ class BodyPayload {

@IsLowerCase()
public isLowerCase!: string;
@IsHalfWidth()
public isHalfWidth!: string;
@IsHexColor()
public IsHexColor!: string;
@IsHexadecimal()
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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`,
Expand Down
10 changes: 10 additions & 0 deletions validators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isDecimal,
isDivisibleBy,
isEmpty,
isHalfWidth,
isHexadecimal,
isHexColor,
isIP,
Expand Down Expand Up @@ -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.
*/
Expand Down