Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: IsFullWidth decorator + test + readme #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -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. |
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,
IsFullWidth,
IsHexColor,
IsIP,
IsLowerCase,
Expand Down Expand Up @@ -78,6 +79,8 @@ class BodyPayload {

@IsEmpty()
public isEmpty!: string;
@IsFullWidth()
public isFullWidth!: string;

@IsLowerCase()
public isLowerCase!: string;
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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`,
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,
isFullWidth,
isHexColor,
isIP,
isLowerCase,
Expand Down Expand Up @@ -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.
*/
Expand Down
Loading