- Readable and declarative validation rules.
- Error messages with multilingual support.
Support platform:
- JVM
- Android
Required dependencies
- kotlin 1.4
- kotlinx.serialization: min 1.0.0-RC
repositories {
jcenter()
}
dependencies {
implementation("com.github.marifeta:kvalidator:1.0.0")
}
val testJson = JsonObject(mapOf(
"name" to JsonPrimitive("John"),
"age" to JsonPrimitive(28),
"isHuman" to JsonPrimitive(true),
))
val rules = mapOf<String, List<Rule>>(
"name" to listOf(Alpha(), Required()),
"age" to listOf(IsInteger(), Between(25, 48)),
"isHuman" to listOf(IsBoolean()),
)
val validator = Validator(testJson, rules, /* lang = en */)
validator.validate() // true
// or
if(!validator.validate()) {
validator.errors.forEach {(attribute, messages)->
messages.forEach { msg ->
println("Error: $attribute has error by $msg")
}
}
}
val firstError = validator.errors.first() // firstError or null
val validator.errors // mutableListOf(reason) or null
val errorCount = validator.errors.count // errors.size (int)
parseRule("max:255"): Rule
parseRules("required|max:255"): List<Rule>
stringifyRules("required|email|max:15|min:8|size:8|boolean")
stringifyRule("max:15")
The field under validation must be entirely alphabetic characters.
The field under validation must be entirely alpha-numeric characters.
The field under validation may have alpha-numeric characters, as well as dashes and underscores.
The field under validation must have an integer value.
The field under validation must be a string.
The field under validation must be a boolean value of the form true, false (Not String).
The field under validation must be an array.
Validate that an attribute is numeric. The string representation of a number will pass.
The field under validation must be a valid date format which is acceptable by Java's LocalDate object in format "yyyy-mm-ddThh:mm:ss".
The field under validation must be after the given date.
Checks if the length of the String representation of the value is >
The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
The field under validation must be yes, on, 1 or true. This is useful for validating "Terms of Service" acceptance.
The field under validation must have a size between the given min and max. Strings, numerics, and files are evaluated in the same fashion as the size rule.
Validate that an attribute is at least a given size.
Validate that an attribute is no greater than a given size
The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value.
Validate that an attribute has a valid URL format
The field under validation must be formatted as an e-mail address.
MIT