Add custom validation message to enum - java

Is it possible to add a custom message to an enum if validation fails?
I have this enum class:
public enum EngineType{
FOO('F'),
BAR('B'),
QUX('Q');
private char id;
EngineType(char id) {
this.id = id;
}
public char getId() {
return this.id;
}
}
My model class contains private MyEnum myEnum;.
Currently if a value is passed in which isn't a valid enum, I get this BindingException:
{
"status": 400,
"validationErrors": {
"myEnum": "Failed to convert property value of type 'java.lang.String' to required type 'ie.aviva.services.motor.cartellservice.model.EngineType' for property 'myEnum'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [#javax.validation.constraints.NotNull ie.aviva.services.motor.cartellservice.model.EngineType] for value 'corge';"
},
"title": "Bad Request"
}
My controller looks like this:
#RequestMapping(
method = RequestMethod.GET,
value = Endpoints.TRUE_MATCH,
produces = {"application/json"})
public ResponseEntity<ResponseWrapper<List<TrueMatch>>> getTrueMatch(
#Valid MyDetails MyDetails) {
LogContext.put(Constants.TAG, myDetails);
LOG.info(
"Controller called to get true match with my details: " + myDetails.toString());
...
}
MyDetails is like this:
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Setter
#SuperBuilder
public class MyDetails extends BasicDetails {
#NotBlank
#Pattern(
regexp = "^[a-zA-Z]*$",
message = "'${validatedValue}' contains unsupported characters")
private String name;
#NotNull private MyEnum myEnum;
...
}
Is it possible to change the message to some custom message of my own?
I'm already able to do this in annotations that I added to other variables by including the message parameter in the annotation. I tried creating an annotation to validate the pattern as seen here but it didn't work. I think because the annotation was added like this, which was too late. The exception had already been thrown:
#NotNull
#EnumNamePattern(regexp = "foo|bar|qux")
private MyEnum myEnum;

Sure. The simplistic solution would be to simply accept the value as a String, then wrap your parsing logic in a try-catch block, then return the result you want the user to see.
final MyEnum userValue;
try
{
userValue = MyEnum.valueOf(someStringInput);
}
catch (Exception e)
{
//respond to the user, either with an exception, or
//a proper response according to your application
}

Bean Validation & ControllerAdvice
You can make use of the Bean Validation and Exception-handling mechanisms provided by spring.
Note that OP is already using Bean validation in their project, which can be observed by the usage of annotations #NotBlank, #Pattern. So the following reference is exclusively for the Readers.
To include the Bean validation into your Spring Boot project, you can add Spring Boot Starter Validation dependency. In a nutshell, Bean validation is a specification (like for instance JPA) describing an IPA, which offers various annotations and interfaces for verifying that the data of domain objects is correct. And Hibernate Validator is an implementation of this specification which comes with Spring Boot Starter Validation.
To validate that the given String matches one of the enum-constants we would need a separate class, let's say MyDetailsDto. In case if you wonder why a new class? If the validation constant would be applied on the field of type enum it would validate only input that can be successfully parsed into an enum (otherwise an IllegalArgumentException would be thrown before applying the validation annotation), which can be useful only certain enum-constants can be assigned to a field. But that's not the case, we need to find out whether the given string is equal to one of the enum constants (by the way, in addition this approach creates an opportunity for sanitizing the input, for instance replacing some characters if needed).
#Getter
#Setter
public class MyDetailsDto {
private String name;
#EngineTypeConstraint
private String engineType;
public MyDetails toMyDetails() {
return new MyDetails(name, EngineType.valueOf(engineType));
}
}
So, to create a custom Validation constraint, we need two things: a custom annotation a Validator associated with it (for more information refer to the documentation - Configuring Custom Constraints):
Each bean validation constraint consists of two parts:
A #Constraint annotation that declares the constraint and its configurable properties.
An implementation of the jakarta.validation.ConstraintValidator interface that implements the constraint’s behavior.
We can find more information regarding the requirements for the custom annotation in the Bean validation documentation Constraint:
Each constraint annotation must host the following attributes:
String message() default [...]; which should default to an error
message key made of the fully-qualified class name of the constraint
followed by .message. For example
"{com.acme.constraints.NotSafe.message}"
Class<?>[] groups() default {}; for user to customize the targeted groups
Class<? extends Payload>[] payload() default {}; for extensibility purposes
So our custom annotation needs to have at least these three attributes, and we're interested primarily in the first one, providing a message which would be used in case if an exception occurs. Let's our annotation:
#Constraint(validatedBy = EngineTypeValidator.class)
#Documented
#Target({ElementType.METHOD, ElementType.FIELD})
#Retention(RetentionPolicy.RUNTIME)
public #interface EngineTypeConstraint {
String message() default "Engine type does not exist";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Validator should implement generic interface ConstraintValidator, specifying the associated annotation and the validated type as its parameters.
That's how the implementation might look like:
public class EngineTypeValidator implements ConstraintValidator<EngineTypeConstraint, String> {
private static final Set<String> TYPE_NAMES =
EnumSet.allOf(EngineType.class).stream().map(Enum::name).collect(Collectors.toSet());
#Override
public boolean isValid(String engineType,
ConstraintValidatorContext constraintValidatorContext) {
if (!TYPE_NAMES.contains(engineType)) throw new EngineTypeNotValidException(engineType);
return true;
}
}
As you have probably noticed, validator throws a custom exception EngineTypeNotValidException which receives an invalid engine model as a parameter. We need this exception in order to build a response based on it.
public class EngineTypeNotValidException extends RuntimeException {
public EngineTypeNotValidException(String type) {
super(String.format("Engine type '%s' doesn't exist.", type));
}
}
To handle this exception and produces an error-response by using Controller Advice. For that, we need to define a class annotated with #ControllerAdvice and create a method marked with #ExceptionHandler to target the custom exception defined above.
#ControllerAdvice(assignableTypes = MyDetailsController.class)
public class ValidationExceptionHandler {
#ExceptionHandler(EngineTypeNotValidException.class)
public ResponseEntity<ErrorResponse> handleInvalidEngine(RuntimeException e) {
Throwable cause = e.getCause();
return ResponseEntity
.badRequest()
.body(new ErrorResponse(
HttpStatus.BAD_REQUEST.value(),
HttpStatus.BAD_REQUEST.getReasonPhrase(),
List.of(cause.getMessage())
));
}
}
Finally, to customize the response body, we can create a POJO with a couple of string field for representing the information of a failing response.
#AllArgsConstructor
#Getter
public class ErrorResponse {
private int status;
private String message;
private List<String> errors;
}
Demo
That's it now we can give it a try.
Consider the following dummy Controller:
#RestController
public class MyDetailsController {
#PostMapping("/newMyDetails")
public String newCar(#RequestBody #Valid MyDetailsDto dto) {
// some business logic
return "is valid";
}
}
And here's a couple screenshots from Postman with responses:
Valid request (hard-coded message from the Controller in the response):
Invalid request (response with a customized error message, prompting that enum-name provided in the request doesn't exist):

Related

Spring validate abstract request parameter class

I'm trying to write a spring endpoint that generates different reports, depending on the request parameters
#GetMapping
#ResponseBody
public ResponseEntity<String> getReport(
#RequestParam(value = "category") String category,
#Valid ReportRequestDTO reportRequestDTO) {
Optional<ReportCategory> reportCategory = ReportCategory.getReportCategoryByRequest(category);
if (reportCategory.isEmpty()) {
throw new ApiRequestException("Requested report category does not exist.");
}
try {
Report report = reportFactory.getReport(reportCategory.get());
return ResponseEntity.ok().body(report.generate(reportRequestDTO));
} catch (Exception e) {
throw new ApiRequestException("Could not generate report.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
The ReportCategory is an enum and Report is an abstract class of which multiple concrete implementations exist. Depending on the passed category the ReportFactory will instantiate the right Report. ReportRequestDTO is a class that contains all parameters that are required to generate the report. If this is passed to the generate() method, the report is generated.
Depending on the ReportCategory, different parameters may be required and need to be validated, but there can also be some common ones.
Is it possible to have an abstract class ReportRequestDTO with the common parameters and then a concrete DTO implementation for each report with its unique parameters, that is instantiated and validated depending on the report category before it is passed to the generate() method?
Edit:
I want something like this for shared parameters:
#Data
public abstract class ReportRequestDTO {
#NotEmpty
private String foo;
#NotEmpty
private String bar;
}
And then for each Report the individual parameters:
#Data
public class ReportADTO extends ReportRequestDTO {
#NotEmpty
private String foobar;
}
But I can't use and abstract class as DTO, because it can't be instantiated.
Also this would try to validate foobar even if I don't need it in ReportB.
Basically I want this endpoint to be able to generate all reports. Since I don't know yet which reports exist and may be added in the future and which parameters they require, I'd like to have the DTO extendable so that I don't have to touch the endpoint anymore and simply implement the report and create a DTO that extends ReportRequestDTO with the required parameters for that report.
So what I need is an Object that I can use as ReportRequestDTO that is extendable with all parameters for all reports so that I can pass them on the request, and then I would instantiate the DTO for the particular report with the request parameters and validate it.
You can use post-validation. I do not see why you need it for you because you can have only one input structure in the one request endpoint body. Would you like to cut the data from the request and ignore what is not used? This is also a solution anyway.
Option 1:
Inject javax.validation.Validator interface and call validate. It can be autowired. API It is just the result Set.
Option 2:
If you would like to throw exception like controller, you have to create a/more bean(s) with #Validated annotation such as:
public class ModelA {
#NotEmpty
private String text;
// getter setter
}
#Component // or use #Configuration with #Bean
#Validated
public class ReportA {
public void generate(#Valid ModelA model) { ... }
}
So I ended up changing it to a POST request and allowing a JSON body, that is then parsed to the required DTO like so:
ReportRequestDTO reportRequestDTO = report.getDto();
reportRequestDTO = new ObjectMapper().readValue(paramsJson,
reportRequestDTO.getClass());
getDTO() returns an instance of the concrete DTO that is populated with the JSON data and it is then validated as in #Numichi answer

Conditional Validating Request with SpringBoot

I am using Spring-WebMvc and SpringBoot in constructing a web service, together with spring-boot-starter-validation to simplify the setup.
In the new user requirement, validation needs to be done conditionally based on user request, which I am not sure how the design could fit well.
Below is an example, where such conditional checking is needed. Hope one can give a direction / solution on how can one do it in a better way.
Example
Let's say we are creating a service for multiple organizations and we are using one server to handle different organization request. Each organization request must supply a valid companyId to identify the organization.
And at the beginning, all organizations are strictly forbidding its own employee to place a lucky draw request.
public class Request {
#CompanyId
private String companyId;
}
#StaffNotAllowLuckyDraw
public class LuckyDrawRequest extends Request {
private String userId;
}
With the corresponding annotation and validator.
#Retention(RetentionPolicy.RUNTIME)
#Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
#Constraint(validatedBy = { StaffNotAllowLuckyDrawValidator.class })
public #interface StaffNotAllowLuckyDraw {
String message() default "{com.abc.validation.constraints.staffnotallowluckydraw.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class StaffNotAllowLuckyDrawValidator implements ConstraintValidator<StaffNotAllowLuckyDraw, LuckyDrawRequest> {
#Override
public boolean isValid(LuckyDrawRequest request, ConstraintValidatorContext context) {
String companyId = request.getCompanyId();
String userId = request.getUserId();
User user = Database.getUser(companyId, userId);
return !user.isStaff();
}
}
But later,
a new requirement comes, which allow company to customize the validation, allowing / forbidding a staff to place a lucky draw request.
So the validator logic needs to be changed.
And definitely I can add custom logic inside this validator to make it work very easily. Like below.
public class StaffNotAllowLuckyDrawValidator implements ConstraintValidator<StaffNotAllowLuckyDraw, LuckyDrawRequest> {
private List<String> exceptions;
#Override
public boolean isValid(LuckyDrawRequest request, ConstraintValidatorContext context) {
String companyId = request.getCompanyId();
if (exceptions.contains(companyId)) {
return true;
}
String userId = request.getUserId();
User user = Database.getUser(companyId, userId);
return !user.isStaff();
}
}
However, it seems unclear to me from the Annotation perspective (look at LuckyDrawRequest class) that there are such condition to skip such validation.
Worse still, there may have more and more such custom validation coming, and some maybe using default validation (e.g. #Max) Is there any approach to handle the above problem?
Note that the validation message should not be changed, on this change. As it has been used for months already.
P.S.
I am using spring-boot#2.5.3, which includes hibernate-validator#6.2.0, which should be JSR380 compliance.

Spring Boot - how to validate fields that depend on each other?

Is there some way in Spring Boot that I can perform validation on properties that depend on each other's values, and have the error message be associated with the property?
I want to return the errors to the user in a nice JSON structure:
{
"errors": {
"name": "is required if flag is true"
}
}
Example:
#Entity
public class MyEntity {
private boolean nameRequiredFlag;
// Required if "nameRequiredFlag" is set to true:
private String name;
}
One solution that doesn't solve my problem of associating the error message with the name property is to create a validator annotation for the entity:
#ValidEntity
public class MyEntity {
private boolean nameRequiredFlag;
// Required if "nameRequiredFlag" is set to true:
private String name;
}
#Constraint( validatedBy = { MyEntityValidator.class } )
#Documented
#Target( { ElementType.TYPE } )
#Retention( RetentionPolicy.RUNTIME )
public #interface ValidEntity{
Class<?>[] groups () default {};
String message () default "name is required if 'nameRequiredFlag' is true";
Class<? extends Payload>[] payload () default {};
}
public class MyEntityValidator implements Validator<ValidEntity, MyEntity> {
#Override
public boolean isValid ( MyEntity entity, ConstraintValidatorContext context ) {
if ( !entity.nameRequiredFlag ) return true;
return !StringUtils.isBlank( entity.getName() );
}
}
This is laughably cumbersome and doesn't solve my problem. Isn't there any way I can do this with the framework validation?
Edit: This is for a JSON API, and the consumer really needs to be able to associate the error message to a best guess at which field has an issue. It is not helpful to send the consumer an error message for the whole object, or a computed property.
Solution given by #EvicKhaosKat is one way of doing it. However, when there are too many fields dependent on each other in a complicated way, your class becomes full of annotations and I personally struggle a lot relating them.
A simpler approach is to create a method(s) in your pojo which does the cross field validations and returns a boolean. On the top of this method annotate it with #AssertTrue(message = "your message"). It will solve your problem in a cleaner fashion.
public class SampleClass {
private String duration;
private String week;
private String month;
#AssertTrue(message = "Duration and time attributes are not properly populated")
public boolean isDurationCorrect() {
if (this.duration.equalsIgnoreCase("month")) {
if (Arrays.asList("jan", "feb", "mar").contains(month))
return true;
}
if (this.duration.equalsIgnoreCase("week")) {
if (Arrays.asList("1-7", "8-15", "16-24", "25-31").contains(week))
return true;
}
return false;
}
}
Note: I have not tested this code but have used this approach in multiple places and it works.
Possible reason is that name validation operates on not-yet-fully constructed object, so nameRequiredFlag is not filled yet.
As an option there is a #GroupSequence annotation, which allows to group and perform validations in an order you specify.
For example it is possible to add to MyEntity annotations:
#ValidEntity(groups = DependentValidations.class)
#GroupSequence({MyEntity.class, DependentValidations.class})
So all the other validation annotations on MyEntity class gonna be performed first, and after that DependentValidations group, which consists of ValidEntity.
Thus ValidEntity will be called on fully created object, and the last in order.
(DependentValidations.class - just an empty interface created somewhere nearby, like any other marker interface)
https://www.baeldung.com/javax-validation-groups will possibly describe that in much more details.
p.s. answer provided by #Innovationchef will possibly suit the case more :)

How can a Spring Boot web app validate a form field that is required conditional on another field?

I have a Spring Boot web app in which fields of my form-backing bean are annotated with Bean Validation annotations (see Baeldung's tutorial or the docs at spring.io). For example, fields on Customer beans might be annotated like this:
#NotBlank
#Pattern(regexp="[ \\.A-Za-z-]*")
private String firstName;
#DateTimeFormat(pattern="M/d/yyyy")
#NotNull
#Past
private Date DOB;
What I want to know is: (How) Can I implement a complex validation that looks at multiple fields? I mean, using this framework. For example, let's say I have a field Country and a field ZipCode and I want the ZipCode to be #NotBlank if and only if the Country equals "US", optional otherwise.
In my Controller, the validation is very elegantly triggered with the use of the #Valid annotation, and errors are attached to a BindingResult object. Like so:
#PostMapping("customer/{id}")
public String updateCustomer( #PathVariable("id") Integer id,
#Valid #ModelAttribute("form") CustomerForm form,
BindingResult bindingResult ) {
if (bindingResult.hasErrors()) {
return "customerview";
}
customerService.updateCustomer(form);
return "redirect:/customer/"+id;
}
What I'm hoping to find is a way to write this conditional validator in a way that it, too, will be triggered by the #Valid annotation and will attach it's error message to the ZipCode field in the BindingResult. Ideally I shouldn't have to change the Controller code at all.
you can getting help of creating custom validator class, the following link helps you:
https://www.baeldung.com/spring-mvc-custom-validator#custom-validation
For this kind of stuff you need to use cross field validation. Please try :
Create annotation :
package foo.bar;
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Constraint(validatedBy = {CustomerValidator.class})
public #interface CustomerValid {
String message() default "{foo.bar.CustomerValid.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Create custom validator :
public class CustomerValidator implements ConstraintValidator<CustomerValid, Customer> {
#Override
public void initialize(CustomerValid constraint) {
}
#Override
public boolean isValid(Customer customer, ConstraintValidatorContext context) {
return !("US".equals(customer.getCountry() && "".equals(customer.getZipCode())));
}
}
Annotate your class :
#CustomerValid
public class Customer {
// body
}
This class validation will be processed in addition to existent field validators.

Can a custom validator have multiple messages based on what validation failed in hibernate validator?

I have a custom validation to check an email field in a class.
annotation interface:
#ReportAsSingleViolation
#NotBlank
#Email
#Target({ CONSTRUCTOR, FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
#Retention(RUNTIME)
#Constraint(validatedBy = CustomEmailValidator.class)
#Documented
public #interface CustomEmail {
String message() default "Failed email validation.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
CustomEmailValidator class:
public class CustomEmailValidator implements ConstraintValidator<CustomEmail, String> {
public void initialize(CustomEmail customEmail) {
// nothing to initialize
}
public boolean isValid(String email, ConstraintValidatorContext arg1) {
if (email != null) {
String domain = "example.com";
String[] emailParts = email.split("#");
return (emailParts.length == 2 && emailParts[1].equals(domain));
} else {
return false;
}
}
}
I use a ValidationMessages.properties file for all of my custom messages. in the properties file I reference a failure in the above code using:
CustomEmail.email=The provided email can not be added to the account.
The problem is that this error message is used for all failures during validation, so even if the user provided a blank string it will print that message. What I want to do is if the validation fails on #NotBlank then print a "required field message", if it fails on #Email provide a "invalid email" message. Then only for when it fails the custom validation would it print the CustomEmail.email message. Also in my annotation interface do the #NotBlank and #Email occur in order or are they randomly run. Then what ever validation is run first is returned as the error? My validation requires that they run in the order they are listed #NotBlank followed by #Email followed by CustomEmail.
Turn off #ReportAsSingleViolation. You'll get a ConstraintViolation for each violation, and can work accordingly.
As for order, following #Hardy's answer.
Note that in your custom constraints there is no order defined. Even so you first list #NotBlank and then #Email, there is no order guarantee. Java itself does not define any order in annotations. If you want to define an order you need to use groups and/or group sequence. In this case you also cannot use a composed constraints, since group definition on the composing constraints are ignored. Only the group of the main constraint applies.
You could create an Enum defining the types of expected errors, and then use its value to retrieve the proper error message from your configuration. Something like
/**
<P>Defines the type of email-formatting error message.</P>
**/
public enum EmailErrorTypeIs {
/**
<P>...</P>
#see #INVALID
#see #OTHER
**/
BLANK,
/**
<P>...</P>
#see #BLANK
**/
INVALID,
/**
<P>...</P>
#see #BLANK
**/
OTHER;
};
Which you would use like this:
if(input == null || input.length() == 0) {
throw new IllegalArgumentException(getErrorFromConfig(EmailErrorTypeIs.BLANK));
} else if...

Categories

Resources