Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom string validator function adds unwanted key to parent object #271

Open
rmattos500 opened this issue Sep 8, 2021 · 9 comments
Open
Labels

Comments

@rmattos500
Copy link

rmattos500 commented Sep 8, 2021

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

@erfanium
Copy link
Collaborator

erfanium commented Sep 8, 2021

Confirmed, for now you can just use strict: "remove" to bypass this bug,

@erfanium erfanium added the bug label Sep 8, 2021
@erfanium
Copy link
Collaborator

erfanium commented Sep 8, 2021

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);

@erfanium erfanium changed the title Adding custom string validator function breaks validation Custom string validator function adds unwanted key to parent object Sep 8, 2021
@erfanium
Copy link
Collaborator

erfanium commented Sep 8, 2021

@icebob I think we should never call custom checker function when value is null or undefined

related to #189

@rmattos500
Copy link
Author

@erfanium Thanks for looking into this so quickly and for the suggested bypass!

@icebob
Copy link
Owner

icebob commented Sep 9, 2021

The issue related to defaults?

@erfanium
Copy link
Collaborator

@icebob Nah, Run my repro code to better understand it

@icebob
Copy link
Owner

icebob commented Sep 13, 2021

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?
image

@icebob
Copy link
Owner

icebob commented Sep 25, 2021

@erfanium ?

@erfanium
Copy link
Collaborator

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)

0x0a0d added a commit to 0x0a0d/fastest-validator that referenced this issue Mar 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants