feat: support JSR-303 validation groups in BeanValidationBinder - #25187
Open
totally-not-ai[bot] wants to merge 2 commits into
Open
feat: support JSR-303 validation groups in BeanValidationBinder#25187totally-not-ai[bot] wants to merge 2 commits into
totally-not-ai[bot] wants to merge 2 commits into
Conversation
Pins down the current behaviour of BeanValidationBinder with respect to JSR-303 validation groups (group scoped constraints are never validated, the required indicator ignores groups) and specifies the semantics the group support asked for in #7032 should have: a group configured on the binder that is used for all validation, and a one-shot validation against an explicit group that does not change the configured groups. The target semantics are exercised through a test local binder that plugs a group aware validator in via configureBinding, which is the workaround needed until the API exists. Part of #7032
BeanValidationBinder validated only the constraints of the default validation group, and there was no way to opt in to constraints that declare a group. The validation groups to validate against can now be configured with setValidationGroups, or given to the constructor, in which case they are used for all validation triggered by the binder, including the validation of a single field when its value changes. As the groups replace the default group, Default.class has to be listed explicitly to validate the constraints without a group as well. In addition, validate(Class<?>...) runs a single validation against the given groups without changing the configured ones, which makes it possible to only check constraints that are relevant when the data is saved, without validating them on every value change. The groups in effect are also available to bean level validators through getValidationGroups, which return the groups of an ongoing one-shot validation while it is running. BeanValidator accepts the validation groups either as an array or as a supplier, the latter allowing the groups to be changed after the validator has been created. The required indicator now takes the configured validation groups into account as well, so that a constraint that is not validated no longer marks a field as required. Fixes #7032
|
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What
BeanValidationBinderpreviously validated only the constraints of the default validation group, with no way to opt in to constraints that declare a group (#7032). This adds first-class validation group support:BeanValidationBinderBeanValidationBinder(Class<BEAN>, Class<?>...)andsetValidationGroups(Class<?>...)configure the groups used for all validation triggered by the binder, including single-field validation on value change. Groups replace the default group, soDefault.classmust be listed explicitly to also validate ungrouped constraints. Passing no groups restores the previous behavior.validate(Class<?>...)runs a one-shot validation against the given groups without changing the configured ones — useful for constraints that only matter on save and shouldn't fire on every keystroke.getValidationGroups()exposes the groups in effect (returning the one-shot groups while such a validation is running), so bean-level validators added withwithValidatorcan validate against the same groups as the field-level validation.BeanValidatorClass<?>...array or as aSerializableSupplier<Class<?>[]>, the latter allowing the groups to change after the validator is created (this is how the binder feeds its current groups in).getValidationGroups()accessor;validateValueis now called with the resolved groups.Tests
BeanValidationGroupsTestcovers the new semantics: group-scoped constraints being validated only when their group is configured, the default group replacement rule, the required indicator honoring groups, and one-shotvalidate(Class<?>...)leaving the configured groups untouched.Fixes #7032