validator | Rule Based Validation Library in Android

Mustafa Yiğit
Kt. Academy
Published in
3 min readJun 13, 2022

--

Recently, I wrote a rule-based validation structure for all my inputs. It’s very flexible and manageable. Today I will try to explain the validator library.

Import

Validator, consist 4 components:

  • IValidatable
  • ValidatableRule
  • NotiftyType
  • Validator

IValidatable

It’s an interface to validate a rule. It includes input, rules and current error text.

Also, there is a function exist here called isValid(notiftyType). It must override this function which class implemented IValidatable.

var input: String //input field value

var currentErrorText: String //invalid rule error text

val rules: MutableList<BaseValidatableRule> //input's validatable rules

ValidatableRule

ValidatableRule extends from BaseValidatableRule. It’s ready to use common rules for input validation.

open class BaseValidatableRule (
open val errorMessage: String,
open val notifyType: NotifyType,
open val rule: (input: String) -> Boolean
)

BaseValidatableRule consist of 3 properties:

  • errorMessage
  • notifyType
  • rule → an action to return is valid

NotifyType

enum class NotifyType {
ON_VALUE_CHANGE,
ON_FOCUS_CHANGE,
ON_FORM_SUBMIT
}

NotifyType, represents a common way of starting form validation. We might want to validate an input on different user interactions: on text change, on focus lost or on form main action click.

Validator

And the magic is here. Everything happens in the Validator class. It implements IValidatable.

class Validator(
override var onValidation(Booelan, String?, NotifyType) -> Unit
) : IValidatable

Validator requires action on the constructor. The action is to give validation values to us. (isValid, errorText, notifyType)

constructor(
vararg rules: BaseValidatableRule,
onValidation: (Boolean, String?, NotifyType) -> Unit
) : this(onValidation) {
this.rules.addAll(rules)
}

You can use another constructor with a rule list (vararg → one or many rule) and onValidation action, but I recommend Builder.

Validator like an abstract class. It hides common logical things.

isValid function works with notifyType. It filters the rules by notifyType and starts validation control of the matches.

currentErrorText updates if any rule’s action returns false in the filteredRules.

Error text change only with filtered rules but isValid control works with all rules.

Every value change of input or isFocused, validate function will be triggered with related NotifyType.

  • input change → NotifyType.ON_VALUE_CHANGE
  • isFocused change → NotifyType.ON_FOCUS_CHANGE

You must call submitForm() function if you check only NotifyType.ON_FORM_SUBMIT rules. It return the boolean isValid value.

Builder

Builder, a class to create a validate instance with few steps.

  • add rule one or many with addRules
  • add data collector (input update, focus update) with addCollector
  • manage validation results in onValidate block
  • get validate instance with build()

And that’s a brief info about the Validator. If you want to see more you should take a look at Github repository.

Don’t keep your pull request or messages about it. If you like Validator, please drop 👏 and ⭐️ ⭐️

Github:

Linkedin:

--

--