Skip to content

Instantly share code, notes, and snippets.

@addeeandra
Last active April 23, 2020 15:33
Show Gist options
  • Save addeeandra/1be1e39d28bbf9ecce00b9bab3abc6ad to your computer and use it in GitHub Desktop.
Save addeeandra/1be1e39d28bbf9ecce00b9bab3abc6ad to your computer and use it in GitHub Desktop.
Fields Validator (idea)
class Validator {
companion object {
val EMAIL_REGEX =
Regex("(?:[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")
val NUMBER_REGEX = Regex("^[0-9]+$")
}
enum class Field {
NONE, EMAIL, NUMBER, PHONE
}
var fieldName: String = "Field"
var required: Boolean = false
var type: Field = Field.NONE
var wordCount: Int = -1
var lengthMin: Int = -1
var lengthMax: Int = -1
var equalsTo: String? = null
var startsWith: String? = null
var endsWith: String? = null
var equalsToIgnore: Boolean = false
var startsWithIgnore: Boolean = false
var endsWithIgnore: Boolean = false
var equalsToName: String? = null
fun validate(text: String?): ValidatorResult {
val errors = mutableListOf<String>()
if (required && text.isNullOrBlank()) {
errors.add("$fieldName is required.")
}
if (equalsTo != null && equalsToName != null && !text.equals(equalsTo, equalsToIgnore)) {
errors.add("$fieldName is not equals to $equalsToName")
}
when (type) {
Field.EMAIL -> {
if (text?.matches(EMAIL_REGEX) != true) {
errors.add("$fieldName is not a valid email address.")
}
}
Field.NUMBER -> {
if (text?.matches(NUMBER_REGEX) != true) {
errors.add("$fieldName is not a valid number.")
}
}
Field.PHONE -> {
if (text?.matches(NUMBER_REGEX) != true) {
errors.add("$fieldName is not a valid phone.")
}
}
Field.NONE -> {
}
}
if (wordCount > 0 && (text?.split(" ")?.size ?: 0) < wordCount) {
errors.add("$fieldName at least need to be $wordCount words.")
}
if (lengthMin > 0 && (text?.length ?: 0) < lengthMin) {
errors.add("$fieldName can't be less than $lengthMin characters.")
}
if (lengthMax > 0 && (text?.length ?: 0) >= lengthMax) {
errors.add("$fieldName can't be more than $lengthMin characters.")
}
if (startsWith != null && text?.startsWith(startsWith!!, startsWithIgnore) != true) {
errors.add("$fieldName should be starts with $startsWith.")
}
if (endsWith != null && text?.endsWith(endsWith!!, endsWithIgnore) != true) {
errors.add("$fieldName should be starts with $endsWith.")
}
return ValidatorResult(errors.toTypedArray())
}
class Builder {
private val mValidator = Validator()
constructor(fieldName: String? = null) {
fieldName?.let { fieldName(it) }
}
fun fieldName(name: String): Builder {
mValidator.fieldName = name
return this
}
fun required(): Builder {
mValidator.required = true
return this
}
fun email(): Builder {
mValidator.type = Field.EMAIL
return this
}
fun phone(): Builder {
mValidator.type = Field.PHONE
return this
}
fun password(): Builder {
required()
min(8)
return this
}
fun number(): Builder {
mValidator.type = Field.NUMBER
return this
}
fun type(field: Field): Builder {
mValidator.type = field
return this
}
fun equalsTo(word: String, wordFieldName: String, ignoreCase: Boolean = true): Builder {
mValidator.equalsTo = word
mValidator.equalsToName = wordFieldName
mValidator.equalsToIgnore = ignoreCase
return this
}
fun wordCount(count: Int): Builder {
mValidator.wordCount = count
return this
}
fun min(count: Int): Builder {
check(count >= 0) { "Invalid value. Minimum length should be more than 0." }
mValidator.lengthMin = count
return this
}
fun max(count: Int): Builder {
check(count >= 0) { "Invalid value. Maximum length should be more than 0." }
mValidator.lengthMax = count
return this
}
fun length(min: Int, max: Int): Builder {
check(min >= 0) { "Invalid value. Minimum length should be more than 0." }
check(max >= 0) { "Invalid value. Maximum length should be more than 0." }
mValidator.lengthMin = min
mValidator.lengthMax = max
return this
}
fun startsWith(text: String, ignoreCase: Boolean = false): Builder {
mValidator.startsWith = text
mValidator.startsWithIgnore = ignoreCase
return this
}
fun startsWithIgnore(text: String): Builder {
return startsWith(text, true)
}
fun endsWith(text: String, ignoreCase: Boolean = false): Builder {
mValidator.endsWith = text
mValidator.endsWithIgnore = ignoreCase
return this
}
fun endsWithIgnore(text: String): Builder {
return endsWith(text, true)
}
fun build(): Validator {
return mValidator
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment