Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

9 Commits

Repository files navigation

Validation

A simple and easy-to-use validation utility library.

By Tom Ansill

Motivation

I got tired of manually including null checks, empty array checks, and various kinds of checks against bad input parameters. So I decided to build a library to make things a bit easier for me.

Why not use Objects.requireNonNull(T)?

Objects.requireNonNull(T) will throw NullPointerException and I disagree with this choice of exception because NullPointerException is supposed to be thrown in event of null object being de-referenced. Like:

StringBuilder sb = null;
sb.append("something"); // <-- This will throw NullPointerException because you are trying to de-reference a null object

So, hence the name NullPointerException, you get the exception because you are attempting to de-reference a null value and it blows up on you. I don't believe this is the case here where I want to ensure that parameters that are passed in is not null. I'm not necessarily trying to dereference it. I think IllegalArgumentException is more appropriate exception to best describe the problem.

Prerequisites

  • Java 8 or better

Download

Package Repository

The library is availble for download on Sonatype public Maven repository (https://oss.sonatype.org/#nexus):

<dependency>
<groupId>com.ansill.validation</groupId>
<artifactId>validation</artifactId>
<version>0.2.2</version>
</dependency>

Build

Maven (or other similar build tools) is needed to build and install JavaValidation.

$ git clone https://github.com/tomansill/javavalidation
$ cd javavalidation
$ mvn install

Then include the dependency in your project's pom.xml:

<dependency>
<groupId>com.ansill.validation</groupId>
<artifactId>validation</artifactId>
<version>0.2.2</version>
</dependency>

How to use

Null Checks

Use Validation.assertNonnull(Object) to assert that object is not null:

importcom.ansill.validation.Validation;
publicclassApplication{
publicstaticvoidmain(String[] args){
Stringmessage = null;
Applicationapplication = newApplication();
application.print(message);
}
publicvoidprint(Stringmessage){
System.out.println(Validation.assertNonnull(message));
}
}

When you run the code, it will yield this message:

Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to be non-null but is found to be null
at Application.print(Application.java:9)
at Application.main(Application.java:7)

Natural Number Checks

Use Validation.assertNaturalNumber(long) to assert that number is a natural number (no negative number or zero):

importcom.ansill.validation.Validation;
publicclassApplication{
publicstaticvoidmain(String[] args){
Applicationapplication = newApplication();
application.setPort(0);
}
privateintport = 80;
publicvoidsetPort(intport){
this.port = Validation.assertNaturalNumber(port, "port");
}
}

When you run the code, it will yield this message:

Exception in thread "main" java.lang.IllegalArgumentException: Value in variable 'port' is expected to be a natural number (1, 2, ..., N-1, N) but it is actually not a natural number
at Application.setPort(Application.java:9)
at Application.main(Application.java:6)

Non-Negative Checks

Use Validation.assertNonnegative(long) to assert that number is a positive number:

importcom.ansill.validation.Validation;
publicclassApplication{
publicstaticvoidmain(String[] args){
Applicationapplication = newApplication();
application.add((short) 0, -1);
}
publicvoidadd(shortone, longtwo){
Validation.assertNonnegative(one);
Validation.assertNonnegative(two);
System.out.println(one + two);
}
}

When you run the code, it will yield this message:

Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to be non-negative but value is actually a negative number
at Application.add(Application.java:10)
at Application.main(Application.java:6)

Non-Empty String Checks

Use Validation.assertNonemptyString(String) to assert that String is non-empty:

importcom.ansill.validation.Validation;
publicclassApplication{
publicstaticvoidmain(String[] args){
Applicationapplication = newApplication("");
}
publicfinalStringname;
publicApplication(Stringname){
Validation.assertNonemptyString(name, "name");
this.name = name;
}
}

When you run the code, it will yield this message:

Exception in thread "main" java.lang.IllegalArgumentException: Value in variable 'name' is expected to be non-empty but value is actually a empty string
at Application.<init>(Application.java:9)
at Application.main(Application.java:5)

Non-Empty Array/Collection

Use Validation.assertNonempty(Object[]) or Validation.assertNonempty(Collection) to assert that Array/Collection is non-empty:

importcom.ansill.validation.Validation;
importjava.util.*;
publicclassApplication{
publicstaticvoidmain(String[] args){
Applicationapplication = newApplication(Collections.emptyList());
}
publicfinalCollectionhostnames;
publicApplication(Collectionhostnames){
Validation.assertNonempty(hostnames);
this.hostnames = hostnames;
}
}

When you run the code, it will yield this message:

Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to be non-empty but value is actually empty
at Application.<init>(Application.java:11)
at Application.main(Application.java:7)

Array/Collection Member Null Check

Use Validation.assertNonnullElements(Object[]) or Validation.assertNonnullElements(Collection) to assert that Array/Collection does not have null elements:

importcom.ansill.validation.Validation;
importjava.util.*;
publicclassApplication{
publicstaticvoidmain(String[] args){
Applicationapplication = newApplication(Arrays.asList("google.com", null, "github.com", null, null, "reddit.com"));
}
publicfinalCollectionhostnames;
publicApplication(Collectionhostnames){
Validation.assertNonnullElements(hostnames, false);
this.hostnames = hostnames;
}
}

When you run the code, it will yield this message:

Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to have all of its list members to be non-null but the list contains null members. Invalid members are located at indices [1, 3, 4]
at Application.<init>(Application.java:11)
at Application.main(Application.java:7)

About

Simple and Easy-To-Use Validation Utility Library for Java

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages