Search in sources :

Example 6 with SpringValidatorAdapter

use of org.springframework.validation.beanvalidation.SpringValidatorAdapter in project chassis by Kixeye.

the class RawWebSocketMessage method deserialize.

/**
 * Deserializes the given message.
 *
 * @param action
 * @return
 * @throws Exception
 */
public T deserialize(WebSocketAction action) throws Exception {
    // first deserialize
    T message = null;
    if (messageClass != null) {
        message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass);
    }
    // then validate
    if (message != null && action.shouldValidatePayload()) {
        SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator);
        BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName());
        validatorAdapter.validate(message, result);
        if (result.hasErrors()) {
            throw new MethodArgumentNotValidException(new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result);
        }
    }
    return message;
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) MethodParameter(org.springframework.core.MethodParameter) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ByteBufferBackedInputStream(com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream)

Aggregations

SpringValidatorAdapter (org.springframework.validation.beanvalidation.SpringValidatorAdapter)6 Test (org.junit.Test)4 Validator (org.springframework.validation.Validator)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 SpringValidator (org.springframework.boot.autoconfigure.validation.SpringValidator)2 ByteBufferBackedInputStream (com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream)1 Test (org.junit.jupiter.api.Test)1 ValidationBindHandler (org.springframework.boot.context.properties.bind.validation.ValidationBindHandler)1 ConfigurationPropertySource (org.springframework.boot.context.properties.source.ConfigurationPropertySource)1 MockConfigurationPropertySource (org.springframework.boot.context.properties.source.MockConfigurationPropertySource)1 MethodParameter (org.springframework.core.MethodParameter)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 PropertySource (org.springframework.core.env.PropertySource)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1