🥄
One Library
  • 🤖onelib
  • 🏡Home
    • Overview
    • Setup Project
    • What's New
  • ☀️Fundamental
    • Basic Android
    • Data Flow
    • Dependency Injection
      • Hilt
      • Koin
  • 📿Layers
    • Data Layer
    • Domain Layer
    • Presentation Layer
  • 🪶Features
    • Views
      • Activity
      • Fragment
      • Bottom Sheet
      • Dialog
    • Adapters
      • Recycler Adapter
      • Filterable Adapter
      • Paging Adapter
      • Selectable Adapter
      • ViewPager Adapter
    • Firebase
      • One Firebase Auth
      • One Firebase Firestore
      • One Firebase Realtime
      • One Firebase Storage
    • Extension Functions
      • Any
      • Common
      • Context
      • Edittext
      • Image View
      • LiveData
      • OneMap
      • State Flow (Jetpack Compose)
      • View
    • Notification
    • One State View
    • Local Database
    • Permission
    • Validation
      • Passive Validator
      • Reactive Validator
      • Extension Validation
Powered by GitBook
On this page
  1. Features
  2. Validation

Extension Validation

Extension validation is an extension function to support validation feature in onelib, you can directly call extension function in Validation when you want to use PassiveValidation or ReactiveValidation. Here are the extension functions that you can use

fun alphaNumericPasswordRule(errorMessage: String): RegexRule =
    RegexRule("(?=.*[A-Za-z])(?=.*\\\\d)[A-Za-z\\\\d]{8,}\$", errorMessage)

fun alphabetOnlyRule(errorMessage: String): RegexRule =
    RegexRule("^[a-zA-Z]{0,}\$", errorMessage)

fun alphabetSpaceOnly(errorMessage: String): RegexRule =
    RegexRule("^[a-zA-Z]+[a-zA-Z ]*$", errorMessage)

fun customRule(errorMessage: String, rule: () -> Boolean): CustomRule =
    CustomRule(rule, errorMessage)

fun emailRule(errorMessage: String): RegexRule =
    RegexRule(
        "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$",
        errorMessage
    )

fun maxLengthRule(errorMessage: String, maxLength: Int): MinMaxLengthRule =
    minMaxLengthRule(errorMessage, null, maxLength)

fun minLengthRule(errorMessage: String, minLength: Int?): MinMaxLengthRule =
    minMaxLengthRule(errorMessage, minLength, null)

fun minMaxLengthRule(errorMessage: String, minLength: Int? = 0, maxLength: Int?): MinMaxLengthRule {
    var result = ""
    var min = ""
    var max = ""

    if (minLength != null) {
        val convertToString = minLength.toString()
        result = convertToString.ifBlank { "0" }
        min = result
    }

    if (maxLength != null) {
        val convertToString = maxLength.toString()
        result = convertToString.ifBlank { "0" }
        max = result
    }

    return MinMaxLengthRule(min, max, errorMessage)
}

fun notEmptyRule(errorMessage: String): NotEmptyRule =
    NotEmptyRule(errorMessage)

fun numberOnlyRule(errorMessage: String): RegexRule =
    RegexRule("^[0-9]{0,}$", errorMessage)

fun passwordRule(errorMessage: String): RegexRule =
    RegexRule("^[\\s\\d\\w!@#$%^&_*]{6,}$", errorMessage)
    
fun regexRule(errorMessage: String, rule: String): RegexRule =
    RegexRule(rule, errorMessage)

If you want to use your own custom regex, you can call the regexRule function and send your regex as a parameter. For example

val regex = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$".toRegex()
regexRule("message", regex)

Last updated 11 months ago

🪶