Replace validation Group by a validation context
See original GitHub issueMy project should be available for many country and validation rules can be different for each contry.
For exemple:
public class User {
public String firstName;
public String lastName;
private static final ValidatorBuilder<User> VALIDATOR = ValidatorBuilder.<User>of()
.constraint(User::getFirstName, "firstName", Constraint::notNull)
. constraintOnCondition((user, group) -> isItalian(), b.constraint(User::LastName, "lastName", Constraint::notNull))
}
In this example the lastName is mandatory only for Italian.
Actually using yavi I can not implement the isItalian
function since I need to read an HTTP header to know if it’s italian or not.
so I think that have a context instead of a group can be fine to solve this issue. We can do something like that:
. constraintOnCondition((user, context) -> context.get("countryCode").equals("IT"), b.constraint(User::LastName, "lastName", Constraint::notNull))
Another solution it to create a group by country, but i can can hard to maintain. If I already use a group EDIT
CREATE
, I will have EDIT_FR
, CREATE_FR
, EDIT_IT
, CREATE_IT
, I think that It can be hard to manage.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
How to set default group in bean validation context
I'm working with bean validations and I'm searching for a possibility to set a default group of my own bean validation annotation.
Read more >context.Validate() and CustomValidator - Microsoft Q&A
4 Answers · change the Model to EditContext , · calling the EditContext.Validate method in the OnInvalidSubmit event or OnSubmit event: <EditForm ...
Read more >[Validator] Valid() with groups should not replace ... - GitHub
An alternative would be to change RecursiveContextualValidator and the context object given to constraints so the list of validation groups ...
Read more >Validation with Hibernate Validator - Quarkus
Enable the Post group, meaning only constraints assigned to the Post (and Default ) groups will be validated for the book parameter of...
Read more >Spring MVC Custom Validation - Baeldung
Creating a custom validator entails rolling out our own annotation and using it in our model to enforce the validation rules. So let's...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@deblockt I just sent a pr (#216) to introduce
ConstraintContext
. I’ve tried backwards compatibility, but it may break the code of advanced users like @duponter . I would appreciate your feedbacks.Our code with the ConstraintGroups is still experimental. This is a welcome addition to the library. Therefore, we will update our code to use the context instead of the groups.