You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Yeah this is due to js considering null a valid value and instead of providing {} it is providing null, we can do something like this, happy to open a PR with it
export function IsNumber(options: IsNumberOptions = {}, validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy(
{
name: IS_NUMBER,
constraints: [getValidationOptions(options)],
validator: {
validate: (value, args): boolean => isNumber(value, args?.constraints[0]),
defaultMessage: buildMessage(
eachPrefix => eachPrefix + '$property must be a number conforming to the specified constraints',
validationOptions
),
},
},
validationOptions
);
}
const getDecoratorOptions = <T>(decoratorOptions: T) => {
if (!decoratorOptions) {
return {} as T;
}
return decoratorOptions;
}
import { IsNumber, Validator } from '../../src';
const validator = new Validator();
describe('typechecker validation', () => {
it('should not throw on error when null is provided', () => {
class MyClass {
@IsNumber(null, { each: true })
someNumber: number;
constructor(someNumber: number) {
this.someNumber = someNumber;
}
}
const model = new MyClass(9);
return validator.validate(model).then(errors => {
expect(errors.length).toEqual(0);
});
});
});
```
Hello,
This week, one of my team member lost time debugging because he had an error due to wrong usage of IsNumber
On an array of numbers property he put the validator:
When the first argument is supposed to be an object:
Is there a way to make sure the validator does not throw an error in that case?
The issue is on the line 31: if (options.maxDecimalPlaces !== undefined)
Thank you
The text was updated successfully, but these errors were encountered: