Validator provides a comprehensive set of annotations that simplify validation by allowing you to declaratively define validation rules directly on your POJO fields. Instead of manually creating validators and chaining rules, you can annotate your class fields and use the static Validator.validOrFail(object) method to validate all annotated fields at once.
Annotations in Validator offer the same validation capabilities as programmatic rules, but with a more declarative syntax. Each annotation corresponds to a predefined rule and can be applied to String fields in your Java classes.
You can apply multiple annotations to a single field. They will be evaluated in the order they are declared, and validation stops at the first failure:
import io.github.ApamateSoft.validator.annotations.*;import static io.github.ApamateSoft.validator.utils.Alphabets.*;public class RegisterForm { @Required(message = "Required") @MinLength(min = 8, message = "At least 8 characters required") @MustContainMin(min = 3, alphabet = ALPHA_LOWERCASE, message = "At least 3 lowercase letters required") @MustContainMin(min = 3, alphabet = NUMBER, message = "At least 3 numbers required") @MustContainOne(alphabet = ALPHA_UPPERCASE, message = "At least one uppercase letter required") @MustContainOne(alphabet = "@#*-", message = "At least one special character required") @Compare(compareWith = "passwordConfirm", message = "Passwords do not match") private String password; private String passwordConfirm; // Constructor, getters, setters...}
Default messages are provided in English (default) and Spanish. You can change the language globally:
import io.github.ApamateSoft.validator.Validator;import io.github.ApamateSoft.validator.messages.MessagesEs;public class Main { public static void main(String[] args) { Validator.setMessages(new MessagesEs()); }}
Annotations simplify rule chaining, but do not allow adding custom validation rules. If you need custom logic, use programmatic validation with the Validator.Builder() approach.
Annotations are evaluated in the order they appear on the field. When a validation fails, remaining annotations on that field are not evaluated.