Skip to content

Commit

Permalink
Reworking the API for starting validation and choosing a custom messa…
Browse files Browse the repository at this point in the history
…ge prefix. Bumping the major version due to the breaking changes.
  • Loading branch information
littleclay committed Sep 16, 2017
1 parent 790a965 commit 959d330
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 233 deletions.
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To install, you can simply include the dependency from Maven Central:
<dependency>
<groupId>com.redfin</groupId>
<artifactId>validity</artifactId>
<version>3.0.0</version>
<version>4.0.0</version>
</dependency>
```

Expand All @@ -39,18 +39,18 @@ Be careful when using primitive boolean validation that they return the given su
For example, `validate.that(false).isFalse()` will return `false`, not true.
If the validation were to fail then it wouldn't return false, but would rather throw an exception.

For best effect, you should statically import the two static `Validity` method entry points.
For best effect, you should statically import the static `Validity` method entry point.
```java
import static com.redfin.validity.Validity.validate;
import static com.redfin.validity.Validity.withMessage;
```

## Customization

The verifiable types are implemented with generics so that if a company or project wants to use the library but have different behavior than the default, they can.

A static class (like the `Validity` class itself) can be created that returns e a `VerifiableFactory` class with different `FailedValidationExecutor` implementations that handle the creation and throwing of Throwable's on failure. Implementations of that interface are where the stack trimming portions of the library are implemented.
The `VerifiableFactory` class can be sub-classed to add new, custom, verifiable types or to customize the entry point for specific throwable types on validation failure.
A static class (like the `Validity` class itself) can be created that returns a sub-class of the `AbstractVerifiableFactory` class with different `FailedValidationExecutor` implementations that handle the creation and throwing of Throwable's on failure.
Implementations of that interface are where the stack trimming portions of the library are implemented.
The `AbstractVerifiableFactory` class can be sub-classed to add new, custom, verifiable types or to customize the entry point for specific throwable types on validation failure.

## Descriptive Predicates

Expand All @@ -70,15 +70,15 @@ t -> null != t
### example

```java

import static com.redfin.validity.Validity.validate;

public final class Foo {

private final int i;

public Foo(int i) {
this.i = validate().that(i).isStrictlyPositive();
this.i = validate().that(i)
.isStrictlyPositive();
}
}

Expand All @@ -103,3 +103,21 @@ java.lang.IllegalArgumentException: Subject failed validation
at java.lang.reflect.Method.invoke(Method.java:498)
... more lines below, truncated for space
```

### example 2 (custom message prefix)
You can add in a custom prefix to the exception which would replace the "Subject failed validation" in the stack trace above.
You do this by adding the `withMessage` message to the factory instance you get from Validity before adding the subject that is to be validated.
```java
import static com.redfin.validity.Validity.validate;

public final class Foo {

private final int i;

public Foo(int i) {
this.i = validate().withMessage("A Foo instance needs a positive integer")
.that(i)
.isStrictlyPositive();
}
}
```
160 changes: 80 additions & 80 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>com.redfin</groupId>
<artifactId>validity</artifactId>
<version>3.0.0</version>
<version>4.0.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

Expand Down Expand Up @@ -69,80 +69,10 @@
<min.java.version>1.8</min.java.version>
</properties>

<!-- Deployment Settings & Build Profile -->

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<!-- Code Dependencies - Version Management -->

<dependencyManagement>

<!-- Code Dependency - Version Management -->

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -152,6 +82,16 @@
</dependencies>
</dependencyManagement>

<!-- Code Dependencies - Declarations -->

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>

<!-- Maven Plugin Version Management -->
Expand Down Expand Up @@ -293,13 +233,73 @@
</plugins>
</reporting>

<!-- Dependency - Declaration -->
<!-- Deployment Settings -->

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Loading

0 comments on commit 959d330

Please sign in to comment.