Skip to content

Latest commit

 

History

History
61 lines (55 loc) · 1.99 KB

README.md

File metadata and controls

61 lines (55 loc) · 1.99 KB

object-validator

Build Status codecov Maven Central

Simple Object Validator based on Hibernate validation library

Rules can be build by code or in the JSON format

  • KeyObject is any object you want to use as a key for your validation rules
  • classes is a classes you want to validate
  • fields is a fields you want to validate
  • constraints is validation rules
val key = KeyObject(officeId = 57, anotherField = "value")
val rule = DefaultConstraintMapping()
rule.type(TestDTO::class.java)
   .property("id", ElementType.FIELD)
   .constraint(NotEmptyDef().message("errorMessage"))
   .constraint(PatternDef().message("errorMessage").regexp("\\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b"))

validatorFactory.add(key, rule)
validatorFactory.validate(key, TestDTO("non valid"))

Representation of a file with rules for validation:

{
  "key": {
    "officeId": 57,
    "anotherField": "value"
  },
  "classes": [
    {
      "name": "com.isadounikau.TestDTO",
      "fields": [
        {
          "name": "id",
          "constraints": [
            {
              "type": "org.hibernate.validator.cfg.defs.NotEmptyDef",
              "errorMessage": "error"
            },
            {
              "type": "org.hibernate.validator.cfg.defs.PatternDef",
              "errorMessage": "error",
              "parameters": [
                {
                  "key": "regexp",
                  "value": "\\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}