question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replace validation Group by a validation context

See original GitHub issue

My 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:closed
  • Created 2 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
makingcommented, Feb 18, 2022

@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.

0reactions
dupontercommented, Feb 18, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found