typestack / class-validator

Decorator-based property validation for classes.
MIT License
10.86k stars 785 forks source link

feature: @isBigInt #1255

Open SamuelScheit opened 3 years ago

SamuelScheit commented 3 years ago

Additionally to IsInt add a @IsBigInt decorator

ales-albert-kilbergr commented 1 month ago

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.