fastest-validator
fastest-validator copied to clipboard
Custom string validator function adds unwanted key to parent object
Adding a custom string validator function seems to have caused my other validation to stop working correctly.
I have simplified my code down and attached a link to the repo containing an example of the issue I am running into. In the example, I am using a dummy custom string validator function that should not be causing the validation to fail, however when run, the validation fails due to some of my other rules I defined in my schema. If you comment out the custom string validator function, the rest of the rules that previously failed now pass. Thus why I believe the issue seems to stem from defining a custom string validator function.
Link to example repo: https://github.com/rmattos500/fastest-validator-issue
Confirmed, for now you can just use strict: "remove" to bypass this bug,
repro
commenting out custom checker function will change the final result
const v = new Validator({
// debug: true,
useNewCustomCheckerFunction: true,
defaults: {
object: {
strict: true,
},
string: {
custom(value) {
return value;
},
},
},
});
const schema = {
method: { type: "equal", value: "bar" },
extra: "string",
};
const check = v.compile(schema);
const obj = {};
const res = check(obj);
console.log(obj);
@icebob I think we should never call custom checker function when value is null or undefined
related to #189
@erfanium Thanks for looking into this so quickly and for the suggested bypass!
The issue related to defaults?
@icebob Nah, Run my repro code to better understand it
Simpler repro:
const v = new Validator({
// debug: true,
useNewCustomCheckerFunction: true,
});
const schema = {
extra: { type: "string", custom: value => value }
};
const check = v.compile(schema);
const obj = {};
const res = check(obj);
console.log(obj);
So the issue is that FV populates the original object with extra: undefined?
Can we move this highlighted parts into the "else" branch?

@erfanium ?
Can we move this highlighted parts into the "else" branch?
I'm okay with this solution. (In fact, my solution was the same, but in another words)