Building a simple DSL

We can now put to use what we learned about receiver and infix functions and create a DSL for validating objects. To keep it simple, we'll only be validating Int types.

First, we'll add the NumberValidator interface. This is where all the different validators can represent their validation logic:

interface NumberValidator 
{

fun isValueValid(value: Int): Boolean

}

Then, we need a type that will keep all the validators and the validated object. We'll name it Validator:

class Validator<T> private constructor(private val validatedObject: T) {

internal val validators = mutableListOf<NumberValidator>()
private lateinit var valueFactory: (T) -> Int
}

To enforce type safety, the Validator type is generic. It has a list of validators and also holds a function type called valueFactory, which knows how to return a property that will be validated. Notice how the constructor is private. We don’t allow the validator type to be created from outside; instead, we use an infix function that returns Validator instances:

companion object {
infix fun <T> validates(obj: T): Validator<T> {
return Validator(obj)
}
}

The Validator type also needs a function to check all validation rules have been satisfied:

fun isValid(): Boolean {
return validators.all { v -> v.isValueValid(valueFactory(validatedObject)) }
}

And, we need a function that will initiate setting validation rules and choosing a property that will be validated:

infix fun forProperty(factory: (T) -> Int): RuleBuilder<T> {
valueFactory = factory
return RuleBuilder(this)
}

The forProperty function accepts a lambda with one parameter; the generic T represents the validated object and returns an Int. The full-blown validator would probably return another generic type, but since we are trying to keep it simple, we can only validate integers.

It returns a RuleBuilder object, which is used for defining various validation rules:

class RuleBuilder<T> internal constructor(internal val validator: Validator<T>) {

fun greaterThan(target: Int): RuleBuilder<T> {
validator.validators.add(object : NumberValidator {
override fun isValueValid(value: Int): Boolean {
return value > target
}
})
return this
}

fun lesserThan(target: Int): RuleBuilder<T> {
validator.validators.add(object : NumberValidator {
override fun isValueValid(value: Int): Boolean {
return value < target
}
})
return this
}

fun finishRules(): Validator<T> {
return validator
}
}

Again, to keep it simple, we offer only two validators, lesserThan and greaterThan. Since each function returns the RuleBuilder object, we can set multiple rules by chaining calls one after another. But, it'd also be nice to set all the rules in one function call, so let's add an extension function that accepts a receiver type, which will enable this:

infix fun <T> RuleBuilder<T>.withRules(init: RuleBuilder<T>.() -> Unit): Validator<T> {
init(this)
return this.validator
}

That's it, our mini DSL is ready. Let's see it in action. 

This is the type that we'll validate:

class Employee(val age: Int, val name: String)

And we'll use its age property for setting the validation rules:

val employee = Employee(35, "John Wayne")

val validator = Validator validates employee forProperty { e -> e.age } withRules {
lesserThan(60)
greaterThan(18)
}

val isValid = validator.isValid()

Thanks to our DSL, setting the validation rules reads almost like a proper English sentence.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.8.27