Skip to content

feat: add IsBigInt decorator #2506

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

Open
wants to merge 3 commits into
base: develop
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 @@ -811,6 +811,7 @@ isBoolean(value);
| `@IsString()` | Checks if the value is a string. |
| `@IsNumber(options: IsNumberOptions)` | Checks if the value is a number. |
| `@IsInt()` | Checks if the value is an integer number. |
| `@IsBigInt()` | Checks if the value is a bigint number. |
| `@IsArray()` | Checks if the value is an array |
| `@IsEnum(entity: object)` | Checks if the value is a valid enum |
| **Number validation decorators** |
Expand Down
1 change: 1 addition & 0 deletions src/decorator/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export * from './string/is-iso4217-currency-code';
// Type checkers
// -------------------------------------------------------------------------

export * from './typechecker/IsBigInt';
export * from './typechecker/IsBoolean';
export * from './typechecker/IsDate';
export * from './typechecker/IsNumber';
Expand Down
27 changes: 27 additions & 0 deletions src/decorator/typechecker/IsBigInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ValidationOptions } from '../ValidationOptions';
import { buildMessage, ValidateBy } from '../common/ValidateBy';

export const IS_BIGINT = 'isBigInt';

/**
* Checks if the value is a bigint
*/
export function isBigInt(val: unknown): val is bigint {
return typeof val === 'bigint';
}

/**
* Checks if the value is a bigint
*/
export function IsBigInt(validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy(
{
name: IS_BIGINT,
validator: {
validate: (value, args): boolean => isBigInt(value),
defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be a bigint number', validationOptions),
},
},
validationOptions
);
}
36 changes: 36 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
ArrayNotContains,
ArrayUnique,
IsArray,
IsBigInt,
IsDateString,
IsInstance,
IsPhoneNumber,
Expand All @@ -79,6 +80,7 @@ import {
isDefined,
isNumber,
isURL,
isBigInt,
isBoolean,
isString,
isInt,
Expand Down Expand Up @@ -757,6 +759,40 @@ describe('IsInt', () => {
});
});

describe('IsBigInt', () => {
// By casting bigints via function instead of `2n` annotation we can avoid
// to bump the typescript build target from es2018 to es2020
const validValues = [BigInt(2), BigInt(4), BigInt(100), BigInt(1000)];
const invalidValues = ['01', '-01', '000', '100e10', '123.123', ' ', '', 10, 2.5, -0.1];

class MyClass {
@IsBigInt()
someProperty: string;
}

it('should not fail if validator.validate said that its valid', () => {
return checkValidValues(new MyClass(), validValues);
});

it('should fail if validator.validate said that its invalid', () => {
return checkInvalidValues(new MyClass(), invalidValues);
});

it('should not fail if method in validator said that its valid', () => {
validValues.forEach(value => expect(isBigInt(value)).toBeTruthy());
});

it('should fail if method in validator said that its invalid', () => {
invalidValues.forEach(value => expect(isBigInt(value as any)).toBeFalsy());
});

it('should return error object with proper data', () => {
const validationType = 'isBigInt';
const message = 'someProperty must be a bigint number';
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
});
});

describe('IsString', () => {
const validValues = ['true', 'false', 'hello', '0', '', '1'];
const invalidValues = [true, false, 1, 2, null, undefined];
Expand Down