# Validator Class

### Basic Concepts

The `EasyValidation` library performs all the validation checks based on the [`Validator`](https://github.com/wajahatkarim3/EasyValidation/blob/develop/easyvalidation-core/src/main/java/com/wajahatkarim3/easyvalidation/core/Validator.kt) class. You can easily perform validations on any `String` using this class.

The easiest way to validate any string is to create an `Object` of `Validator` class and pass any `String` in it and then you can perform validation checks.

```kotlin
var myStr = "test.email@domain.com"
var validator = Validator(myStr)
var isValid = validator.validEmail()
```

Now, the `Validator` class contains a lot of [built-in rules](https://wajahatkarim.gitbook.io/easyvalidation/usage/built-in-rules) in form of methods. For example, to check valid email, you can use `validEmail()` method, or check non-empty `String`, you can use `nonEmpty()` method and so on. Each method returns a `Boolean` value, where `true` means `String` is valid and `false` means `String` is not valid. For example:

```kotlin
var myStr = "hello my string"
var validator = Validator(myStr)
var isValidStr = validator.nonEmpty()
```

Also, each method has an optional error callback method. This will be called in case on non-valid `String`. For example:

```kotlin
var myNumber = "1234567"
var validator = Validator(myNumber)

// Normal Way
var isValid = validator.minLength(6, { msg -> 
    // This method will be called when myNumber is less than 6 characters.
    Log.e("EasyValidation", msg)
})

// Kotlin Way
var isValid = validator.minLength(6)
{
    // This method will be called when myNumber is less than 6 characters.
    Log.e("EasyValidation", it)
}

// Or you can skip the returned Boolean value
validator.minLength(6)
{
    // This method will be called when myNumber is less than 6 characters.
    Log.e("EasyValidation", it)
}
```

### Error and Success Callbacks

The `Validator` class provides callback methods with `addErrorCallback()` and  `addSuccessCallback()` methods. For example:

```kotlin
var myNumber = "1234567"
var validator = Validator(myNumber)

validator.startsWithNumber()
    .addErrorCallback( { msg -> 
        // Do something here with the error.
        // The msg variable contains the error message
    })
    .addSuccessCallback( {
        // Validation checks are passed. The text is valid.
        // Do anything with success case here.
    })
    .check()
```

### Validator Extensions

You can access the Validator object by using [Kotlin extension ](https://kotlinlang.org/docs/reference/extensions.html)methods on `String`, `EditText`, `TextView`, `AutoCompleteTextView`, `TextInputLayout`, and `Spinner` using `validator()` method. For example:

```kotlin
var strValidator = myStr.validator()
var txtViewValidator = myTextView.validator()

// Then all usage is same
strValidator.nonEmpty()
{
    // Invalid error here
}

// Or in simple way
myStr.validator().nonEmpty()
{
   // Invalid error
}

myTextView.validator().nonEmpty()
{
   // invalid error 
}
```

### Adding Rules Manually

You can add [built-in rules](https://wajahatkarim.gitbook.io/easyvalidation/usage/built-in-rules) or [custom rules](https://wajahatkarim.gitbook.io/easyvalidation/usage/create-custom-rules) by calling `addRule()` method of the `Validator` class.

```kotlin
var validator = myTextView.validator()
var isValid = validator.addRule(EmailRule()).check()
```
