Skip to content

Instantly share code, notes, and snippets.

@hamurcuabi
Last active September 18, 2021 13:50
Show Gist options
  • Save hamurcuabi/6edb6b5c55da36aac0bc004f330a232c to your computer and use it in GitHub Desktop.
Save hamurcuabi/6edb6b5c55da36aac0bc004f330a232c to your computer and use it in GitHub Desktop.
Generic object validation helper
data class User(
var name: String = "", // No empty, min 5 character
var surname: String = "", //Not empty
var email: String = "", // Not empty, Email format
var age: Int = 0, // min age 18
)
package com.emrehmrc.validator
/**
* To avoid using if and make it simple we will wrap it with this class
*/
sealed class ValidationState {
object Valid : ValidationState()
data class InValid(val errorMessage: String?) : ValidationState()
}
package com.emrehmrc.validator
/**
* Rev 1.0
* Author EmreHamurcu
* Date 9/17/2021
* FileName Validator
*/
/**
* Rule is our business logic
* @param T is a generic type of any
*/
typealias Rule<T> = (value: T?) -> Boolean
class Validator<T>(private val data: T) {
private val validationRules = ArrayList<Rule<T>>()
private var errorMessages = ArrayList<String?>()
/**
* @param errorMsg if condition is wrong should return this
* @param rule business logic
*
* @sample addRule("Name Cannot be empty") { it?.name.isNullOrEmpty() }
*/
fun addRule(errorMsg: String, rule: Rule<T>) {
validationRules.add(rule)
errorMessages.add(errorMsg)
}
/**
* Returns ValidationState
*/
fun isValid(): ValidationState {
for (i in 0 until validationRules.size) {
if (validationRules[i](data)) {
val errorMsg = errorMessages[i]
return ValidationState.InValid(errorMsg)
}
}
return ValidationState.Valid
}
/**
* Returns a list of invalid ValidationState
*/
fun invalidList(): List<ValidationState> {
val pairList = ArrayList<ValidationState>()
for (i in 0 until validationRules.size) {
if (validationRules[i](data)) {
val errorMsg = errorMessages[i]
pairList.add(ValidationState.InValid(errorMsg))
}
}
return pairList
}
/**
* Returns a list of valid ValidationState
*/
fun validList(): List<ValidationState> {
val pairList = ArrayList<ValidationState>()
for (i in 0 until validationRules.size) {
if (validationRules[i](data).not()) {
pairList.add(ValidationState.Valid)
}
}
return pairList
}
}
package com.emrehmrc.validator
/**
* Rev 1.0
* Author EmreHamurcu
* Date 9/17/2021
* FileName ValidatorResolver
*/
class ValidatorResolver(private val validators: List<Validator<*>>) {
fun isValid(): ValidationState {
for (validator in validators) {
when (val state = validator.isValid()) {
is ValidationState.InValid -> return state
is ValidationState.Valid -> Unit
}
}
return ValidationState.Valid
}
fun inValidList(): List<ValidationState> {
val invalidList = ArrayList<ValidationState>()
for (validator in validators) {
when (val state = validator.isValid()) {
is ValidationState.InValid -> invalidList.add(state)
is ValidationState.Valid -> Unit
}
}
return invalidList
}
fun validList(): List<ValidationState> {
val validList = ArrayList<ValidationState>()
for (validator in validators) {
when (val state = validator.isValid()) {
is ValidationState.InValid -> Unit
is ValidationState.Valid -> validList.add(state)
}
}
return validList
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment