commonMain.io.github.lyxnx.util.validation.EmailValidator.kt Maven / Gradle / Ivy
package io.github.lyxnx.util.validation
import kotlin.jvm.JvmField
/**
* Used to validate an email input
*/
public open class EmailValidator : StringInputValidator {
public companion object {
@JvmField
public val EMAIL_PATTERN: Regex =
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}\\@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}(\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25})+".toRegex()
}
/**
* Represents the result of an email validation
*/
public enum class Result : ValidationResult {
/**
* The result of an input being [null or blank][String.isNullOrBlank]
*/
EMPTY,
/**
* The result of an input being correct but not a valid email
*/
INVALID,
/**
* The result of an input being a correctly formatted email
*/
VALID;
override val isValid: Boolean get() = this == VALID
}
override fun validate(input: String?): Result =
when {
input.isNullOrBlank() -> Result.EMPTY
!EMAIL_PATTERN.matches(input) -> Result.INVALID
else -> Result.VALID
}
}