-
Notifications
You must be signed in to change notification settings - Fork 40.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide callback mechanism for customizing validation configuration
See gh-29429
- Loading branch information
1 parent
f3aa5d0
commit 76a1c6b
Showing
4 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
59 changes: 59 additions & 0 deletions
59
...springframework/boot/validation/beanvalidation/CustomizableLocalValidatorFactoryBean.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.validation.beanvalidation; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import jakarta.validation.Configuration; | ||
|
||
import org.springframework.boot.validation.beanvalidation.customize.Customizer; | ||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; | ||
|
||
/** | ||
* This class will callback the {@link Customizer} which supply by application. For | ||
* example: | ||
* | ||
* <pre>{@code | ||
* @Bean | ||
* public Customizer customizer() { | ||
* return configuration -> { | ||
* configuration.addValueExtractor(new CustomResultValueExtractor()); | ||
* configuration.xxx | ||
* ... | ||
* }; | ||
* } | ||
* }</pre> | ||
* | ||
* @author Dang Zhicairang | ||
* @since 2.6.2 | ||
*/ | ||
public class CustomizableLocalValidatorFactoryBean extends LocalValidatorFactoryBean { | ||
|
||
private List<Customizer> customizers; | ||
|
||
public void setCustomizers(List<Customizer> customizers) { | ||
this.customizers = customizers; | ||
} | ||
|
||
@Override | ||
protected void postProcessConfiguration(Configuration<?> configuration) { | ||
Optional.ofNullable(this.customizers) | ||
.ifPresent((list) -> list.forEach((customizer) -> customizer.customize(configuration))); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...springframework/boot/validation/beanvalidation/customize/AddValueExtractorCustomizer.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.validation.beanvalidation.customize; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import jakarta.validation.Configuration; | ||
import jakarta.validation.valueextraction.ValueExtractor; | ||
|
||
/** | ||
* Add given collection of {@link ValueExtractor} into {@link Configuration}. | ||
* | ||
* @author Dang Zhicairang | ||
* @since 2.6.2 | ||
*/ | ||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public class AddValueExtractorCustomizer implements Customizer { | ||
|
||
private List<ValueExtractor> valueExtractors; | ||
|
||
public AddValueExtractorCustomizer(List<ValueExtractor> valueExtractors) { | ||
this.valueExtractors = valueExtractors; | ||
} | ||
|
||
public List<ValueExtractor> getValueExtractors() { | ||
return this.valueExtractors; | ||
} | ||
|
||
public void setValueExtractors(List<ValueExtractor> valueExtractors) { | ||
this.valueExtractors = valueExtractors; | ||
} | ||
|
||
@Override | ||
public void customize(Configuration<?> configuration) { | ||
Optional.ofNullable(this.getValueExtractors()) | ||
.ifPresent((list) -> list.forEach(configuration::addValueExtractor)); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...rc/main/java/org/springframework/boot/validation/beanvalidation/customize/Customizer.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.validation.beanvalidation.customize; | ||
|
||
import jakarta.validation.Configuration; | ||
|
||
/** | ||
* Callback interface that can be used to customize {@link Configuration}. | ||
* | ||
* @author Dang Zhicairang | ||
* @since 2.6.2 | ||
* @see AddValueExtractorCustomizer | ||
*/ | ||
@FunctionalInterface | ||
public interface Customizer { | ||
|
||
void customize(Configuration<?> configuration); | ||
|
||
} |