|
| 1 | +# Validation |
| 2 | + |
| 3 | +A simple and easy-to-use validation utility library. |
| 4 | + |
| 5 | +By Tom Ansill |
| 6 | + |
| 7 | +## Motivation |
| 8 | + |
| 9 | +I got tired of manually including null checks, empty array checks, and various kinds of checks against bad input parameters. |
| 10 | +So I decided to build a library to make things a bit easier for me. |
| 11 | + |
| 12 | +### Why not use `Objects.requireNonNull(T)`? |
| 13 | + |
| 14 | +`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: |
| 15 | +``` |
| 16 | +StringBuilder sb = null; |
| 17 | +sb.append("something"); // <-- This will throw NullPointerException because you are trying to de-reference a null object |
| 18 | +``` |
| 19 | + |
| 20 | +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. |
| 21 | +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. |
| 22 | +I think `IllegalArgumentException` is more appropriate exception to best describe the problem. |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +* Java 8 or better |
| 27 | +* Maven |
| 28 | + |
| 29 | +## Download |
| 30 | + |
| 31 | +**No Maven Repository available yet ):** |
| 32 | + |
| 33 | +For now, you need to build and install it on your machine. |
| 34 | + |
| 35 | +```bash |
| 36 | +$ git clone https://github.com/tomansill/java-validation |
| 37 | +$ cd java-validation |
| 38 | +$ mvn install |
| 39 | +``` |
| 40 | + |
| 41 | +Then include the dependency in your project's `pom.xml`: |
| 42 | + |
| 43 | +```xml |
| 44 | +<dependency> |
| 45 | + <groupId>com.ansill.validation</groupId> |
| 46 | + <artifactId>validation</artifactId> |
| 47 | + <version>0.1.0</version> |
| 48 | +</dependency> |
| 49 | +``` |
| 50 | + |
| 51 | +## How to use |
| 52 | + |
| 53 | +### Null Checks |
| 54 | + |
| 55 | +Use `Validation.assertNonnull(Object)` to assert that object is not null: |
| 56 | + |
| 57 | +```java |
| 58 | +import com.ansill.validation.Validation; |
| 59 | + |
| 60 | +public class Application{ |
| 61 | + public static void main(String[] args){ |
| 62 | + String message = null; |
| 63 | + Application application = new Application(); |
| 64 | + application.print(message); |
| 65 | + } |
| 66 | + public void print(String message){ |
| 67 | + Validation.assertNonnull(message); |
| 68 | + System.out.println(message); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +When you run the code, it will yield this message: |
| 74 | +``` |
| 75 | +Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to be non-null but is found to be null |
| 76 | + at Application.print(Application.java:10) |
| 77 | + at Application.main(Application.java:7) |
| 78 | +``` |
| 79 | + |
| 80 | +### Natural Number Checks |
| 81 | + |
| 82 | +Use `Validation.assertNaturalNumber(long)` to assert that number is a natural number (no negative number or zero): |
| 83 | + |
| 84 | +```java |
| 85 | +import com.ansill.validation.Validation; |
| 86 | + |
| 87 | +public class Application{ |
| 88 | + public static void main(String[] args){ |
| 89 | + Application application = new Application(); |
| 90 | + application.setPort(0); |
| 91 | + } |
| 92 | + private int port = 80; |
| 93 | + public void setPort(int port){ |
| 94 | + Validation.assertNaturalNumber(port, "port"); |
| 95 | + this.port = port; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +When you run the code, it will yield this message: |
| 101 | +``` |
| 102 | +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 |
| 103 | + at Application.setPort(Application.java:10) |
| 104 | + at Application.main(Application.java:6) |
| 105 | +``` |
| 106 | + |
| 107 | +### Non-Negative Checks |
| 108 | + |
| 109 | +Use `Validation.assertNonnegative(long)` to assert that number is a positive number: |
| 110 | + |
| 111 | +```java |
| 112 | +import com.ansill.validation.Validation; |
| 113 | + |
| 114 | +public class Application{ |
| 115 | + public static void main(String[] args){ |
| 116 | + Application application = new Application(); |
| 117 | + application.add((short) 0, -1); |
| 118 | + } |
| 119 | + public void add(short one, long two){ |
| 120 | + Validation.assertNonnegative(one); |
| 121 | + Validation.assertNonnegative(two); |
| 122 | + System.out.println(one + two); |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +When you run the code, it will yield this message: |
| 128 | +``` |
| 129 | +Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to be non-negative but value is actually a negative number |
| 130 | + at Application.add(Application.java:10) |
| 131 | + at Application.main(Application.java:6) |
| 132 | +``` |
| 133 | + |
| 134 | +### Non-Empty String Checks |
| 135 | + |
| 136 | +Use `Validation.assertNonemptyString(String)` to assert that String is non-empty: |
| 137 | + |
| 138 | +```java |
| 139 | +import com.ansill.validation.Validation; |
| 140 | + |
| 141 | +public class Application{ |
| 142 | + public static void main(String[] args){ |
| 143 | + Application application = new Application(""); |
| 144 | + } |
| 145 | + public final String name; |
| 146 | + public Application(String name){ |
| 147 | + Validation.assertNonemptyString(name, "name"); |
| 148 | + this.name = name; |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +When you run the code, it will yield this message: |
| 154 | +``` |
| 155 | +Exception in thread "main" java.lang.IllegalArgumentException: Value in variable 'name' is expected to be non-empty but value is actually a empty string |
| 156 | + at Application.<init>(Application.java:9) |
| 157 | + at Application.main(Application.java:5) |
| 158 | +``` |
| 159 | + |
| 160 | +### Non-Empty Array/Collection |
| 161 | + |
| 162 | +Use `Validation.assertNonempty(Object[])` or `Validation.assertNonempty(Collection)` to assert that Array/Collection is non-empty: |
| 163 | + |
| 164 | +```java |
| 165 | +import com.ansill.validation.Validation; |
| 166 | + |
| 167 | +import java.util.*; |
| 168 | + |
| 169 | +public class Application{ |
| 170 | + public static void main(String[] args){ |
| 171 | + Application application = new Application(Collections.emptyList()); |
| 172 | + } |
| 173 | + public final Collection hostnames; |
| 174 | + public Application(Collection hostnames){ |
| 175 | + Validation.assertNonempty(hostnames); |
| 176 | + this.hostnames = hostnames; |
| 177 | + } |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +When you run the code, it will yield this message: |
| 182 | +``` |
| 183 | +Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to be non-empty but value is actually empty |
| 184 | + at Application.<init>(Application.java:11) |
| 185 | + at Application.main(Application.java:7) |
| 186 | +``` |
| 187 | + |
| 188 | +### Array/Collection Member Null Check |
| 189 | + |
| 190 | +Use `Validation.assertNonnullElements(Object[])` or `Validation.assertNonnullElements(Collection)` to assert that Array/Collection does not have null elements: |
| 191 | + |
| 192 | +```java |
| 193 | +import com.ansill.validation.Validation; |
| 194 | + |
| 195 | +import java.util.*; |
| 196 | + |
| 197 | +public class Application{ |
| 198 | + public static void main(String[] args){ |
| 199 | + Application application = new Application(Arrays.asList("google.com", null, "github.com", null, null, "reddit.com")); |
| 200 | + } |
| 201 | + public final Collection hostnames; |
| 202 | + public Application(Collection hostnames){ |
| 203 | + Validation.assertNonnullElements(hostnames, false); |
| 204 | + this.hostnames = hostnames; |
| 205 | + } |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +When you run the code, it will yield this message: |
| 210 | +``` |
| 211 | +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] |
| 212 | + at Application.<init>(Application.java:11) |
| 213 | + at Application.main(Application.java:7) |
| 214 | +``` |
0 commit comments