All Projects → anderscheow → Validator

anderscheow / Validator

Licence: mit
A tool to validate text inside TextInputLayout

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Validator

Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+506.84%)
Mutual labels:  validation, validator
Ismailfine
A simple (but correct) library for validating email addresses. Supports mail addresses as defined in rfc5322 as well as the new Internationalized Mail Address standards (rfc653x). Based on https://github.com/jstedfast/EmailValidation
Stars: ✭ 9 (-92.31%)
Mutual labels:  validation, validator
Tram Policy
Policy Object Pattern
Stars: ✭ 16 (-86.32%)
Mutual labels:  validation, validator
Nice Validator
Simple, smart and pleasant validation solution.
Stars: ✭ 587 (+401.71%)
Mutual labels:  validation, validator
Vee Validate
✅ Form Validation for Vue.js
Stars: ✭ 8,820 (+7438.46%)
Mutual labels:  validation, validator
Validation
The most awesome validation engine ever created for PHP
Stars: ✭ 5,484 (+4587.18%)
Mutual labels:  validation, validator
Grpc Dotnet Validator
Simple request message validator for grpc.aspnet
Stars: ✭ 25 (-78.63%)
Mutual labels:  validation, validator
Graphql Constraint Directive
Validate GraphQL fields
Stars: ✭ 401 (+242.74%)
Mutual labels:  validation, validator
Inspector
A tiny class validation library.
Stars: ✭ 64 (-45.3%)
Mutual labels:  validation, validator
Is
Micro check library in Golang.
Stars: ✭ 61 (-47.86%)
Mutual labels:  validation, validator
Express Validator
An express.js middleware for validator.js.
Stars: ✭ 5,236 (+4375.21%)
Mutual labels:  validation, validator
Unicorn
Unicorn - W3C's Unified Validator
Stars: ✭ 70 (-40.17%)
Mutual labels:  validation, validator
Validator.js
⁉️轻量级的 JavaScript 表单验证,字符串验证。没有依赖,支持 UMD ,~3kb。
Stars: ✭ 486 (+315.38%)
Mutual labels:  validation, validator
Class Validator
Decorator-based property validation for classes.
Stars: ✭ 6,941 (+5832.48%)
Mutual labels:  validation, validator
Indicative
Indicative is a simple yet powerful data validator for Node.js and browsers. It makes it so simple to write async validations on nested set of data.
Stars: ✭ 412 (+252.14%)
Mutual labels:  validation, validator
Cti Stix Validator
OASIS TC Open Repository: Validator for STIX 2.0 JSON normative requirements and best practices
Stars: ✭ 24 (-79.49%)
Mutual labels:  validation, validator
Approvejs
A simple JavaScript validation library that doesn't interfere
Stars: ✭ 336 (+187.18%)
Mutual labels:  validation, validator
Validate
⚔ Go package for data validation and filtering. support Map, Struct, Form data. Go通用的数据验证与过滤库,使用简单,内置大部分常用验证、过滤器,支持自定义验证器、自定义消息、字段翻译。
Stars: ✭ 378 (+223.08%)
Mutual labels:  validation, validator
Govalidator
Validate Golang request data with simple rules. Highly inspired by Laravel's request validation.
Stars: ✭ 969 (+728.21%)
Mutual labels:  validation, validator
Schemasafe
A reasonably safe JSON Schema validator with draft-04/06/07/2019-09 support.
Stars: ✭ 67 (-42.74%)
Mutual labels:  validation, validator

Android Arsenal Build Status codecov

Download

dependencies {
  implementation 'com.github.anderscheow:validator:2.2.1'
}

Usage

Available rules

  • LengthRule
  • MaxRule
  • MinRule
  • NotEmptyRule
  • NotNullRule
  • RegexRule
  • AlphabetRule
  • AlphanumericRule
  • DigitsRule
  • EmailRule
  • PasswordRule
  • FutureRule
  • PastRule
  • CreditCardRule
  • ContainRule
  • NotContainRule
  • EqualRule
  • NotEqualRule
  • NotBlankRule
  • AllUpperCaseRule
  • AllLowerCaseRule
  • EndsWithRule
  • StartsWithRule
  • SymbolRule

Additional predefined rules to use in Validation or Condition

  • contain
  • notContain
  • notEqualTo
  • withinRange
  • minimumLength
  • maximumLength
  • email
  • alphanumericOnly
  • alphabetOnly
  • digitsOnly
  • symbolsOnly
  • allUppercase
  • allLowercase
  • startsWith
  • endsWith
  • withCreditCard
  • withPassword
  • notNull
  • notEmpty
  • notBlank
  • regex
  • future
  • past
  • matchAtLeastOneRule (Only for Validation)
  • matchAllRules (Only for Validation)

You can create your own Predefined Rules

// For Validation
fun Validation.customPredefinedRule(keyword: String, ignoreCase: Boolean = false): Validation {
    baseRules.add(ContainRule(keyword, ignoreCase))
    return this
}
 
// For Condition
fun Condition.customPredefinedRule(keyword: String, ignoreCase: Boolean = false): Condition {
    baseRules.add(ContainRule(keyword, ignoreCase))
    return this
}

Beside from using the provided Rules, you can create your own Rule by extending BaseRule (Create as many as you want)

class CustomRule : BaseRule {
 
    override fun validate(value: String?): Boolean {
        if (value == null) {
            throw NullPointerException()
        }
        return value == "ABC"
    }
 
    // You can use this to return error message
    override fun getErrorMessage(): String {
        return "Value doesn't match 'ABC'"
    }
 
    // or use this to return error message as StringRes
    override fun getErrorRes(): Int {
        return R.string.error_not_match
    }
}

Add it to your Activity class

// Username
// Input: [email protected]
val usernameInput = findViewById(R.id.layout_username)
val usernameValidation = Validation(usernameInput)
                .addRule(
                    // You can also override the default error message
                    NotEmptyRule(R.string.error_not_empty)
                     
                    // Use either errorRes() or errorMessage()
                    // Note: errorRes() has higher priority
                    NotEmptyRule("Value is empty")
                )
                .addRule(CustomRule())
 
// Password
// Input: 12345abc
val passwordInput = findViewById(R.id.layout_password)
val passwordValidation = Validation(passwordInput)
                .addRule(NotEmptyRule())
                .withPassword(PasswordRegex.ALPHA_NUMERIC)
                .minimumLength(8)

Condition

// And Condition
val usernameWithConditionValidation = Validation(usernameInput)
                .add(And().add(EmailRule()))
 
// Or Condition
val usernameWithConditionValidation = Validation(usernameInput)
                .add(Or().add(MinRule(5)).add(MaxRule(10)))
 
// Both by using Predefined Rules
val usernameWithConditionValidation = new Validation(usernameInput)
                .matchAllRules(listOf(EmailRule()))
                .matchAtLeastOneRule(listOf(MinRule(5), MaxRule(10)))

Mode

Validator.with(applicationContext)
            /* Set your mode here, by default is CONTINUOUS */
            .setMode(Mode.CONTINUOUS));
Single Continuous

Validate the input field

// Order of the values on the success callback follow the sequence of your Validation object
Validator.with(applicationContext)
            .setMode(Mode.CONTINUOUS)
            .setListener(object : Validator.OnValidateListener {
                    override fun onValidateSuccess(values: List<String>) {
                        Log.d("MainActivity", values.toTypedArray().contentToString())
                        Toast.makeText(applicationContext, "Validate successfully", Toast.LENGTH_LONG).show()
                    }

                    override fun onValidateFailed(errors: List<String>) {
                        Log.e("MainActivity", errors.toTypedArray().contentToString())
                    }
                })
            .validate(usernameValidation, passwordValidation)

Changelog

2.2.1

  • Added Validation.notNull, Validation.notEmpty, Validation.notBlank, Validation.regex, Validation.past and Validation.future
  • Can use TextInputLayout.validate or TextInputLayout.add instead of Validation(TextInputLayout) in Kotlin

2.2.0

  • Added AllUpperCaseRule, AllLowerCaseRule, StartsWithRule, EndsWithRule and SymbolRule
  • Changed validate(Any?) to validate(String?)

2.1.2

  • Added NotEmptyRule
  • Added error messages into Validator.OnValidateListener.onValidateFailed()

2.1.0

  • Updated Gradle and Kotlin version
  • Changed Android Support artifacts to AndroidX
  • Removed some install dependencies from README

2.1.0

  • Updated Gradle and Kotlin version
  • Changed Android Support artifacts to AndroidX
  • Removed some install dependencies from README

2.0.1

  • Updated to support Android SDK 28
  • Converted Android Support to AndroidX

1.3.1

  • Addressed some algorithm issues
  • Added more test cases

1.3.0

  • Removed Validation.and() and Validation.or() by encouraging user to use Condition
  • Removed listener parameter from Validator.validate() and required to assigned manually using Validator.setListener()
  • Added some predefined rules in Validation and Condition to simplify procedure on adding rules

1.2.2

  • Added Dokka to show Kotlin sources

1.2.1

  • Removed generic type from BaseRule as there's a limitation

  • validate() only accept "Any?" as parameter

1.2.0

  • For version lower than 1.2.0, upgrading to latest version may broke your code. Use it at your risk

  • Updated to Kotlin

  • Validation does support for Object parameter instead of just TextInputLayout (Refer to example below)

  • Set TextInputLayout.setErrorEnabled(false) on every Validate method called

1.1.5

  • Removed unwanted log

1.1.4

  • Fixed an issue where validate will success if last validation pass the rules in Mode.CONTINUOUS

  • RegexRule now is open class rather than abstract class

1.1.3

  • Input can be any object, previously restrict to String (Along with proper validation with different object)

  • Added more test cases to validate input

1.1.2

  • Updated ALPHA_NUMERIC_SYMBOLS regex on PasswordRule

1.1.1

  • Fixed bug where overloading the constructor with errorMessage or errorRes does not override the default value

1.1.0

  • Added ability to add conditions (And or Or)

  • Added mode of validation (Single or Continuous)

  • Added ability to override errorRes or errorMessage in constructor without overriding the methods

1.0.4

  • Added more rules, please check at 'Available Rules'

1.0.3

  • Fixed LengthRule wrong validation on maxValue

1.0.2

  • Added success and failed callback instead of just success callback

  • Success callback return list of EditText values (Order by sequence of Validation object(s))

1.0.1

  • Added some common rules (LengthRule, MinRule, MaxRule, RegexRule etc.)

  • Able to use String as error message

1.0.0

  • Introduce Validator library

Testing

I have added unit testing for Rules and Conditions, soon will provide test code on Validation and Validator, please check it out under Test code

Contributions

Any contribution is more than welcome! You can contribute through pull requests and issues on GitHub.

License

Validator is released under the MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].