How to trigger custom annotated validation rule of an object manually?

Pati Ram Yadav

I need to trigger my custom validator on an object either manually or during the json-string to object conversion.

My dto looks as follows:

@NotNullable.List({
    @NotNullable(enumMessage = RequiredFieldErrors.NAME_REQUIRED, fieldName = "info.personName")
//More
})

@SpecificValue.List({
    @SpecificValue(enumMessage = BusinessErrors.CONFIDENTIALITY_REQUESTED, fieldName = "info.personName.confidentialityRequested", value = ConstantValue.CONFIDENTIALITY_VALUES)
//More
})

@DependentField.List({
    @DependentField(
            parentFieldName = "info.personDemographics.ssnUnknown", 
            parentFieldValue = "false", 
            childFieldNames = {"info.personDemographics.ssn"},
            childFieldValues = {ConstantValue.SSN_PATTERN},
            includeChildValues = true,
            enumMessage = RequiredFieldErrors.PAYEE_SSN_MUST_BE_NUMERIC)
//More
})

public class MyClass implements Serializable {
    private static final long serialVersionUID = 1L;
    @SpecificValue(enumMessage = BusinessErrors.SOURCE_ID_INVALID, value = ConstantValue.SOURCE_ID)
    private String sourceId;
    @NotNullable(enumMessage = RequiredFieldErrors.INFO_REQUIRED)
    private PersonType info;
//More
}

In above code, PersonType is being composed from other object type. Again there is validation for those object.

This way working fine.

@RequestMapping(value = "/api/rest/uploadefile", method = {RequestMethod.POST}
public ResponseEntity<Object> upload1(@Valid @RequestBody MyClass request, BindingResult bindingResult, HttpServletRequest request) {        
     if(bindingResult.hasErrors()) {
          throw new FieldValidationException(Contstants.INVALID_REQUEST, errors);
    }
}

I need to get the json request as string. I know @Valid @RequestPart...., will work fine for multipart/mixed type. I want something like this:

@RequestMapping(value = "/api/rest/uploadefile", method = {RequestMethod.POST}, headers = {"content-type=multipart/form-data"})
public ResponseEntity<Object> upload1(@RequestPart(value = "request", required = true) String request, @RequestPart(value = "file", required = true) List<MultipartFile> file, Errors errors) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        MyClass myClass = mapper.readValue(request, MyClass.class);
        ValidationUtils.invokeValidator(validator1, myClass, errors);
        if(errors.hasErrors()) {
                  throw new FieldValidationException(Contstants.INVALID_REQUEST, errors);
        }
}

I tried with both springframework validator as well as javax jsr validator. Neither of them is working. I am getting following error:

class java.lang.IllegalStateException JSR-303 validated property 'info.personName' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access

For following request:

curl -i -X POST -myURL -H "Authorization: ABC" -F request={} -F [email protected]

My validator implements javax.validation.ConstraintValidator as follows:

public class DependentFieldValidator implements ConstraintValidator<DependentField, Object> {
 //My code
}
Denis Zavedeev

The Errors you're using for validation are not that Errors that should be used.

It should work if you will use manually created Errors:

BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(myClass, "myClass");
ValidationUtils.invokeValidator(validator, myClass, bindingResult);

Note that BeanPropertyBindingResult implements Errors

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to manually trigger spring validation?

How do I trigger HTML5 validation error popup when custom validation message is set manually?

How to manually trigger validation with jQuery validate?

How to trigger Angular 2 input validation manually?

how to use laravel's validation rule in custom validation rule?

Trigger authorization validation manually

How to set custom trigger rule in Airflow?

How to create a custom validation rule in cake 3

How to override the message of the custom validation rule in Laravel?

How to add custom validation rule into laravel 5.2?

How to write custom validation rule in controller Laravel?

custom validation rule is not triggered

Laravel custom validation rule

Blazor - Manually trigger form validation

How to create custom validation rule for dependent input fields in laravel

How to apply a custom validation rule to a model in phoenix framework

How to prioritize Laravel custom validation rule over defaults

How to access route parameter in custom validation rule laravel

How to send Multiple parameters in passes function of custom validation rule

How to create custom validation rule in Laravel 6 API

How to add a custom Laravel error message when using Rule::in as validation

How to create custom validation rule that only accepts specific numbers in Laravel?

WPF: Custom Validation Rule not found

Laravel : Validation messages for custom rule

Laravel 5.6 custom rule validation

Multi Select Custom Validation Rule

Manually trigger an event on window object

How to define prop validation rule for an object of objects in React?

How to trigger/refer a custom JavaScript object?