# 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`

{% hint style="info" %}
`RebbVal`has a function`date()`to parse `string` to `Date`
{% endhint %}

```java
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
```

{% hint style="info" %}
`RebbVal`has a function`year()`to convert `string` to `Date`
{% endhint %}

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

#### String

## Rule

Rule is the second parameter of the`val` 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

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

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

#### boolean

```java
true

false
```

#### number

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

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

#### date

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

// without leading zeros
2020-1-1
```

#### regex

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

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

```

#### string

```java
// 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.

```java
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

```java
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.

```java
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

```java
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:

```java
Valid v = new Valid();
v.setTimezone(Locale.CHINA);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://willking.gitbook.io/rebb-val/concepts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
