class-validator
class-validator copied to clipboard
feature: @isBigInt
Additionally to IsInt add a @IsBigInt decorator
Hi. I would also benefit from the @isBigInt decorator.
The IsBigint decorator would check only if a given value is a type of a bigint, the same way as the IsInt decorator only checks if the value is an integer.
Here is an example of an implementation I'm currently using in my projects:
import {
buildMessage,
ValidateBy,
ValidationOptions
} from 'class-validator';
export const IS_BIGINT = 'isBigInt';
/**
* Checks if the value is a bigint.
*/
export function isBigInt(val: unknown): val is Number {
return typeof value === 'bigint';
}
/**
* Checks if the value is a bigintt
*/
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',
validationOptions
),
},
},
validationOptions
);
}
I'm ready to create a PR with the implementation.