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

BingInt validator not exist

Open Rober9614 opened this issue 3 years ago • 4 comments

Description

Its necessary create BingInt validator. This validator its necessary allow string or numbers, its necessary because its posible that existe BigInt type:

  •  - "20221301083605n"
    
  •  - "20221301083605"
    
  •  -  20221301083605
    

Only its required validate if input its number or string, but only if type is BigInt

Proposed solution

Code of solution:


import { ValidationOptions } from '../ValidationOptions';
import { buildMessage, ValidateBy } from '../common/ValidateBy';
import matchesValidator from 'validator/lib/matches';


export const IS_BIGINT = 'isBigInt';

/**
 * Checks if the value is a BingInt
 */
export function isBigInt(value: number | string): boolean {
    return (typeof value === 'number' || typeof value === 'string') && (matchesValidator(`${value}`, /^[0-9]+$/) || matchesValidator(`${value}`, /^[0-9]+n+$/));
}

/**
 * Checks if the value is a BingInt
 * BigInt => simple number or string with 'n'
 * Ej: 
 *      - "20221301083605n"
 *      - "20221301083605"
 *      -  20221301083605
 */
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
    );
}

Rober9614 avatar Jan 13 '22 16:01 Rober9614

I would suggest you apply code formatting to your issue like this:

```ts
const myconst : number = 1234;
```

Which results in this:

const myconst : number = 1234;

Also, I suppose there is a discussion to be held whether or not this feature request belongs in https://github.com/validatorjs/validator.js, which powers the validation in this project

braaar avatar Jan 14 '22 09:01 braaar

For anyone looking for a quick workaround right now, best is to define your custom decorator. Can refer to https://github.com/pigulla/class-validator-extended/blob/main/src/bigint/is-bigint/is-bigint.decorator.ts on how to make one.

Also, I suppose there is a discussion to be held whether or not this feature request belongs in https://github.com/validatorjs/validator.js, which powers the validation in this project

@braaar I was also curious whether you guys had the discussion?

rohanrajpal avatar Jul 27 '22 11:07 rohanrajpal

@braaar I was also curious whether you guys had the discussion?

I have not brought it up in validator.js, and I haven't been paying special attention to whether or not @Rober9614 has done so either. Perhaps you could make an issue about it there, @rohanrajpal?

Without looking very much into it I suspect that this functionality could be implemented as an option in an existing validator in that project. Could the options in isInt be of use? I'm not used to working with BigInts.

braaar avatar Aug 01 '22 08:08 braaar

@braaar LTM like validator.js is already good w/BigInt:

> const bi = BigInt(Number.MAX_SAFE_INTEGER) * 3n;
undefined
> validator.isInt(String(bi));
true

In-contrast, class-validator's @IsInt (as-well-as @IsNegative, @IsPositive, etc) all check for type number.

fauxbytes avatar Jul 28 '23 05:07 fauxbytes