| src | 6 months ago | ||
| .gitignore | 6 years ago | ||
| Jenkinsfile | 2 years ago | ||
| README.md | 6 months ago | ||
| lombok.config | 5 years ago | ||
| pom.xml | 1 year ago | ||
Annotate member variables for validation
public class User {
@Alpha
private String firstName;
}
Instantiate
Validator validator = new Validator(object);
Validate
validator.validate();
Check validity of object
validator.isValid();
Get invalid Items
List<Item> items = validator.getInvalidItems();
Get member name for invalid Item
items.get(0).getMemberName();
Get error messages for invalid Item
List<String> errors = items.get(0).getErrorMessages();
Get all error messages across every invalid Item (deduplicated)
Set<String> messages = validator.getErrorMessages();
Validate nested objects recursively
new Validator(object).recursive().validate();
When recursive() is enabled, the Validator descends into non-java.*, non-primitive, non-enum reference fields and validates their annotated members. Collection elements are always traversed (regardless of the recursive() flag) when the element type is itself a user-defined class.
Validates variable consists of only letters
@Alpha
Data types supported
Validates variable consists of only letters and/or numbers
@AlphaNumeric
Data types supported
Validates against a custom class
@CustomValidator(MyEvaluation.class)
Required
Note: Custom validation class must implement the Evaluation interface
Validates a string is in a given date format
@DateFormat("yyyy-MM-dd")
Data types supported
Format used by java.text.SimpleDateFormat
Validates variable is an email address
Data types supported
Validates variable equals given value
@ExactMatch(match = "match", message = "message string")
@ExactMatch(match = "match", message = "message string", caseSensitive = false)
@ExactMatch(matches = {"PST", "MST", "CST", "EST"}, message = "message string")
Options
Data types supported
Note: matches array will override match string
Validates variable is of given length
@Length(4)
Required
Data types supported
Validates variable less than or equal to given length
@MaxLength(4)
Required
Data types supported
Validates variable is greater than or equal to given length
@MinLength(4)
Required
Data types supported
Validates variable is not empty
@NotEmpty
Data types supported
Validates variable is not null
@NotNull
Data types supported
Validates if variable is a number
@Number
Data types supported
Validates variable consists of only numbers
@Numeric
Data types supported
Will override all other validations if variable is null
@Optional
Data types supported
Validates variable matches given regex
@Regex("^[a-zA-Z]+$")
Required
Data types supported
Validates variable is of given size
@Size(3)
Required
Data types supported
Validates variable is between two given numbers
@Between(minimum = 3.2, maximum = 4, inclusive = true)
Optional
Required
Data types supported
Validates variable is greater than given number
@GreaterThan(3.2)
Required
Data types supported
Validates variable is greater than or equal to given number
@GreaterThanOrEqualTo(3.2)
Required
Data types supported
Validates variable is less than given number
@LessThan(3.2)
Required
Data types supported
Validates variable is less than or equal to given number
@LessThanOrEqualTo(3.2)
Required
Data types supported
Validates variable is not between two given numbers
@NotBetween(minimum = 3.2, maximum = 4, inclusive = true)
Optional
Required
Data types supported
Validates variable contains a given value
@Contains(text = "contain") @Contains(text = "contain", caseSensitive = false)
Options
Data types supported
Validates variable starts with given value
@StartsWith(text = "Begins") @StartsWith(text = "Begins", caseSensitive = false)
Options
Data types supported
Validates variable begins with given value
@EndsWith(text = "Ends") @EndsWith(text = "Ends", caseSensitive = false)
Options
Data types supported
Compile, test, and install the library locally with Maven:
mvn clean install
Run only the test suite:
mvn test
commons-lang3, reflection-utils, lombok (provided), junit (test).Recent changes in review-and-improve:
IllegalAccessException path in Validator.findItems where the same private field was first read with FieldUtils.readField(..., true) but later re-read via Field.get(...), which could throw on some JDKs. The previously-read value is now reused.AlphaValidation to honour its documented Collection and Array support. It previously stringified the value before invoking AlphaEvaluation, defeating the collection/array branches of the evaluator.java.* (for example, String, Number, Boolean) are no longer traversed as embedded objects. This avoids InaccessibleObjectException failures on JDK 9+ when a field happens to be a List<String>.Contains, EndsWith, StartsWith) and ExactMatchesEvaluation to remove redundant ? true : false patterns and use anyMatch / equalsIgnoreCase.notBeteen -> notBetween) in NotBetweenValidation.Item, the recursive() feature, the getErrorMessages() aggregation/deduplication behaviour, and extended OptionalValidationTest, AlphaValidationTest, and AlphaNumericValidationTest to cover collection and array fields.