RebbVal
EN
EN
  • Getting Started
  • Overview
  • Concepts
  • List of rules
    • Age
    • Array
    • Boolean
    • Comparions
    • Composite
    • Datetime
    • Identifications
    • Internet
    • Localization
    • String
Powered by GitBook
On this page
  • Input
  • Datatype in input
  • Rule
  • Datatype in rule
  • Result
  • Error
  • Custom Validator
  • Timezone

Was this helpful?

Concepts

Input

input is the object to be validated, it is the first parameter of the val function.

RebbVal support following datatypes:

Datatype in input

Boolean

Number

Include integer, long, float, double, all input numbers will be casted to BigDecimal(in Java implementation).

Date

In Java implementation,the input could been a java.uitl.Date

RebbValhas a functiondate()to parse string to Date

RebbVal v = new RebbVal();
v.val(v.date("2010-05-01"),"between 2010-01-01 and 2020-01-01"); // true 

// custom pattern for parser
v.val(v.date("2020/12/31","yyyy/MM/dd"), "[2020-01-01, 2021-12-31]"); //true

RebbValhas a functionyear()to convert string to Date

v.val(v.year("2000"), "is leapyear"); //true

String

Rule

Rule is the second parameter of theval function

A rule is just a string, the supported rule grammar are:

  • comparions: =, !=, >,<,>=,<=

  • between .. and ..

  • interval

  • in (array)

  • contains

  • not empty

  • max length

  • is (true, false, phone, email, ipv4,url, ect.)

  • is (custom function)

  • match (regex)

  • older than/younger than

  • starts with/ends with

Rules can be negated by using not operation

Rules can be combined with and, or

Datatype in rule

array

// array of numbers
[1,2,3.0]

// array of strings
['foo','bar']

boolean

true

false

number

// integer
1
-300
// float
3.1415
-39.

// not supported yet
// .8
// 80%
// 1.05e8

date

// yyyy-MM-dd
2014-12-01

// without leading zeros
2020-1-1

regex

// the delimiter is slash
/^\d+$/

//modifier support:g,i,m,s,x,u,U
/\d+/g

string

// using single quotes
'a string'

Result

The result of the val function is a boolean.

If result is true, means all rules are satisfied, if false, you can check errors by calling getErrors function

Error

If the result of val is false, or hasError function returns true, then the validataion failed.

You can call getErrors function, which returns an array of string(in Java, an ArrayList<String>), to get the detail message.

v.val(8.8,">=10"); // false
v.getErrors().size() // 1
v.getErrors().get(0) // 8.8 >=10 failed

Custom Validator

Write a class that implements com.rebb.val.IValidator interface

package com.rebb.valid;

public interface IValidator {
    public boolean run(Object obj) throws ValidationException;
}

In the run function, do your validate work and return a boolean as validation result.

package com.example.valid;

public class DemoCustomValidator implements IValidator{

    @Override
    public boolean run(Object obj) throws ValidationException {
        if(obj instanceof String)
        {
            String object = (String)obj;
            return object.equals("Demo");
        }
        else
        {
            throw new ValidationException("Unsupported object type");
        }
    }
}

After initialize the RebbVal object, call registerCustomValidator function

Valid v = new Valid();
v.registerCustomValidator("Demo", DemoCustomValidator.class);
v.val("Demo","is Demo"); // true

Timezone

When we deal with date, timezone is always the first consideration, you can set the timezone like this:

Valid v = new Valid();
v.setTimezone(Locale.CHINA);
PreviousOverviewNextAge

Last updated 3 years ago

Was this helpful?