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

Validation: conditional validators #245

Open
System-Glitch opened this issue Mar 20, 2025 · 0 comments
Open

Validation: conditional validators #245

System-Glitch opened this issue Mar 20, 2025 · 0 comments
Labels
contributions welcome Looking for contributors feature request Request for new feature implementation

Comments

@System-Glitch
Copy link
Member

System-Glitch commented Mar 20, 2025

Proposal

Sometimes we want a validator to be executed only if a certain condition is matched. For example, we want a validator on a field only if another field has a certain value.

It is currently possible but inconvenient to do so. We should introduce a new OnlyIf validator (or another more suited name if you have better ideas) that only executes a given validator if a condition is met, otherwise returns true.

Example usage:

v.OnlyIf(func(ctx *v.Context) bool {
	other := walk.MustParse("other").First(ctx.Data)
	return other.Value == true
}, MyValidator())

In this example, MyValidator would only be executed if the other field has the value true. If the condition returns false, the validation passes without executing MyValidator.

The message placeholders and other properties must be inherited from MyValidator.

@System-Glitch System-Glitch added the feature request Request for new feature implementation label Mar 20, 2025
@System-Glitch System-Glitch added the contributions welcome Looking for contributors label Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributions welcome Looking for contributors feature request Request for new feature implementation
Projects
None yet
Development

No branches or pull requests

1 participant