项目作者: siddhant0509

项目描述 :
POJO Validator Using Annotations
高级语言: Java
项目地址: git://github.com/siddhant0509/domain-validator.git
创建时间: 2017-09-10T15:51:07Z
项目社区:https://github.com/siddhant0509/domain-validator

开源协议:

下载


Domain Validator

Latest release: [0.1.1]

An annotation based validator for POJOs with Runtime exceptions on failures.
Programmer can define static functions in a class and define an Annotation
over method before which validation needs to happen.
Spring based wrapper based on spring annotation available for uage with spring framework.

Installation

Build using gradle

Usage

  1. class Order{}
  2. class OrderService {
  3. @ValidateBy(clazz = CreateOrderValidator.class)
  4. public Order create(Order order){
  5. return new Order();
  6. }
  7. }
  8. @Validator(clazz = Order.class, onFail = "Order creation failed")
  9. class CreateOrderValidator{
  10. @OnFail(onFail = "Order is not ok")
  11. public static boolean isOrderOk(Order order){
  12. return false;
  13. }
  14. public static boolean isOrderOk1(Order order){
  15. return true;
  16. }
  17. }

Usually, create and update methods have some validations which are required to pass. Such logic usually leads to some bad
design and code smells. It makes sense to collect all such methods into the same class and collect them at runtime.
By defining class name(CreateOrderValidator) over which validation needs to be done, this library can invoke all validation methods
at runtime.
On the validator class we define the class on which validation needs to be done alongwith general error message on validation failure.
For a function to be invoked it should have following properties:

  1. Return type should be Boolen(or primitive counterpart boolean)
  2. Method should have only one argument, same as the object being validated.
  3. For now, method has to be static(will add functionality for non static methods)