forked from micronaut-projects/micronaut-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to disable automatically prepend property path in excepti…
…on message. Fixed micronaut-projects#286
- Loading branch information
Showing
7 changed files
with
169 additions
and
34 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
validation/src/main/java/io/micronaut/validation/ConstraintViolationExceptionUtil.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,49 @@ | ||
/* | ||
* Copyright 2017-2024 original 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 io.micronaut.validation; | ||
|
||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.ConstraintViolationException; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Utility class for ConstraintViolationException. | ||
* | ||
* @since 4.9.0 | ||
*/ | ||
public final class ConstraintViolationExceptionUtil { | ||
|
||
private ConstraintViolationExceptionUtil() { | ||
} | ||
|
||
/** | ||
* Method to create ConstraintViolationException with custom message | ||
* | ||
* @param prependPropertyPath prependPropertyPath configuration flag | ||
* @param constraintViolations constraint violations | ||
* @return ConstraintViolationException | ||
*/ | ||
public static ConstraintViolationException createConstraintViolationException(boolean prependPropertyPath, Set<? extends ConstraintViolation<?>> constraintViolations) { | ||
var message = constraintViolations.stream() | ||
.map(cv -> cv == null ? "null" : (prependPropertyPath ? cv.getPropertyPath() + ": " : "") + cv.getMessage()) | ||
.collect(Collectors.joining(", ")); | ||
|
||
return new ConstraintViolationException(message, constraintViolations); | ||
} | ||
|
||
} |
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
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
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
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
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
42 changes: 42 additions & 0 deletions
42
.../src/test/groovy/io/micronaut/validation/validator/DisabledPrependPropertyPathSpec.groovy
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,42 @@ | ||
package io.micronaut.validation.validator | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.validation.Validated | ||
import jakarta.inject.Singleton | ||
import jakarta.validation.Valid | ||
import jakarta.validation.constraints.Max | ||
import jakarta.validation.constraints.Min | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class DisabledPrependPropertyPathSpec extends Specification { | ||
|
||
@Shared | ||
@AutoCleanup | ||
ApplicationContext applicationContext = ApplicationContext.run(["micronaut.validator.prepend-property-path": false]) | ||
|
||
void "test disabled prependPropertyPath"() { | ||
when: | ||
def service = applicationContext.getBean(MyService3) | ||
def bean = new MyBeanWithPrimitives() | ||
bean.number = 100 | ||
service.myMethod2(bean) | ||
then: | ||
Exception e = thrown() | ||
e.message == 'must be less than or equal to 20' | ||
} | ||
} | ||
|
||
@Validated | ||
@Singleton | ||
class MyService3 { | ||
|
||
@Min(10) | ||
int myMethod1(@Max(5) int a) { | ||
return a | ||
} | ||
|
||
void myMethod2(@Valid MyBeanWithPrimitives bean) { | ||
} | ||
} |