hope-and-doubt

lightweight validation library

License

License

GroupId

GroupId

com.martiansoftware
ArtifactId

ArtifactId

hope-and-doubt
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

hope-and-doubt
lightweight validation library
Project URL

Project URL

https://github.com/martylamb/hope-and-doubt
Source Code Management

Source Code Management

https://github.com/martylamb/hope-and-doubt

Download hope-and-doubt

How to add to project

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

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

hope and doubt

Hope and doubt is a tiny library aimed at simplifying validation of arguments to methods, especially constructors and setters/getters. It has no external dependencies and is under 9 KB at the time of this writing. It provides both optimistic and pessimistic validators to suit your needs. The differences are:

  • Optimistic validators assume that the input is valid and throw unchecked exceptions if the input is invalid. Optimistic validators are created via Hope.that()
  • Pessimistic validators assume that the input is invalid and throw checked exceptions if this turns out to be the case. Pessimistic validators are created via Doubt.that()

get via maven

<dependency>
    <groupId>com.martiansoftware</groupId>
    <artifactId>hope-and-doubt</artifactId>
    <version>0.1.0</version>
</dependency>

quick examples

Optimistically ensure that the "name" parameter is neither null nor an empty String:

public void setName(String name) {
    String validatedName = Hope.that(name).isNotNullOrEmpty().value();
    ...
}

If name is null, this will fail with:

Exception in thread "main" com.martiansoftware.validation.UncheckedValidationException: value must not be null

If name is an empty String, it will fail with

Exception in thread "main" com.martiansoftware.validation.UncheckedValidationException: value must not be empty

Pessimistically ensure that the "name" parameter is neither null nor an empty String:

public void setName(String name) throws CheckedValidationException {
    String validatedName = Doubt.that(name).isNotNullOrEmpty().value();
    ...
}

If name is null, this will fail with:

Exception in thread "main" com.martiansoftware.validation.CheckedValidationException: value must not be null

If name is an empty String, it will fail with

Exception in thread "main" com.martiansoftware.validation.CheckedValidationException: value must not be empty

Naming the validated Object for better error messages

public void setName(String name) {
    String validatedName = Hope.that(name).named("a person's name").isNotNullOrEmpty().value();
    ...
}

If name is null, this will fail with:

Exception in thread "main" com.martiansoftware.validation.UncheckedValidationException: a person's name must not be null

Providing a default value

public void setName(String name) {
    String validatedName = Hope.that(name).orElse("Anonymous").isNotNullOrEmpty().value();
    ...
}

Collections, arrays, and other things that can be empty

public void setFoo(List<Foo> fooList, Bar[] barArray) {
    List<Foo> myFoos = Hope.that(fooList).isNotNullOrEmpty().value();
    Bar[] myBars = Hope.that(barArray).isNotNullOrEmpty().value();
    ...
}

Custom validation logic via Predicates

public void setName(String name) {
    String validatedName = Hope.that(name)
                               .isNotNullOrEmpty()
                               .isFalse(n -> n.equalsIgnoreCase("anonymous"))
                               .value();
    ...
}

Custom validation logic via Predicates with custom error messages

public void setAge(Integer age) {
    String validatedAge = Hope.that(name)
                               .isNotNullOrEmpty()
                               .isFalse(n -> n < 0,
                                       "negative ages are not allowed")
                               .value();
    ...
}

Matching regular expressions

public void setName(String name) {
    String validatedName = Hope.that(name)
                               .orElse("Mrs. Jane Doe")
                               .isNotNullOrEmpty()
                               .matchesAny("^Mrs?\\. .*", // accepts "Mr. or "Mrs."
                                           "^Dr\\. .*")   // accepts "Dr."
                               .value();
    ...
}

Mapping inputs to different objects

public void setBaz(String baz) {
    int i = Hope.that(baz).isNotNullOrEmpty().map(Integer::valueOf).value();
    ...
}

What else does it do?

Take a look at the source code for Validator.java

Versions

Version
0.1.0