Parameter Validation Utils

Validating input parameters of methods and constructors is a recurring and stupid task, especially when you are writing public APIs. This module tries to ease your life a bit by providing a static validateThat() method, allowing you to throw IllegalArgumentExceptions using hamcrest matchers.

License

License

GroupId

GroupId

info.gehrels
ArtifactId

ArtifactId

parameter-validation
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

jar
Description

Description

Parameter Validation Utils
Validating input parameters of methods and constructors is a recurring and stupid task, especially when you are writing public APIs. This module tries to ease your life a bit by providing a static validateThat() method, allowing you to throw IllegalArgumentExceptions using hamcrest matchers.
Project URL

Project URL

https://github.com/BGehrels/parameter-validation
Source Code Management

Source Code Management

https://github.com/BGehrels/parameter-validation

Download parameter-validation

How to add to project

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

Dependencies

compile (1)

Group / Artifact Type Version
org.hamcrest : hamcrest-core jar [1.2,1.3)

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

Parameter validation using Hamcrest matchers

Validating input parameters of methods and constructors is a recurring and stupid task, especially when you are writing public APIs. This module tries to ease your life a bit by providing a static validateThat() method, allowing you to throw IllegalArgumentExceptions using hamcrest matchers.

Using the matchers is as easy as

import static info.gehrels.parameterValidation.MatcherValidation.validateThat;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.isEmptyOrNullString;

...

public MyConstructor(String name) {
	this.name = validateThat(name, not(isEmptyOrNullString()));
}

or

validateThat("parameter 'name has an invalid value", name, not(isEmptyOrNullString()));

If it fails to validate the parameter, an Exception is thrown:

Exception in thread "main" java.lang.IllegalArgumentException: parameter 'name has an invalid value
Expected: not (null or an empty string)
     but: was null
	at info.gehrels.parameterValidation.MatcherValidation.validateThat(MatcherValidation.java:40)
	...

How to get this?

You will find the lib at Maven Central. Just add the following lines to your pom.xml:

<dependency>
	<groupId>info.gehrels</groupId>
    <artifactId>parameter-validation</artifactId>
	<version>1.1</version>
</dependency>

How is it licensed?

Since the code ist mostly copied from the original Hamcrest sources, i do not claim copyright myself. The code therefore falls under Hamcrests BSD 3-Clause License. It may be found at https://raw.github.com/hamcrest/JavaHamcrest/master/LICENSE.txt

Versions

Version
1.1
1.0