io.vavr:vavr-beanvalidation2

Integrates vavr with bean validation 2.x frameworks (JSR 380)

License

License

Categories

Categories

Vavr General Purpose Libraries Functional Programming
GroupId

GroupId

io.vavr
ArtifactId

ArtifactId

vavr-beanvalidation2
Last Version

Last Version

0.10.0
Release Date

Release Date

Type

Type

jar
Description

Description

Integrates vavr with bean validation 2.x frameworks (JSR 380)
Source Code Management

Source Code Management

https://github.com/vavr-io/vavr-beanvalidation2.git

Download vavr-beanvalidation2

How to add to project

<!-- https://jarcasting.com/artifacts/io.vavr/vavr-beanvalidation2/ -->
<dependency>
    <groupId>io.vavr</groupId>
    <artifactId>vavr-beanvalidation2</artifactId>
    <version>0.10.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.vavr/vavr-beanvalidation2/
implementation 'io.vavr:vavr-beanvalidation2:0.10.0'
// https://jarcasting.com/artifacts/io.vavr/vavr-beanvalidation2/
implementation ("io.vavr:vavr-beanvalidation2:0.10.0")
'io.vavr:vavr-beanvalidation2:jar:0.10.0'
<dependency org="io.vavr" name="vavr-beanvalidation2" rev="0.10.0">
  <artifact name="vavr-beanvalidation2" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.vavr', module='vavr-beanvalidation2', version='0.10.0')
)
libraryDependencies += "io.vavr" % "vavr-beanvalidation2" % "0.10.0"
[io.vavr/vavr-beanvalidation2 "0.10.0"]

Dependencies

compile (2)

Group / Artifact Type Version
io.vavr : vavr jar 0.10.0
javax.validation : validation-api jar 2.0.1.Final

test (8)

Group / Artifact Type Version
org.hibernate.validator : hibernate-validator jar 6.0.17.Final
javax.xml.bind : jaxb-api jar 2.3.0
com.sun.xml.bind : jaxb-core jar 2.3.0
com.sun.xml.bind : jaxb-impl jar 2.3.0
javax.activation : activation jar 1.1.1
org.glassfish : javax.el jar 3.0.0
junit : junit jar 4.12
org.assertj : assertj-core jar 3.12.2

Project Modules

There are no modules declared in this project.

Build Status

Vavr-Beanvalidation 2.0

This module provides support for bean validation 2.0 (JSR380). Can be used with any service provider of the bean validation spec e.g. org.hibernate.validator:hibernate-validator

Features:

  • @Size for vavr's Traversable<T>
  • @NotEmpty for vavr's Value<T>
  • All available validations can be applied to nested Tuple Values. See example below
  • All available validations can be applied to vavr's Map<K, V>s and Traversable<T>s including Multimap<K, V>s
  • All available validations on nested collection element types now give proper feedback as to where violations occurred (index for Seqs and key for Maps)

Using the module

Add the dependency to your classpath. For maven:

<dependency>
    <groupId>io.vavr</groupId>
    <artifactId>vavr-beanvalidation2</artifactId>
    <version>0.10.0</version>
</dependency>

For the bean validation service provider to pick it up the constraints must be registered. Add the following to your validation.xml:

...
<constraint-mapping>META-INF/constraints-vavr.xml</constraint-mapping>
...

Or register via java config of your validation provider. See javax.validation.Configuration#addMapping.

Since it would be very tedious to register all the ValueExtractors for the tuple elements by yourself, configuration is automatically done for you via Java service loader. The definition in META-INF/services is picked up by the validation service provider.

Now JSR 380 validations will work on vavr types. e.g.

public class TestBean {

    @Size(min = 1, max = 2)
    private Seq<@Max(10) Integer> seqWithOneOrTwoDecimals = List.of(0);

    @NotEmpty
    private Either<String, @Positive Integer> mustNotBeLeftOrNull = Either.right(42);
    
    private Tuple3<@NotBlank String, @NotBlank String, @NotNull Integer> allElementsMustBeProvided =
        Tuple.of("a", "x", 3);

    @NotNull
    @NotEmpty
    private Map<@Pattern(regexp = "^[a-z]$") String, @NotBlank String> allCharKeysMustHaveNonBlankValues =
        HashMap.of("a", "Alice");
    
    // getters and setters
    
}

Considerations/Limitations

  • While it is possible to validate Try<T> and Lazy<T>, the usage of these monads for beanvalidation is questionable. Furthermore if the nested value of Lazy<T> is to be validated, evaluation will be forced and thus defeating the purpose of Lazy<T>.
  • Validation of io.vavr.control.Validation<T> is out of scope/undefined for obvious reasons.
io.vavr

vavr

Versions

Version
0.10.0
0.9.2