typestack / class-validator

Decorator-based property validation for classes.
MIT License
10.89k stars 786 forks source link

BingInt validator not exist #1491

Closed Rober9614 closed 2 months ago

Rober9614 commented 2 years ago

Description

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

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
    );
}
braaar commented 2 years ago

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

```ts
const myconst : number = 1234;

Which results in this:
```ts
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

rohanrajpal commented 2 years ago

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?

braaar commented 2 years ago

@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.

fauxbytes commented 1 year ago

@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.

github-actions[bot] commented 1 month ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.