Skip to content

Latest commit

 

History

History
167 lines (130 loc) · 6.49 KB

README.md

File metadata and controls

167 lines (130 loc) · 6.49 KB

KillerValidator

Hi Developers,

This library used for check any type of validations in Kotlin without write any extra code. Just simply create a data class and apply any annotation according to your requirements.

Hope this library will game changer in android development validations. Now no need to add extra code for validations only apply annotation's and check with the help of 2 lines of code.

Features Available

  1. Required validation (Check field is empty or not.)
  2. Email validation
  3. Length check validation (Minimum and Maximum Length)
  4. Link validation
  5. Password validation
  6. Match multiple fields validation
  7. Custom Regex check validation

Available Annotations

  1. @RequiredField

  2. Required annotation check if variable is empty or not. If value is null then it will also return error message.

  3. @EmailField

  4. EmailField annotation is used to check its valid email address or not.

  5. @CustomRegex("{ANY REGEX VALUE}")

  6. If you want to validate custom regex then this will possible with the help of CustomRegex annotation.

  7. @LengthField(minLength = 2, maxLength = 20)

  8. LengthField is used to check minimum and maximum length of any variable.

  9. @LinkField

  10. LinkField is used to check url or link is valid or not. Https, Http or ftp any type of link check with the help of this annotation.

  11. @PasswordField

  12. PasswordField is used to check its valid password or not. In this validation user need to add One Capital Character, One Small Character, One Special Character, One Numeric character and length must be greater than 7.

  13. @MatchField("uniqueType")

  14. MatchField is used to check values more than two fields. If you want to match variable values then you can pass same key on both variables. Also refer data class below attached example.

How to used validator classes in app.

How to integrate into your app?

  Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:
allprojects {
	  repositories {
	  	...
	  	maven { url 'https://jitpack.io' }
	  }
  }

Step 2. Add the dependency

dependencies {
        implementation 'com.github.rajputmukesh748:KillerValidator:1.3.0'
  }

Source Code

1. Initialized validator classes in application class.

  class ApplicationClass: Application() {
      override fun onCreate() {
        super.onCreate()
        KillerValidator.initialize(context = applicationContext)
     }
  }

2. Add application class in manifest file.

 <application
        android:name=".ApplicationClass"
        />

3. What is errorKey in annotations parameter.

It's used for provide custom error messages to user.

ErrorKey annotation is user in every annotation but its optional. If developer not provide this key then in-build error message will return. If developer provide "errorKey" is any annotation and provide string file key in that, then it will return a user error message.

1. Firstly declare error message in `string.xml` file
    <string name="not_valid_email_address">Please enter valid email address.</string>

2. Now provide string key into any errorKey in any annotation. Please refer below syntax.
        data class Login(
            @EmailField(errorKey = "not_valid_email_address")
            var email: String? = null
        )

3. `not_valid_email_address` is a key that declare in `string.xml` file.

4. How to apply validation's in data class.

    data class ValidatorData( 
        @RequiredField(errorKey = "empty_name")
        val name: String? = null,
    
        @RequiredField(errorKey = "empty_email")
        @EmailField(errorKey = "email_address")
        val emailAddress: String? = null,
    
        @RequiredField(errorKey = "custom_regex_valid")
        @CustomRegex(regex = "{ANY REGEX VALUE}", errorKey = "custom_regex_valid")
        val customRegex: String? = null,
    
        @RequiredField(errorKey = "not_empty")
        @LengthField(minLength = 2, maxLength = 20, errorKey = "length_error_message")
        val checkLength: String? = null,
    
        @RequiredField(errorKey = "empty_url")
        @LinkField(errorKey = "not_valid_url")
        val urlLink: String? = null,
    
        @RequiredField(errorKey = "empty_password")
        @PasswordField(errorKey = "not_valid_password")
        val password: String? = null,
    
        //Apply Same key and automatically match all same keys
        @RequiredField(errorKey = "empty_email")
        @MatchField(key = "email", errorKey = "not_valid_email")
        val reEnterEmail: String? = null,
    
        @RequiredField("empty_confirm_email")
        @MatchField(key = "email", errorKey = "not_valid_email")
        val confirmEmail: String? = null,
    
        //Apply Same key and automatically match all same keys
        @RequiredField(errorKey = "empty_value")
        @MatchField(key = "checkValues", errorKey = "value_not_match")
        val value1: String? = null,
    
        @RequiredField(errorKey = "empty_value")
        @MatchField(key = "checkValues", errorKey = "value_not_match")
        val value2: String? = null,
    
        @RequiredField(errorKey = "empty_value")
        @MatchField(key = "checkValues", errorKey = "value_not_match")
        val value3: String? = null
     )

5. How to check data class all validation are valid or not.

  KillerValidator.isValid(dataClass = ValidatorData()) {
        if (it.isNotEmpty()) {
            it.errorHandling()
        } else {
            //All validations are valid
        }
    }

6. Get error messages and also check error type.

 private fun ArrayList<ValidatorModel>.errorHandling() {
      println(map { it.errorMessages })

      //Check which type of error
      forEach {
          when (it.errorType) {
              ErrorTypes.REQUITED_ERROR -> {
                  "TODO WORK"
              }
              else -> ""
          }
      }
  }

Thank you for your support.