class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

feature: @isBigInt

Open samuelscheit opened this issue 4 years ago • 2 comments

Additionally to IsInt add a @IsBigInt decorator

samuelscheit avatar Aug 22 '21 18:08 samuelscheit

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.

ales-albert-kilbergr avatar Jul 16 '24 08:07 ales-albert-kilbergr