Class ValidatingModelMapper

java.lang.Object
org.modelmapper.ModelMapper
uk.co.jasonmarston.kiunzi.utility.producer.modelmapper.ValidatingModelMapper

public class ValidatingModelMapper extends org.modelmapper.ModelMapper
A ModelMapper extension that automatically validates the mapped destination object using Bean Validation after every mapping operation.

If any constraint violations are found on the destination, a DomainInvariantViolationException is thrown instead of returning the partially-valid object.

See Also:
  • ModelMapper
  • Validator
  • Constructor Summary

    Constructors
    Constructor
    Description
    ValidatingModelMapper(jakarta.validation.Validator validator)
    Constructs a ValidatingModelMapper with the given Bean Validation Validator.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    map(Object source, Class<T> destinationType)
    Maps properties from source to a new instance of destinationType, then validates the result against all declared Bean Validation constraints.
    <T> T
    map(Object source, Class<T> destinationType, String typeMapName)
    Maps properties from source to a new instance of destinationType using the named type map, then validates the result.
    void
    map(Object source, Object destination)
    Maps properties from source to an existing destination object, then validates the destination against all declared Bean Validation constraints.
    void
    map(Object source, Object destination, String typeMapName)
    Maps properties from source to an existing destination object using the named type map, then validates the destination object.
    <T> T
    map(Object source, Type destinationType)
    Maps properties from source to a new instance of the given generic destinationType, then validates the result against all declared Bean Validation constraints.
    <T> T
    map(Object source, Type destinationType, String typeMapName)
    Maps properties from source to a new instance of the given generic destinationType using the named type map, then validates the result.

    Methods inherited from class org.modelmapper.ModelMapper

    addConverter, addConverter, addMappings, createTypeMap, createTypeMap, createTypeMap, createTypeMap, createTypeMap, createTypeMap, createTypeMap, createTypeMap, emptyTypeMap, emptyTypeMap, getConfiguration, getTypeMap, getTypeMap, getTypeMaps, registerModule, typeMap, typeMap, validate

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ValidatingModelMapper

      public ValidatingModelMapper(jakarta.validation.Validator validator)
      Constructs a ValidatingModelMapper with the given Bean Validation Validator.
      Parameters:
      validator - the Jakarta Validator used to validate mapped destinations; must not be null
  • Method Details

    • map

      public void map(Object source, Object destination)
      Maps properties from source to an existing destination object, then validates the destination against all declared Bean Validation constraints.
      Overrides:
      map in class org.modelmapper.ModelMapper
      Parameters:
      source - the source object to map from; must not be null
      destination - the destination object to map into; must not be null
      Throws:
      uk.co.jasonmarston.kiunzi.utility.validator.DomainInvariantViolationException - if the destination object fails validation after mapping
    • map

      public <T> T map(Object source, Class<T> destinationType)
      Maps properties from source to a new instance of destinationType, then validates the result against all declared Bean Validation constraints.
      Overrides:
      map in class org.modelmapper.ModelMapper
      Type Parameters:
      T - the destination type
      Parameters:
      source - the source object to map from; must not be null
      destinationType - the Class of the destination type; must not be null
      Returns:
      the validated mapped destination instance
      Throws:
      uk.co.jasonmarston.kiunzi.utility.validator.DomainInvariantViolationException - if the destination object fails validation after mapping
    • map

      public <T> T map(Object source, Type destinationType)
      Maps properties from source to a new instance of the given generic destinationType, then validates the result against all declared Bean Validation constraints.
      Overrides:
      map in class org.modelmapper.ModelMapper
      Type Parameters:
      T - the destination type
      Parameters:
      source - the source object to map from; must not be null
      destinationType - the Type of the destination; must not be null
      Returns:
      the validated mapped destination instance
      Throws:
      uk.co.jasonmarston.kiunzi.utility.validator.DomainInvariantViolationException - if the destination object fails validation after mapping
    • map

      public void map(Object source, Object destination, String typeMapName)
      Maps properties from source to an existing destination object using the named type map, then validates the destination object.
      Overrides:
      map in class org.modelmapper.ModelMapper
      Parameters:
      source - the source object to map from; must not be null
      destination - the destination object to map into; must not be null
      typeMapName - the name of the type map to use; must not be null
      Throws:
      uk.co.jasonmarston.kiunzi.utility.validator.DomainInvariantViolationException - if the destination object fails validation after mapping
    • map

      public <T> T map(Object source, Class<T> destinationType, String typeMapName)
      Maps properties from source to a new instance of destinationType using the named type map, then validates the result.
      Overrides:
      map in class org.modelmapper.ModelMapper
      Type Parameters:
      T - the destination type
      Parameters:
      source - the source object to map from; must not be null
      destinationType - the Class of the destination type; must not be null
      typeMapName - the name of the type map to use; must not be null
      Returns:
      the validated mapped destination instance
      Throws:
      uk.co.jasonmarston.kiunzi.utility.validator.DomainInvariantViolationException - if the destination object fails validation after mapping
    • map

      public <T> T map(Object source, Type destinationType, String typeMapName)
      Maps properties from source to a new instance of the given generic destinationType using the named type map, then validates the result.
      Overrides:
      map in class org.modelmapper.ModelMapper
      Type Parameters:
      T - the destination type
      Parameters:
      source - the source object to map from; must not be null
      destinationType - the Type of the destination; must not be null
      typeMapName - the name of the type map to use; must not be null
      Returns:
      the validated mapped destination instance
      Throws:
      uk.co.jasonmarston.kiunzi.utility.validator.DomainInvariantViolationException - if the destination object fails validation after mapping