├── sample
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── values
│ │ │ ├── colors.xml
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── layout
│ │ │ └── activity_main.xml
│ │ ├── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ └── drawable
│ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── io
│ │ └── github
│ │ └── anderscheow
│ │ └── validator
│ │ └── sample
│ │ └── MainActivity.kt
├── proguard-rules.pro
└── build.gradle
├── jitpack.yml
├── library
├── .gitignore
├── src
│ ├── main
│ │ ├── AndroidManifest.xml
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ └── java
│ │ │ └── io
│ │ │ └── github
│ │ │ └── anderscheow
│ │ │ └── validator
│ │ │ ├── constant
│ │ │ └── Mode.java
│ │ │ ├── interfaces
│ │ │ ├── Validate.kt
│ │ │ └── ErrorImpl.kt
│ │ │ ├── rules
│ │ │ ├── Rule.kt
│ │ │ ├── common
│ │ │ │ ├── NotNullRule.kt
│ │ │ │ ├── NotBlankRule.kt
│ │ │ │ ├── NotEmptyRule.kt
│ │ │ │ ├── RegexRule.kt
│ │ │ │ ├── EqualRule.kt
│ │ │ │ ├── NotEqualRule.kt
│ │ │ │ ├── MinRule.kt
│ │ │ │ ├── MaxRule.kt
│ │ │ │ ├── AllLowerCaseRule.kt
│ │ │ │ ├── AllUpperCaseRule.kt
│ │ │ │ ├── PastRule.kt
│ │ │ │ ├── ContainRule.kt
│ │ │ │ ├── EndsWithRule.kt
│ │ │ │ ├── NotContainRule.kt
│ │ │ │ ├── StartsWithRule.kt
│ │ │ │ ├── FutureRule.kt
│ │ │ │ └── LengthRule.kt
│ │ │ └── regex
│ │ │ │ ├── DigitsRule.kt
│ │ │ │ ├── AlphabetRule.kt
│ │ │ │ ├── EmailRule.kt
│ │ │ │ ├── SymbolRule.kt
│ │ │ │ ├── AlphanumericRule.kt
│ │ │ │ ├── PasswordRule.kt
│ │ │ │ └── CreditCardRule.kt
│ │ │ ├── conditions
│ │ │ ├── common
│ │ │ │ ├── Or.kt
│ │ │ │ └── And.kt
│ │ │ └── Condition.kt
│ │ │ ├── Validation.kt
│ │ │ └── Validator.kt
│ └── test
│ │ └── java
│ │ └── io
│ │ └── github
│ │ └── anderscheow
│ │ └── validator
│ │ ├── rules
│ │ ├── common
│ │ │ ├── NotNullRuleTest.kt
│ │ │ ├── EqualRuleTest.kt
│ │ │ ├── RegexRuleTest.kt
│ │ │ ├── NotEqualRuleTest.kt
│ │ │ ├── NotContainRuleTest.kt
│ │ │ ├── NotBlankRuleTest.kt
│ │ │ ├── NotEmptyRuleTest.kt
│ │ │ ├── MaxRuleTest.kt
│ │ │ ├── MinRuleTest.kt
│ │ │ ├── AllLowerCaseRuleTest.kt
│ │ │ ├── AllUpperCaseRuleTest.kt
│ │ │ ├── EndsWithRuleTest.kt
│ │ │ ├── ContainRuleTest.kt
│ │ │ ├── StartsWithRuleTest.kt
│ │ │ ├── PastRuleTest.kt
│ │ │ ├── FutureRuleTest.kt
│ │ │ └── LengthRuleTest.kt
│ │ ├── RuleTest.kt
│ │ └── regex
│ │ │ ├── EmailRuleTest.kt
│ │ │ ├── DigitsRuleTest.kt
│ │ │ ├── SymbolRuleTest.kt
│ │ │ ├── AlphabetRuleTest.kt
│ │ │ ├── AlphanumericRuleTest.kt
│ │ │ ├── CreditCardRuleTest.kt
│ │ │ └── PasswordRuleTest.kt
│ │ └── conditions
│ │ ├── ConditionTest.kt
│ │ └── common
│ │ ├── OrTest.kt
│ │ └── AndTest.kt
├── proguard-rules.pro
├── gradle.properties
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── caches
│ └── build_file_checksums.ser
└── misc.xml
├── settings.gradle
├── gradle.properties
├── LICENSE
├── .gitignore
├── gradlew.bat
├── dependencies.gradle
├── CHANGELOG.md
├── README.md
└── gradlew
/sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/jitpack.yml:
--------------------------------------------------------------------------------
1 | jdk:
2 | - openjdk11
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Validator
3 |
4 |
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anderscheow/Validator/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/constant/Mode.java:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.constant;
2 |
3 | public enum Mode {
4 | SINGLE,
5 | CONTINUOUS
6 | }
7 |
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/interfaces/Validate.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.interfaces
2 |
3 | interface Validate {
4 |
5 | fun validate(value: String?): Boolean
6 | }
7 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Validator
3 | Filed is required
4 | Password is too short
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Jul 09 10:16:52 MYT 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
7 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':sample', ':library'
2 |
3 | dependencyResolutionManagement {
4 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
5 | repositories {
6 | google()
7 | mavenCentral()
8 | maven { url 'https://jitpack.io' }
9 | }
10 | }
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/Rule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.interfaces.ErrorImpl
5 | import io.github.anderscheow.validator.interfaces.Validate
6 |
7 | abstract class Rule : ErrorImpl, Validate {
8 |
9 | constructor(@StringRes errorRes: Int) : super(errorRes)
10 |
11 | constructor(errorString: String) : super(errorString)
12 | }
13 |
14 | class RulesBuilder {
15 | val ruleList = arrayListOf()
16 |
17 | operator fun Rule.unaryPlus() {
18 | ruleList.add(this)
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/NotNullRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class NotNullRule : Rule {
7 |
8 | constructor(@StringRes errorRes: Int) : super(errorRes)
9 |
10 | constructor(errorMessage: String) : super(errorMessage)
11 |
12 | override fun validate(value: String?): Boolean {
13 | return value != null
14 | }
15 | }
16 |
17 | fun notNull(@StringRes errorRes: Int): NotNullRule = NotNullRule(errorRes)
18 |
19 | fun notNull(errorMessage: String): NotNullRule = NotNullRule(errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/regex/DigitsRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.common.RegexRule
5 |
6 | class DigitsRule : RegexRule {
7 |
8 | constructor(@StringRes errorRes: Int) : super(DIGITS_REGEX, errorRes)
9 |
10 | constructor(errorMessage: String) : super(DIGITS_REGEX, errorMessage)
11 |
12 | companion object {
13 | private const val DIGITS_REGEX = "\\d+"
14 | }
15 | }
16 |
17 | fun digitsOnly(@StringRes errorRes: Int): DigitsRule = DigitsRule(errorRes)
18 |
19 | fun digitsOnly(errorMessage: String): DigitsRule = DigitsRule(errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/regex/AlphabetRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.common.RegexRule
5 |
6 | class AlphabetRule : RegexRule {
7 |
8 | constructor(@StringRes errorRes: Int) : super(ALPHABET_REGEX, errorRes)
9 |
10 | constructor(errorMessage: String) : super(ALPHABET_REGEX, errorMessage)
11 |
12 | companion object {
13 | private const val ALPHABET_REGEX = "^[a-zA-Z]*$"
14 | }
15 | }
16 |
17 | fun alphabetOnly(@StringRes errorRes: Int): AlphabetRule = AlphabetRule(errorRes)
18 |
19 | fun alphabetOnly(errorMessage: String): AlphabetRule = AlphabetRule(errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/regex/EmailRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.common.RegexRule
5 |
6 | class EmailRule : RegexRule {
7 |
8 | constructor(@StringRes errorRes: Int) : super(EMAIL_REGEX, errorRes)
9 |
10 | constructor(errorMessage: String) : super(EMAIL_REGEX, errorMessage)
11 |
12 | companion object {
13 | private const val EMAIL_REGEX = "\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\\b"
14 | }
15 | }
16 |
17 | fun email(@StringRes errorRes: Int): EmailRule = EmailRule(errorRes)
18 |
19 | fun email(errorMessage: String): EmailRule = EmailRule(errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/regex/SymbolRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.common.RegexRule
5 |
6 | class SymbolRule : RegexRule {
7 |
8 | constructor(@StringRes errorRes: Int) : super(ALPHABET_REGEX, errorRes)
9 |
10 | constructor(errorMessage: String) : super(ALPHABET_REGEX, errorMessage)
11 |
12 | companion object {
13 | private const val ALPHABET_REGEX = "^[-!@#\$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\/]*\$"
14 | }
15 | }
16 |
17 | fun symbolsOnly(@StringRes errorRes: Int): SymbolRule = SymbolRule(errorRes)
18 |
19 | fun symbolsOnly(errorMessage: String): SymbolRule = SymbolRule(errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/regex/AlphanumericRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.common.RegexRule
5 |
6 | class AlphanumericRule : RegexRule {
7 |
8 | constructor(@StringRes errorRes: Int) : super(ALPHANUMERIC_REGEX, errorRes)
9 |
10 | constructor(errorMessage: String) : super(ALPHANUMERIC_REGEX, errorMessage)
11 |
12 | companion object {
13 | private const val ALPHANUMERIC_REGEX = "^[a-zA-Z0-9]*$"
14 | }
15 | }
16 |
17 | fun alphanumericOnly(@StringRes errorRes: Int): AlphanumericRule = AlphanumericRule(errorRes)
18 |
19 | fun alphanumericOnly(errorMessage: String): AlphanumericRule = AlphanumericRule(errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/NotBlankRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class NotBlankRule : Rule {
7 |
8 | constructor(@StringRes errorRes: Int) : super(errorRes)
9 |
10 | constructor(errorMessage: String) : super(errorMessage)
11 |
12 | override fun validate(value: String?): Boolean {
13 | if (value == null) {
14 | throw NullPointerException()
15 | } else {
16 | return value.isNotBlank()
17 | }
18 | }
19 | }
20 |
21 | fun notBlank(@StringRes errorRes: Int): NotBlankRule = NotBlankRule(errorRes)
22 |
23 | fun notBlank(errorMessage: String): NotBlankRule = NotBlankRule(errorMessage)
24 |
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/NotEmptyRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class NotEmptyRule : Rule {
7 |
8 | constructor(@StringRes errorRes: Int) : super(errorRes)
9 |
10 | constructor(errorMessage: String) : super(errorMessage)
11 |
12 | override fun validate(value: String?): Boolean {
13 | if (value == null) {
14 | throw NullPointerException()
15 | } else {
16 | return value.isNotEmpty()
17 | }
18 | }
19 | }
20 |
21 | fun notEmpty(@StringRes errorRes: Int): NotEmptyRule = NotEmptyRule(errorRes)
22 |
23 | fun notEmpty(errorMessage: String): NotEmptyRule = NotEmptyRule(errorMessage)
24 |
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/sample/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | rootProject.sampleModulePlugins.each {
2 | apply plugin: it
3 | }
4 |
5 | android {
6 | def ext = rootProject.extensions.ext
7 | compileSdkVersion ext.android.compileSdkVersion
8 | defaultConfig {
9 | applicationId "io.github.anderscheow.validator"
10 | minSdkVersion ext.android.minSdkVersion
11 | targetSdkVersion ext.android.targetSdkVersion
12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | rootProject.sampleModuleDependencies.each {
24 | add(it.configuration, it.dependency, it.options)
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
19 | android.useAndroidX=true
20 | android.enableJetifier=true
--------------------------------------------------------------------------------
/library/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Settings specified in this file will override any Gradle settings
4 | # configured through the IDE.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
10 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
11 | # When configured, Gradle will run in incubating parallel mode.
12 | # This option should only be used with decoupled projects. More details, visit
13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
14 | # org.gradle.parallel=true
15 | POM_NAME=Validator
16 | POM_ARTIFACT_ID=validator
17 | POM_PACKAGING=aar
18 |
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/RegexRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | open class RegexRule : Rule {
7 |
8 | private var regex: String
9 |
10 | constructor(regex: String, @StringRes errorRes: Int) : super(errorRes) {
11 | this.regex = regex
12 | }
13 |
14 | constructor(regex: String, errorMessage: String) : super(errorMessage) {
15 | this.regex = regex
16 | }
17 |
18 | override fun validate(value: String?): Boolean {
19 | if (value == null) {
20 | throw NullPointerException()
21 | } else {
22 | return value.matches(regex.toRegex())
23 | }
24 | }
25 | }
26 |
27 | fun regex(regex: String, @StringRes errorRes: Int): RegexRule = RegexRule(regex, errorRes)
28 |
29 | fun regex(regex: String, errorMessage: String): RegexRule = RegexRule(regex, errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/EqualRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class EqualRule : Rule {
7 |
8 | private var keyword: Any
9 |
10 | constructor(keyword: Any, @StringRes errorRes: Int) :
11 | super(errorRes) {
12 | this.keyword = keyword
13 | }
14 |
15 | constructor(keyword: Any, errorMessage: String) :
16 | super(errorMessage) {
17 | this.keyword = keyword
18 | }
19 |
20 | override fun validate(value: String?): Boolean {
21 | if (value == null) {
22 | throw NullPointerException()
23 | } else {
24 | return value == keyword
25 | }
26 | }
27 | }
28 |
29 | fun equalTo(keyword: String, @StringRes errorRes: Int): EqualRule = EqualRule(keyword, errorRes)
30 |
31 | fun equalTo(keyword: String, errorMessage: String): EqualRule = EqualRule(keyword, errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/NotEqualRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class NotEqualRule : Rule {
7 |
8 | private var keyword: Any
9 |
10 | constructor(keyword: Any, @StringRes errorRes: Int) : super(errorRes) {
11 | this.keyword = keyword
12 | }
13 |
14 | constructor(keyword: Any, errorMessage: String) : super(errorMessage) {
15 | this.keyword = keyword
16 | }
17 |
18 | override fun validate(value: String?): Boolean {
19 | if (value == null) {
20 | throw NullPointerException()
21 | } else {
22 | return value != keyword
23 | }
24 | }
25 | }
26 |
27 | fun notEqualTo(keyword: String, @StringRes errorRes: Int): NotEqualRule =
28 | NotEqualRule(keyword, errorRes)
29 |
30 | fun notEqualTo(keyword: String, errorMessage: String): NotEqualRule =
31 | NotEqualRule(keyword, errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/MinRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class MinRule : Rule {
7 |
8 | private var minLength: Int
9 |
10 | constructor(minLength: Int, @StringRes errorRes: Int) :
11 | super(errorRes) {
12 | this.minLength = minLength
13 | }
14 |
15 | constructor(minLength: Int, errorMessage: String) :
16 | super(errorMessage) {
17 | this.minLength = minLength
18 | }
19 |
20 | override fun validate(value: String?): Boolean {
21 | if (value == null) {
22 | throw NullPointerException()
23 | } else {
24 | return value.length >= minLength
25 | }
26 | }
27 | }
28 |
29 | fun minimumLength(minLength: Int, @StringRes errorRes: Int): MinRule = MinRule(minLength, errorRes)
30 |
31 | fun minimumLength(minLength: Int, errorMessage: String): MinRule = MinRule(minLength, errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/MaxRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class MaxRule : Rule {
7 |
8 | private var maxLength: Int
9 |
10 | constructor(maxLength: Int, @StringRes errorRes: Int) :
11 | super(errorRes) {
12 | this.maxLength = maxLength
13 | }
14 |
15 | constructor(maxLength: Int, errorMessage: String) :
16 | super(errorMessage) {
17 | this.maxLength = maxLength
18 | }
19 |
20 | override fun validate(value: String?): Boolean {
21 | if (value == null) {
22 | throw NullPointerException()
23 | } else {
24 | return value.length <= maxLength
25 | }
26 | }
27 | }
28 |
29 | fun maximumLength(maxLength: Int, @StringRes errorRes: Int): MaxRule = MaxRule(maxLength, errorRes)
30 |
31 | fun maximumLength(maxLength: Int, errorMessage: String): MaxRule = MaxRule(maxLength, errorMessage)
32 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | rootProject.libraryModulePlugins.each {
2 | apply plugin: it
3 | }
4 |
5 | android {
6 | def ext = rootProject.extensions.ext
7 | compileSdkVersion ext.android.compileSdkVersion
8 | defaultConfig {
9 | minSdkVersion ext.android.minSdkVersion
10 | targetSdkVersion ext.android.targetSdkVersion
11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | rootProject.libraryModuleDependencies.each {
23 | add(it.configuration, it.dependency, it.options)
24 | }
25 | }
26 |
27 | afterEvaluate {
28 | publishing {
29 | publications {
30 | release(MavenPublication) {
31 | from components.release
32 | groupId = "io.github.anderscheow"
33 | artifactId = "validator"
34 | version = "3.0.3"
35 | }
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License
2 |
3 | Copyright (c) 2017-2018 Anders Cheow
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by https://www.gitignore.io/api/android,mac
2 |
3 | ### Android ###
4 | # Built application files
5 | *.apk
6 | *.ap_
7 |
8 | # Files for the ART/Dalvik VM
9 | *.dex
10 |
11 | # Java class files
12 | *.class
13 |
14 | # Generated files
15 | bin/
16 | gen/
17 | out/
18 |
19 | # Gradle files
20 | .gradle/
21 | build/
22 |
23 | # Local configuration file (sdk path, etc)
24 | local.properties
25 |
26 | # Proguard folder generated by Eclipse
27 | proguard/
28 |
29 | # Log Files
30 | *.log
31 |
32 | # Android Studio Navigation editor temp files
33 | .navigation/
34 |
35 | # Android Studio captures folder
36 | captures/
37 |
38 | # Intellij
39 | *.iml
40 | .idea/workspace.xml
41 | .idea/tasks.xml
42 | .idea/gradle.xml
43 | .idea/dictionaries
44 | .idea/libraries
45 |
46 | # External native build folder generated in Android Studio 2.2 and later
47 | .externalNativeBuild
48 |
49 | # Freeline
50 | freeline.py
51 | freeline/
52 | freeline_project_description.json
53 |
54 | ### Android Patch ###
55 | gen-external-apklibs
56 |
57 | #!! ERROR: mac is undefined. Use list command to see defined gitignore types !!#
58 |
59 | # End of https://www.gitignore.io/api/android,mac
60 | .idea
61 |
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/AllLowerCaseRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 | import java.util.*
6 |
7 | class AllLowerCaseRule : Rule {
8 |
9 | private var locale: Locale
10 |
11 | constructor(@StringRes errorRes: Int, locale: Locale = Locale.getDefault())
12 | : super(errorRes) {
13 | this.locale = locale
14 | }
15 |
16 | constructor(errorMessage: String, locale: Locale = Locale.getDefault())
17 | : super(errorMessage) {
18 | this.locale = locale
19 | }
20 |
21 | override fun validate(value: String?): Boolean {
22 | if (value == null) {
23 | throw NullPointerException()
24 | } else {
25 | return value.lowercase(locale) == value
26 | }
27 | }
28 | }
29 |
30 | fun allLowercase(
31 | @StringRes errorRes: Int,
32 | locale: Locale = Locale.getDefault()
33 | ): AllLowerCaseRule = AllLowerCaseRule(errorRes, locale)
34 |
35 | fun allLowercase(
36 | errorMessage: String,
37 | locale: Locale = Locale.getDefault()
38 | ): AllLowerCaseRule = AllLowerCaseRule(errorMessage, locale)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/AllUpperCaseRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 | import java.util.*
6 |
7 | class AllUpperCaseRule : Rule {
8 |
9 | private var locale: Locale
10 |
11 | constructor(@StringRes errorRes: Int, locale: Locale = Locale.getDefault())
12 | : super(errorRes) {
13 | this.locale = locale
14 | }
15 |
16 | constructor(errorMessage: String, locale: Locale = Locale.getDefault())
17 | : super(errorMessage) {
18 | this.locale = locale
19 | }
20 |
21 | override fun validate(value: String?): Boolean {
22 | if (value == null) {
23 | throw NullPointerException()
24 | } else {
25 | return value.uppercase(locale) == value
26 | }
27 | }
28 | }
29 |
30 | fun allUppercase(
31 | @StringRes errorRes: Int,
32 | locale: Locale = Locale.getDefault()
33 | ): AllUpperCaseRule = AllUpperCaseRule(errorRes, locale)
34 |
35 | fun allUppercase(
36 | errorMessage: String,
37 | locale: Locale = Locale.getDefault()
38 | ): AllUpperCaseRule = AllUpperCaseRule(errorMessage, locale)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/regex/PasswordRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.common.RegexRule
5 |
6 | class PasswordRule : RegexRule {
7 |
8 | enum class PasswordRegex constructor(private val value: String) {
9 | ANY(".+"),
10 | ALPHA("\\w+"),
11 | ALPHA_MIXED_CASE("(?=.*[a-z])(?=.*[A-Z]).+"),
12 | NUMERIC("\\d+"),
13 | ALPHA_NUMERIC("(?=.*[a-zA-Z])(?=.*[\\d]).+"),
14 | ALPHA_NUMERIC_SYMBOLS("(?=.*[a-zA-Z])(?=.*[\\d])(?=.*([^\\w])).+");
15 |
16 | override fun toString(): String {
17 | return value
18 | }
19 | }
20 |
21 | constructor(regex: PasswordRegex, @StringRes errorRes: Int) : super(regex.toString(), errorRes)
22 |
23 | constructor(regex: PasswordRegex, errorMessage: String) : super(regex.toString(), errorMessage)
24 | }
25 |
26 | fun withPassword(
27 | regex: PasswordRule.PasswordRegex,
28 | @StringRes errorRes: Int
29 | ): PasswordRule = PasswordRule(regex, errorRes)
30 |
31 | fun withPassword(regex: PasswordRule.PasswordRegex, errorMessage: String): PasswordRule =
32 | PasswordRule(regex, errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/interfaces/ErrorImpl.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.interfaces
2 |
3 | import android.content.Context
4 | import androidx.annotation.StringRes
5 |
6 | abstract class ErrorImpl {
7 |
8 | @StringRes
9 | var errorRes: Int = -1
10 | private set
11 |
12 | var errorString: String = "Invalid input"
13 | private set
14 |
15 | val isErrorAvailable: Boolean
16 | get() = isErrorResAvailable || isErrorMessageAvailable
17 |
18 | val isErrorResAvailable: Boolean
19 | get() = errorRes != -1
20 |
21 | val isErrorMessageAvailable: Boolean
22 | get() = errorString.isNotBlank()
23 |
24 | constructor(errorRes: Int) {
25 | this.errorRes = errorRes
26 | }
27 |
28 | constructor(errorString: String) {
29 | this.errorString = errorString
30 | }
31 |
32 | fun getErrorMessage(context: Context): String {
33 | return when {
34 | isErrorResAvailable -> {
35 | context.getString(errorRes)
36 | }
37 | isErrorMessageAvailable -> {
38 | errorString
39 | }
40 | else -> {
41 | throw IllegalStateException("Please either use errorRes or errorMessage as your error output")
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/PastRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 | import java.text.DateFormat
6 | import java.text.ParseException
7 | import java.util.*
8 |
9 | class PastRule : Rule {
10 |
11 | private var dateFormat: DateFormat
12 |
13 | constructor(dateFormat: DateFormat, @StringRes errorRes: Int) : super(errorRes) {
14 | this.dateFormat = dateFormat
15 | }
16 |
17 | constructor(dateFormat: DateFormat, errorMessage: String) : super(errorMessage) {
18 | this.dateFormat = dateFormat
19 | }
20 |
21 | override fun validate(value: String?): Boolean {
22 | if (value == null) {
23 | throw NullPointerException()
24 | } else {
25 | var parsedDate: Date? = null
26 |
27 | try {
28 | parsedDate = dateFormat.parse(value)
29 | } catch (ignored: ParseException) {
30 | }
31 |
32 | return parsedDate != null && parsedDate.before(Date())
33 | }
34 | }
35 | }
36 |
37 | fun past(dateFormat: DateFormat, @StringRes errorRes: Int): PastRule =
38 | PastRule(dateFormat, errorRes)
39 |
40 | fun past(dateFormat: DateFormat, errorMessage: String): PastRule =
41 | PastRule(dateFormat, errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/ContainRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class ContainRule : Rule {
7 |
8 | private var keyword: String
9 |
10 | private var ignoreCase = false
11 |
12 | constructor(keyword: String, @StringRes errorRes: Int, ignoreCase: Boolean = false) :
13 | super(errorRes) {
14 | this.keyword = keyword
15 | this.ignoreCase = ignoreCase
16 | }
17 |
18 | constructor(keyword: String, errorMessage: String, ignoreCase: Boolean = false) :
19 | super(errorMessage) {
20 | this.keyword = keyword
21 | this.ignoreCase = ignoreCase
22 | }
23 |
24 | override fun validate(value: String?): Boolean {
25 | if (value == null) {
26 | throw NullPointerException()
27 | } else {
28 | return value.contains(keyword, ignoreCase)
29 | }
30 | }
31 | }
32 |
33 | fun contain(
34 | keyword: String,
35 | @StringRes errorRes: Int,
36 | ignoreCase: Boolean = false
37 | ): ContainRule = ContainRule(keyword, errorRes, ignoreCase)
38 |
39 | fun contain(
40 | keyword: String,
41 | errorMessage: String,
42 | ignoreCase: Boolean = false
43 | ): ContainRule = ContainRule(keyword, errorMessage, ignoreCase)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/EndsWithRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class EndsWithRule : Rule {
7 |
8 | private var keyword: String
9 |
10 | private var ignoreCase = false
11 |
12 | constructor(keyword: String, @StringRes errorRes: Int, ignoreCase: Boolean = false) :
13 | super(errorRes) {
14 | this.keyword = keyword
15 | this.ignoreCase = ignoreCase
16 | }
17 |
18 | constructor(keyword: String, errorMessage: String, ignoreCase: Boolean = false) :
19 | super(errorMessage) {
20 | this.keyword = keyword
21 | this.ignoreCase = ignoreCase
22 | }
23 |
24 | override fun validate(value: String?): Boolean {
25 | if (value == null) {
26 | throw NullPointerException()
27 | } else {
28 | return value.endsWith(keyword, ignoreCase)
29 | }
30 | }
31 | }
32 |
33 | fun endsWith(
34 | keyword: String,
35 | @StringRes errorRes: Int,
36 | ignoreCase: Boolean = false
37 | ): EndsWithRule = EndsWithRule(keyword, errorRes, ignoreCase)
38 |
39 | fun endsWith(
40 | keyword: String,
41 | errorMessage: String,
42 | ignoreCase: Boolean = false
43 | ): EndsWithRule = EndsWithRule(keyword, errorMessage, ignoreCase)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/NotContainRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class NotContainRule : Rule {
7 |
8 | private var keyword: String
9 |
10 | private var ignoreCase = false
11 |
12 | constructor(keyword: String, @StringRes errorRes: Int, ignoreCase: Boolean = false) :
13 | super(errorRes) {
14 | this.keyword = keyword
15 | this.ignoreCase = ignoreCase
16 | }
17 |
18 | constructor(keyword: String, errorMessage: String, ignoreCase: Boolean = false) :
19 | super(errorMessage) {
20 | this.keyword = keyword
21 | this.ignoreCase = ignoreCase
22 | }
23 |
24 | override fun validate(value: String?): Boolean {
25 | if (value == null) {
26 | throw NullPointerException()
27 | } else {
28 | return !value.contains(keyword, ignoreCase)
29 | }
30 | }
31 | }
32 |
33 | fun notContain(
34 | keyword: String,
35 | @StringRes errorRes: Int,
36 | ignoreCase: Boolean = false
37 | ): NotContainRule = NotContainRule(keyword, errorRes, ignoreCase)
38 |
39 | fun notContain(
40 | keyword: String,
41 | errorMessage: String,
42 | ignoreCase: Boolean = false
43 | ): NotContainRule = NotContainRule(keyword, errorMessage, ignoreCase)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/StartsWithRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 |
6 | class StartsWithRule : Rule {
7 |
8 | private var keyword: String
9 |
10 | private var ignoreCase = false
11 |
12 | constructor(keyword: String, @StringRes errorRes: Int, ignoreCase: Boolean = false) :
13 | super(errorRes) {
14 | this.keyword = keyword
15 | this.ignoreCase = ignoreCase
16 | }
17 |
18 | constructor(keyword: String, errorMessage: String, ignoreCase: Boolean = false) :
19 | super(errorMessage) {
20 | this.keyword = keyword
21 | this.ignoreCase = ignoreCase
22 | }
23 |
24 | override fun validate(value: String?): Boolean {
25 | if (value == null) {
26 | throw NullPointerException()
27 | } else {
28 | return value.startsWith(keyword, ignoreCase)
29 | }
30 | }
31 | }
32 |
33 | fun startsWith(
34 | keyword: String,
35 | @StringRes errorRes: Int,
36 | ignoreCase: Boolean = false
37 | ): StartsWithRule = StartsWithRule(keyword, errorRes, ignoreCase)
38 |
39 | fun startsWith(
40 | keyword: String,
41 | errorMessage: String,
42 | ignoreCase: Boolean = false
43 | ): StartsWithRule = StartsWithRule(keyword, errorMessage, ignoreCase)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/regex/CreditCardRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.common.RegexRule
5 |
6 | class CreditCardRule : RegexRule {
7 |
8 | enum class CreditCardRegex(private val value: String) {
9 | VISA("^4[0-9]{12}(?:[0-9]{3})?$"),
10 | MASTERCARD("^5[1-5][0-9]{14}$"),
11 | AMERICAN_EXPRESS("^3[47][0-9]{13}$"),
12 | DINERS_CLUB("^3(?:0[0-5]|[68][0-9])[0-9]{11}$"),
13 | DISCOVER("^6(?:011|5[0-9]{2})[0-9]{12}$"),
14 | JCB("^(?:2131|1800|35\\d{3})\\d{11}$"),
15 | CHINA_UNION_PAY("^62[0-9]{14,17}$");
16 |
17 | override fun toString(): String {
18 | return value
19 | }
20 | }
21 |
22 | constructor(regex: CreditCardRegex, @StringRes errorRes: Int) : super(
23 | regex.toString(),
24 | errorRes
25 | )
26 |
27 | constructor(regex: CreditCardRegex, errorMessage: String) : super(
28 | regex.toString(),
29 | errorMessage
30 | )
31 | }
32 |
33 | fun withCreditCard(
34 | regex: CreditCardRule.CreditCardRegex,
35 | @StringRes errorRes: Int
36 | ): CreditCardRule = CreditCardRule(regex, errorRes)
37 |
38 | fun withCreditCard(regex: CreditCardRule.CreditCardRegex, errorMessage: String): CreditCardRule =
39 | CreditCardRule(regex, errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/FutureRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 | import java.text.DateFormat
6 | import java.text.ParseException
7 | import java.util.*
8 |
9 | class FutureRule : Rule {
10 |
11 | private var dateFormat: DateFormat
12 |
13 | constructor(dateFormat: DateFormat, @StringRes errorRes: Int) :
14 | super(errorRes) {
15 | this.dateFormat = dateFormat
16 | }
17 |
18 | constructor(dateFormat: DateFormat, errorMessage: String) :
19 | super(errorMessage) {
20 | this.dateFormat = dateFormat
21 | }
22 |
23 | override fun validate(value: String?): Boolean {
24 | if (value == null) {
25 | throw NullPointerException()
26 | } else {
27 | var parsedDate: Date? = null
28 |
29 | try {
30 | parsedDate = dateFormat.parse(value)
31 | } catch (ignored: ParseException) {
32 | }
33 |
34 | return parsedDate != null && parsedDate.after(Date())
35 | }
36 | }
37 | }
38 |
39 | fun future(dateFormat: DateFormat, @StringRes errorRes: Int): FutureRule =
40 | FutureRule(dateFormat, errorRes)
41 |
42 | fun future(dateFormat: DateFormat, errorMessage: String): FutureRule =
43 | FutureRule(dateFormat, errorMessage)
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/conditions/common/Or.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.conditions.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.conditions.Condition
5 | import io.github.anderscheow.validator.conditions.ConditionBuilder
6 | import io.github.anderscheow.validator.conditions.ConditionsBuilder
7 | import io.github.anderscheow.validator.rules.Rule
8 |
9 | class Or : Condition {
10 |
11 | constructor(rules: List, @StringRes errorRes: Int) : super(rules, errorRes)
12 |
13 | constructor(rules: List, errorMessage: String) : super(rules, errorMessage)
14 |
15 | constructor(rule: Rule, @StringRes errorRes: Int) : super(listOf(rule), errorRes)
16 |
17 | constructor(rule: Rule, errorMessage: String) : super(listOf(rule), errorMessage)
18 |
19 | override fun validate(value: String?): Boolean {
20 | for (baseRule in rules) {
21 | if (baseRule.validate(value)) {
22 | return true
23 | }
24 | }
25 | return false
26 | }
27 | }
28 |
29 | class OrBuilder : ConditionBuilder() {
30 | }
31 |
32 | fun ConditionsBuilder.or(
33 | @StringRes errorRes: Int,
34 | init: OrBuilder.() -> Unit
35 | ): Or {
36 | val or = OrBuilder()
37 | or.init()
38 | return Or(or.ruleList, errorRes)
39 | }
40 |
41 | fun ConditionsBuilder.or(
42 | errorMessage: String,
43 | init: OrBuilder.() -> Unit
44 | ): Or {
45 | val or = OrBuilder()
46 | or.init()
47 | return Or(or.ruleList, errorMessage)
48 | }
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/conditions/common/And.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.conditions.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.conditions.Condition
5 | import io.github.anderscheow.validator.conditions.ConditionBuilder
6 | import io.github.anderscheow.validator.conditions.ConditionsBuilder
7 | import io.github.anderscheow.validator.rules.Rule
8 |
9 | class And : Condition {
10 |
11 | constructor(rules: List, @StringRes errorRes: Int) : super(rules, errorRes)
12 |
13 | constructor(rules: List, errorMessage: String) : super(rules, errorMessage)
14 |
15 | constructor(rule: Rule, @StringRes errorRes: Int) : super(listOf(rule), errorRes)
16 |
17 | constructor(rule: Rule, errorMessage: String) : super(listOf(rule), errorMessage)
18 |
19 | override fun validate(value: String?): Boolean {
20 | for (baseRule in rules) {
21 | if (baseRule.validate(value).not()) {
22 | return false
23 | }
24 | }
25 | return true
26 | }
27 | }
28 |
29 | class AndBuilder : ConditionBuilder() {
30 | }
31 |
32 | fun ConditionsBuilder.and(
33 | @StringRes errorRes: Int,
34 | init: AndBuilder.() -> Unit
35 | ): And {
36 | val and = AndBuilder()
37 | and.init()
38 | return And(and.ruleList, errorRes)
39 | }
40 |
41 | fun ConditionsBuilder.and(
42 | errorMessage: String,
43 | init: AndBuilder.() -> Unit
44 | ): And {
45 | val and = AndBuilder()
46 | and.init()
47 | return And(and.ruleList, errorMessage)
48 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/NotNullRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class NotNullRuleTest {
10 |
11 | private lateinit var notNullRule: NotNullRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test
24 | @Throws(Exception::class)
25 | fun validate_ValidSample_ReturnTrue() {
26 | notNullRule = NotNullRule("")
27 |
28 | val sample = "test"
29 |
30 | assertTrue(notNullRule.validate(sample))
31 | }
32 |
33 | @Test
34 | @Throws(Exception::class)
35 | fun validate_InvalidSample_ReturnFalse() {
36 | notNullRule = NotNullRule("")
37 |
38 | assertFalse(notNullRule.validate(null))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun errorMessage_CustomErrorMessage() {
44 | val errorMessage = "This is a custom error message"
45 |
46 | notNullRule = NotNullRule(errorMessage)
47 |
48 | assertEquals(errorMessage, notNullRule.errorString)
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun errorMessage_CustomErrorRes() {
54 | @StringRes val errorRes = 0
55 |
56 | notNullRule = NotNullRule(errorRes)
57 |
58 | assertEquals(errorRes, notNullRule.errorRes)
59 | }
60 | }
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/conditions/Condition.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.conditions
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.interfaces.ErrorImpl
5 | import io.github.anderscheow.validator.interfaces.Validate
6 | import io.github.anderscheow.validator.rules.Rule
7 | import io.github.anderscheow.validator.rules.RulesBuilder
8 |
9 | abstract class Condition : ErrorImpl, Validate {
10 |
11 | val rules: List
12 |
13 | constructor(rules: List, @StringRes errorRes: Int) : super(errorRes) {
14 | this.rules = rules
15 | }
16 |
17 | constructor(rules: List, errorString: String) : super(errorString) {
18 | this.rules = rules
19 | }
20 |
21 | constructor(rule: Rule, @StringRes errorRes: Int) : super(errorRes) {
22 | this.rules = listOf(rule)
23 | }
24 |
25 | constructor(rule: Rule, errorString: String) : super(errorString) {
26 | this.rules = listOf(rule)
27 | }
28 | }
29 |
30 | class ConditionsBuilder {
31 | val conditionList = arrayListOf()
32 |
33 | operator fun Condition.unaryPlus() {
34 | conditionList.add(this)
35 | }
36 | }
37 |
38 | open class ConditionBuilder {
39 | val ruleList = arrayListOf()
40 |
41 | fun rules(init: RulesBuilder.() -> Unit) {
42 | ruleList.addAll(RulesBuilder().apply(init).ruleList)
43 | }
44 |
45 | operator fun Rule.unaryPlus() {
46 | ruleList.add(this)
47 | }
48 |
49 | operator fun List.unaryPlus() {
50 | ruleList.addAll(this)
51 | }
52 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/RuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class RuleTest {
10 |
11 | private lateinit var rule: Rule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test
24 | @Throws(Exception::class)
25 | fun errorMessage_CustomErrorMessage() {
26 | val errorMessage = "This is a custom error message"
27 |
28 | rule = object : Rule(errorMessage) {
29 | override fun validate(value: String?): Boolean {
30 | return false
31 | }
32 | }
33 |
34 | assertEquals(errorMessage, rule.errorString)
35 | assertTrue(rule.isErrorAvailable)
36 | assertTrue(rule.isErrorMessageAvailable)
37 | assertFalse(rule.isErrorResAvailable)
38 | }
39 |
40 | @Test
41 | @Throws(Exception::class)
42 | fun errorMessage_CustomErrorRes() {
43 | @StringRes val errorRes = 0
44 |
45 | rule = object : Rule(errorRes) {
46 | override fun validate(value: String?): Boolean {
47 | return false
48 | }
49 | }
50 |
51 | assertEquals(errorRes, rule.errorRes)
52 | assertTrue(rule.isErrorAvailable)
53 | assertTrue(rule.isErrorMessageAvailable)
54 | assertTrue(rule.isErrorResAvailable)
55 | }
56 | }
--------------------------------------------------------------------------------
/sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
19 |
20 |
21 |
25 |
26 |
30 |
31 |
32 |
37 |
38 |
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/EqualRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class EqualRuleTest {
10 |
11 | private lateinit var equalRule: EqualRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | equalRule = EqualRule(STRING_KEYWORD, "")
27 |
28 | equalRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_ReturnTrue() {
34 | equalRule = EqualRule(STRING_KEYWORD, "")
35 |
36 | val sample = "test"
37 |
38 | assertTrue(equalRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_ReturnFalse() {
44 | equalRule = EqualRule(STRING_KEYWORD, "")
45 |
46 | val sample = "TEST"
47 |
48 | assertFalse(equalRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun errorMessage_CustomErrorMessage() {
54 | val errorMessage = "This is a custom error message"
55 |
56 | equalRule = EqualRule(STRING_KEYWORD, errorMessage)
57 |
58 | assertEquals(errorMessage, equalRule.errorString)
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorRes() {
64 | @StringRes val errorRes = 0
65 |
66 | equalRule = EqualRule(STRING_KEYWORD, errorRes)
67 |
68 | assertEquals(errorRes, equalRule.errorRes)
69 | }
70 |
71 | companion object {
72 | private const val STRING_KEYWORD = "test"
73 | }
74 | }
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/Validation.kt:
--------------------------------------------------------------------------------
1 | @file:Suppress("UNUSED")
2 |
3 | package io.github.anderscheow.validator
4 |
5 | import android.content.Context
6 | import com.google.android.material.textfield.TextInputLayout
7 | import io.github.anderscheow.validator.conditions.Condition
8 | import io.github.anderscheow.validator.conditions.ConditionsBuilder
9 | import io.github.anderscheow.validator.interfaces.ErrorImpl
10 | import io.github.anderscheow.validator.rules.Rule
11 | import io.github.anderscheow.validator.rules.RulesBuilder
12 | import java.util.*
13 |
14 | class Validation(val textInputLayout: TextInputLayout) {
15 |
16 | val rules = arrayListOf()
17 | val conditions = arrayListOf()
18 |
19 | fun add(rule: Rule): Validation {
20 | rules.add(rule)
21 | return this
22 | }
23 |
24 | fun add(condition: Condition): Validation {
25 | conditions.add(condition)
26 | return this
27 | }
28 |
29 | fun setError(context: Context, errorImpl: ErrorImpl, errors: ArrayList) {
30 | val error = errorImpl.getErrorMessage(context)
31 | textInputLayout.isErrorEnabled = true
32 | errors.add(error)
33 | textInputLayout.error = error
34 | }
35 | }
36 |
37 | class ValidationBuilder {
38 | val ruleList = arrayListOf()
39 | val conditionList = arrayListOf()
40 |
41 | fun rules(init: RulesBuilder.() -> Unit) {
42 | ruleList.addAll(RulesBuilder().apply(init).ruleList)
43 | }
44 |
45 | fun conditions(init: ConditionsBuilder.() -> Unit) {
46 | conditionList.addAll(ConditionsBuilder().apply(init).conditionList)
47 | }
48 | }
49 |
50 | fun validation(textInputLayout: TextInputLayout, init: ValidationBuilder.() -> Unit): Validation {
51 | val validation = ValidationBuilder()
52 | validation.init()
53 | return Validation(textInputLayout).apply {
54 | this.rules.addAll(validation.ruleList)
55 | this.conditions.addAll(validation.conditionList)
56 | }
57 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/RegexRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class RegexRuleTest {
10 |
11 | private lateinit var regexRule: RegexRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | regexRule = object : RegexRule(REGEX, "") {
27 | }
28 |
29 | regexRule.validate(null)
30 | }
31 |
32 | @Test
33 | @Throws(Exception::class)
34 | fun validate_ValidSample_ReturnTrue() {
35 | regexRule = object : RegexRule(REGEX, "") {
36 | }
37 |
38 | val sample = "123"
39 |
40 | assertTrue(regexRule.validate(sample))
41 | }
42 |
43 | @Test
44 | @Throws(Exception::class)
45 | fun validate_InvalidSample_ReturnFalse() {
46 | regexRule = object : RegexRule(REGEX, "") {
47 | }
48 |
49 | val sample = "TEST"
50 |
51 | assertFalse(regexRule.validate(sample))
52 | }
53 |
54 | @Test
55 | @Throws(Exception::class)
56 | fun errorMessage_CustomErrorMessage() {
57 | val errorMessage = "This is a custom error message"
58 |
59 | regexRule = object : RegexRule(REGEX, errorMessage) {
60 | }
61 |
62 | assertEquals(errorMessage, regexRule.errorString)
63 | }
64 |
65 | @Test
66 | @Throws(Exception::class)
67 | fun errorMessage_CustomErrorRes() {
68 | @StringRes val errorRes = 0
69 |
70 | regexRule = object : RegexRule(REGEX, errorRes) {
71 | }
72 |
73 | assertEquals(errorRes, regexRule.errorRes)
74 | }
75 |
76 | companion object {
77 | private const val REGEX = "\\d+"
78 | }
79 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/NotEqualRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class NotEqualRuleTest {
10 |
11 | private lateinit var notEqualRule: NotEqualRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | notEqualRule = NotEqualRule(INT_KEYWORD, "")
27 |
28 | notEqualRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_ReturnTrue() {
34 | notEqualRule = NotEqualRule(STRING_KEYWORD, "")
35 |
36 | val sample = "TEST"
37 |
38 | assertTrue(notEqualRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_ReturnFalse() {
44 | notEqualRule = NotEqualRule(STRING_KEYWORD, "")
45 |
46 | val sample = "test"
47 |
48 | assertFalse(notEqualRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun errorMessage_CustomErrorMessage() {
54 | val errorMessage = "This is a custom error message"
55 |
56 | notEqualRule = NotEqualRule(STRING_KEYWORD, errorMessage)
57 |
58 | assertEquals(errorMessage, notEqualRule.errorString)
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorRes() {
64 | @StringRes val errorRes = 0
65 |
66 | notEqualRule = NotEqualRule(STRING_KEYWORD, errorRes)
67 |
68 | assertEquals(errorRes, notEqualRule.errorRes)
69 | }
70 |
71 | companion object {
72 | private const val STRING_KEYWORD = "test"
73 | private const val INT_KEYWORD = 456
74 | }
75 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/regex/EmailRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class EmailRuleTest {
10 |
11 | private lateinit var emailRule: EmailRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | emailRule = EmailRule("")
27 |
28 | emailRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_ValidSamples_ReturnTrue() {
34 | emailRule = EmailRule("")
35 |
36 | val samples = arrayOf("test@hotmail.com", "test@hotmail.co.uk", "test@hotmail.my")
37 |
38 | for (sample in samples) {
39 | assertTrue("This email failed: $sample", emailRule.validate(sample))
40 | }
41 | }
42 |
43 | @Test
44 | @Throws(Exception::class)
45 | fun validate_InvalidSamples_ReturnFalse() {
46 | emailRule = EmailRule("")
47 |
48 | val samples = arrayOf("test@hotmail.c", "test@hotmail.co.")
49 |
50 | for (sample in samples) {
51 | assertFalse("This email failed: $sample", emailRule.validate(sample))
52 | }
53 | }
54 |
55 | @Test
56 | @Throws(Exception::class)
57 | fun errorMessage_CustomErrorMessage() {
58 | val errorMessage = "This is a custom error message"
59 |
60 | emailRule = EmailRule(errorMessage)
61 |
62 | assertEquals(errorMessage, emailRule.errorString)
63 | }
64 |
65 | @Test
66 | @Throws(Exception::class)
67 | fun errorMessage_CustomErrorRes() {
68 | @StringRes val errorRes = 0
69 |
70 | emailRule = EmailRule(errorRes)
71 |
72 | assertEquals(errorRes, emailRule.errorRes)
73 | }
74 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/NotContainRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class NotContainRuleTest {
10 |
11 | private lateinit var notContainRule: NotContainRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | notContainRule = NotContainRule(STRING_KEYWORD, "")
27 |
28 | notContainRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_ReturnTrue() {
34 | notContainRule = NotContainRule(STRING_KEYWORD, "")
35 |
36 | val sample = "contain_TEST"
37 |
38 | assertTrue(notContainRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_ReturnFalse() {
44 | notContainRule = NotContainRule(STRING_KEYWORD, "")
45 |
46 | val sample = "contain_test"
47 |
48 | assertFalse(notContainRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun errorMessage_CustomErrorMessage() {
54 | val errorMessage = "This is a custom error message"
55 |
56 | notContainRule = NotContainRule(STRING_KEYWORD, errorMessage)
57 |
58 | assertEquals(errorMessage, notContainRule.errorString)
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorRes() {
64 | @StringRes val errorRes = 0
65 |
66 | notContainRule = NotContainRule(STRING_KEYWORD, errorRes)
67 |
68 | assertEquals(errorRes, notContainRule.errorRes)
69 | }
70 |
71 | companion object {
72 | private const val STRING_KEYWORD = "test"
73 | }
74 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/NotBlankRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class NotBlankRuleTest {
10 |
11 | private lateinit var notBlankRule: NotBlankRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | notBlankRule = NotBlankRule("")
27 |
28 | notBlankRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_ReturnTrue() {
34 | notBlankRule = NotBlankRule("")
35 |
36 | val sample = "not blank"
37 |
38 | assertTrue(notBlankRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_ReturnFalse() {
44 | notBlankRule = NotBlankRule("")
45 |
46 | val sample = ""
47 |
48 | assertFalse(notBlankRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_SpaceSample_ReturnFalse() {
54 | notBlankRule = NotBlankRule("")
55 |
56 | val sample = " "
57 |
58 | assertFalse(notBlankRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | notBlankRule = NotBlankRule(errorMessage)
67 |
68 | assertEquals(errorMessage, notBlankRule.errorString)
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorRes() {
74 | @StringRes val errorRes = 0
75 |
76 | notBlankRule = NotBlankRule(errorRes)
77 |
78 | assertEquals(errorRes, notBlankRule.errorRes)
79 | }
80 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/NotEmptyRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class NotEmptyRuleTest {
10 |
11 | private lateinit var notEmptyRule: NotEmptyRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | notEmptyRule = NotEmptyRule("")
27 |
28 | notEmptyRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_ReturnTrue() {
34 | notEmptyRule = NotEmptyRule("")
35 |
36 | val sample = "not empty"
37 |
38 | assertTrue(notEmptyRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_ReturnFalse() {
44 | notEmptyRule = NotEmptyRule("")
45 |
46 | val sample = ""
47 |
48 | assertFalse(notEmptyRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_SpaceSample_ReturnTrue() {
54 | notEmptyRule = NotEmptyRule("")
55 |
56 | val sample = " "
57 |
58 | assertTrue(notEmptyRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | notEmptyRule = NotEmptyRule(errorMessage)
67 |
68 | assertEquals(errorMessage, notEmptyRule.errorString)
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorRes() {
74 | @StringRes val errorRes = 0
75 |
76 | notEmptyRule = NotEmptyRule(errorRes)
77 |
78 | assertEquals(errorRes, notEmptyRule.errorRes)
79 | }
80 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/MaxRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class MaxRuleTest {
10 |
11 | private lateinit var maxRule: MaxRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | maxRule = MaxRule(MAX_LENGTH, "")
27 |
28 | maxRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSampleEqualMax_ReturnTrue() {
34 | maxRule = MaxRule(MAX_LENGTH, "")
35 |
36 | val sample = "test"
37 |
38 | assertTrue(maxRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSampleLessThanMax_ReturnTrue() {
44 | maxRule = MaxRule(MAX_LENGTH, "")
45 |
46 | val sample = "tes"
47 |
48 | assertTrue(maxRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_SampleMoreThanMax_ReturnTrue() {
54 | maxRule = MaxRule(MAX_LENGTH, "")
55 |
56 | val sample = "testing"
57 |
58 | assertFalse(maxRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | maxRule = MaxRule(MAX_LENGTH, errorMessage)
67 |
68 | assertEquals(errorMessage, maxRule.errorString)
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorRes() {
74 | @StringRes val errorRes = 0
75 |
76 | maxRule = MaxRule(MAX_LENGTH, errorRes)
77 |
78 | assertEquals(errorRes, maxRule.errorRes)
79 | }
80 |
81 | companion object {
82 | private const val MAX_LENGTH = 4
83 | }
84 | }
--------------------------------------------------------------------------------
/library/src/main/java/io/github/anderscheow/validator/rules/common/LengthRule.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.Rule
5 | import java.util.*
6 |
7 | class LengthRule : Rule {
8 |
9 | private var minLength: Int
10 | private var maxLength: Int
11 |
12 | constructor(minLength: Int, maxLength: Int, @StringRes errorRes: Int) :
13 | super(errorRes) {
14 | this.minLength = minLength
15 | this.maxLength = maxLength
16 | }
17 |
18 | constructor(minLength: Int, maxLength: Int, errorMessage: String) :
19 | super(errorMessage) {
20 | this.minLength = minLength
21 | this.maxLength = maxLength
22 | }
23 |
24 | override fun validate(value: String?): Boolean {
25 | if (value == null) {
26 | throw NullPointerException()
27 | } else {
28 | value.let {
29 | assertMinMax(minLength, maxLength)
30 |
31 | val length = it.length
32 |
33 | var isMinValid = true
34 | if (minLength != Integer.MIN_VALUE) {
35 | isMinValid = length >= minLength
36 | }
37 |
38 | var isMaxValid = true
39 | if (maxLength != Integer.MAX_VALUE) {
40 | isMaxValid = length <= maxLength
41 | }
42 |
43 | return isMinValid && isMaxValid
44 | }
45 | }
46 | }
47 |
48 | private fun assertMinMax(min: Int, max: Int) {
49 | if (min > max) {
50 | val message = String.format(
51 | Locale.getDefault(),
52 | "'minLength' (%d) " + "should be smaller than 'maxLength' (%d)",
53 | min,
54 | max
55 | )
56 | throw IllegalStateException(message)
57 | }
58 | }
59 | }
60 |
61 | fun withinRange(minLength: Int, maxLength: Int, @StringRes errorRes: Int): LengthRule =
62 | LengthRule(minLength, maxLength, errorRes)
63 |
64 | fun withinRange(minLength: Int, maxLength: Int, errorMessage: String): LengthRule =
65 | LengthRule(minLength, maxLength, errorMessage)
66 |
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/MinRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class MinRuleTest {
10 |
11 | private lateinit var minRule: MinRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | minRule = MinRule(MIN_LENGTH, "")
27 |
28 | minRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSampleEqualMin_ReturnTrue() {
34 | minRule = MinRule(MIN_LENGTH, "")
35 |
36 | val sample = "tests"
37 |
38 | assertTrue(minRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSampleMoreThanMin_ReturnTrue() {
44 | minRule = MinRule(MIN_LENGTH, "")
45 |
46 | val sample = "testing"
47 |
48 | assertTrue(minRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_StringSampleLessThanMin_ReturnTrue() {
54 | minRule = MinRule(MIN_LENGTH, "")
55 |
56 | val sample = "test"
57 |
58 | assertFalse(minRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | minRule = MinRule(MIN_LENGTH, errorMessage)
67 |
68 | assertEquals(errorMessage, minRule.errorString)
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorRes() {
74 | @StringRes val errorRes = 0
75 |
76 | minRule = MinRule(MIN_LENGTH, errorRes)
77 |
78 | assertEquals(errorRes, minRule.errorRes)
79 | }
80 |
81 | companion object {
82 | private const val MIN_LENGTH = 5
83 | }
84 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/AllLowerCaseRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class AllLowerCaseRuleTest {
10 |
11 | private lateinit var allLowerCaseRule: AllLowerCaseRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | allLowerCaseRule = AllLowerCaseRule("")
27 |
28 | allLowerCaseRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_ReturnTrue() {
34 | allLowerCaseRule = AllLowerCaseRule("")
35 |
36 | val sample = "lowercase"
37 |
38 | assertTrue(allLowerCaseRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_ReturnFalse() {
44 | allLowerCaseRule = AllLowerCaseRule("")
45 |
46 | val sample = "UPPERCASE"
47 |
48 | assertFalse(allLowerCaseRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_MixStringSample_ReturnFalse() {
54 | allLowerCaseRule = AllLowerCaseRule("")
55 |
56 | val sample = "lowerUpper"
57 |
58 | assertFalse(allLowerCaseRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | allLowerCaseRule = AllLowerCaseRule(errorMessage)
67 |
68 | assertEquals(errorMessage, allLowerCaseRule.errorString)
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorRes() {
74 | @StringRes val errorRes = 0
75 |
76 | allLowerCaseRule = AllLowerCaseRule(errorRes)
77 |
78 | assertEquals(errorRes, allLowerCaseRule.errorRes)
79 | }
80 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/AllUpperCaseRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class AllUpperCaseRuleTest {
10 |
11 | private lateinit var allUpperCaseRule: AllUpperCaseRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | allUpperCaseRule = AllUpperCaseRule("")
27 |
28 | allUpperCaseRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_ReturnTrue() {
34 | allUpperCaseRule = AllUpperCaseRule("")
35 |
36 | val sample = "UPPERCASE"
37 |
38 | assertTrue(allUpperCaseRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_ReturnFalse() {
44 | allUpperCaseRule = AllUpperCaseRule("")
45 |
46 | val sample = "lowercase"
47 |
48 | assertFalse(allUpperCaseRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_MixStringSample_ReturnFalse() {
54 | allUpperCaseRule = AllUpperCaseRule("")
55 |
56 | val sample = "lowerUpper"
57 |
58 | assertFalse(allUpperCaseRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | allUpperCaseRule = AllUpperCaseRule(errorMessage)
67 |
68 | assertEquals(errorMessage, allUpperCaseRule.errorString)
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorRes() {
74 | @StringRes val errorRes = 0
75 |
76 | allUpperCaseRule = AllUpperCaseRule(errorRes)
77 |
78 | assertEquals(errorRes, allUpperCaseRule.errorRes)
79 | }
80 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/EndsWithRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class EndsWithRuleTest {
10 |
11 | private lateinit var endsWithRule: EndsWithRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | endsWithRule = EndsWithRule(KEYWORD, "")
27 |
28 | endsWithRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_WithIgnoreCase_ReturnTrue() {
34 | endsWithRule = EndsWithRule(KEYWORD, "", true)
35 |
36 | val sample = "ends_with_TEST"
37 |
38 | assertTrue(endsWithRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_WithoutIgnoreCase_ReturnTrue() {
44 | endsWithRule = EndsWithRule(KEYWORD, "")
45 |
46 | val sample = "ends_with_test"
47 |
48 | assertTrue(endsWithRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_StringSample_WithoutIgnoreCase_ReturnFalse() {
54 | endsWithRule = EndsWithRule(KEYWORD, "")
55 |
56 | val sample = "ends_with_TEST"
57 |
58 | assertFalse(endsWithRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | endsWithRule = EndsWithRule(KEYWORD, errorMessage)
67 |
68 | assertEquals(errorMessage, endsWithRule.errorString)
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorRes() {
74 | @StringRes val errorRes = 0
75 |
76 | endsWithRule = EndsWithRule(KEYWORD, errorRes)
77 |
78 | assertEquals(errorRes, endsWithRule.errorRes)
79 | }
80 |
81 | companion object {
82 | private const val KEYWORD = "test"
83 | }
84 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/ContainRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class ContainRuleTest {
10 |
11 | private lateinit var containRule: ContainRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | containRule = ContainRule(STRING_KEYWORD, "")
27 |
28 | containRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_WithIgnoreCase_ReturnTrue() {
34 | containRule = ContainRule(STRING_KEYWORD, "", true)
35 |
36 | val sample = "contain_TEST"
37 |
38 | assertTrue(containRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_WithoutIgnoreCase_ReturnTrue() {
44 | containRule = ContainRule(STRING_KEYWORD, "")
45 |
46 | val sample = "contain_test"
47 |
48 | assertTrue(containRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_StringSample_WithoutIgnoreCase_ReturnFalse() {
54 | containRule = ContainRule(STRING_KEYWORD, "")
55 |
56 | val sample = "contain_TEST"
57 |
58 | assertFalse(containRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | containRule = ContainRule(STRING_KEYWORD, errorMessage)
67 |
68 | assertEquals(errorMessage, containRule.errorString)
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorRes() {
74 | @StringRes val errorRes = 0
75 |
76 | containRule = ContainRule(STRING_KEYWORD, errorRes)
77 |
78 | assertEquals(errorRes, containRule.errorRes)
79 | }
80 |
81 | companion object {
82 | private const val STRING_KEYWORD = "test"
83 | }
84 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/regex/DigitsRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class DigitsRuleTest {
10 |
11 | private lateinit var digitsRule: DigitsRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | digitsRule = DigitsRule("")
27 |
28 | digitsRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_SampleWithDigitsOnly_ReturnTrue() {
34 | digitsRule = DigitsRule("")
35 |
36 | val sample = "123"
37 |
38 | assertTrue(digitsRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_SampleWithAlphanumeric_ReturnFalse() {
44 | digitsRule = DigitsRule("")
45 |
46 | val sample = "abc123"
47 |
48 | assertFalse(digitsRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_SampleWithAlphabetOnly_ReturnFalse() {
54 | digitsRule = DigitsRule("")
55 |
56 | val sample = "abc"
57 |
58 | assertFalse(digitsRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun validate_SampleWithDigitsAndSymbols_ReturnFalse() {
64 | digitsRule = DigitsRule("")
65 |
66 | val sample = "100,000,000"
67 |
68 | assertFalse(digitsRule.validate(sample))
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorMessage() {
74 | val errorMessage = "This is a custom error message"
75 |
76 | digitsRule = DigitsRule(errorMessage)
77 |
78 | assertEquals(errorMessage, digitsRule.errorString)
79 | }
80 |
81 | @Test
82 | @Throws(Exception::class)
83 | fun errorMessage_CustomErrorRes() {
84 | @StringRes val errorRes = 0
85 |
86 | digitsRule = DigitsRule(errorRes)
87 |
88 | assertEquals(errorRes, digitsRule.errorRes)
89 | }
90 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/StartsWithRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class StartsWithRuleTest {
10 |
11 | private lateinit var startsWithRule: StartsWithRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | startsWithRule = StartsWithRule(KEYWORD, "")
27 |
28 | startsWithRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_StringSample_WithIgnoreCase_ReturnTrue() {
34 | startsWithRule = StartsWithRule(KEYWORD, "", true)
35 |
36 | val sample = "TEST_starts_with"
37 |
38 | assertTrue(startsWithRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_StringSample_WithoutIgnoreCase_ReturnTrue() {
44 | startsWithRule = StartsWithRule(KEYWORD, "")
45 |
46 | val sample = "test_starts_with"
47 |
48 | assertTrue(startsWithRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_StringSample_WithoutIgnoreCase_ReturnFalse() {
54 | startsWithRule = StartsWithRule(KEYWORD, "")
55 |
56 | val sample = "TEST_starts_with"
57 |
58 | assertFalse(startsWithRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | startsWithRule = StartsWithRule(KEYWORD, errorMessage)
67 |
68 | assertEquals(errorMessage, startsWithRule.errorString)
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun errorMessage_CustomErrorRes() {
74 | @StringRes val errorRes = 0
75 |
76 | startsWithRule = StartsWithRule(KEYWORD, errorRes)
77 |
78 | assertEquals(errorRes, startsWithRule.errorRes)
79 | }
80 |
81 | companion object {
82 | private const val KEYWORD = "test"
83 | }
84 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/regex/SymbolRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class SymbolRuleTest {
10 |
11 | private lateinit var symbolRule: SymbolRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | symbolRule = SymbolRule("")
27 |
28 | symbolRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_SampleWithDigitsOnly_ReturnTrue() {
34 | symbolRule = SymbolRule("")
35 |
36 | val sample = "123"
37 |
38 | assertFalse(symbolRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_SampleWithAlphanumeric_ReturnFalse() {
44 | symbolRule = SymbolRule("")
45 |
46 | val sample = "abc123"
47 |
48 | assertFalse(symbolRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_SampleWithAlphabetOnly_ReturnFalse() {
54 | symbolRule = SymbolRule("")
55 |
56 | val sample = "abc"
57 |
58 | assertFalse(symbolRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun validate_SampleWithDigitsAndSymbols_ReturnFalse() {
64 | symbolRule = SymbolRule("")
65 |
66 | val sample = "100,000,000"
67 |
68 | assertFalse(symbolRule.validate(sample))
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun validate_SampleWithSymbolsOnly_ReturnTrue() {
74 | symbolRule = SymbolRule("")
75 |
76 | val sample = "!@#$%^"
77 |
78 | assertTrue(symbolRule.validate(sample))
79 | }
80 |
81 | @Test
82 | @Throws(Exception::class)
83 | fun errorMessage_CustomErrorMessage() {
84 | val errorMessage = "This is a custom error message"
85 |
86 | symbolRule = SymbolRule(errorMessage)
87 |
88 | assertEquals(errorMessage, symbolRule.errorString)
89 | }
90 |
91 | @Test
92 | @Throws(Exception::class)
93 | fun errorMessage_CustomErrorRes() {
94 | @StringRes val errorRes = 0
95 |
96 | symbolRule = SymbolRule(errorRes)
97 |
98 | assertEquals(errorRes, symbolRule.errorRes)
99 | }
100 | }
--------------------------------------------------------------------------------
/dependencies.gradle:
--------------------------------------------------------------------------------
1 | ext {
2 | android = [
3 | compileSdkVersion : 28,
4 | minSdkVersion : 16,
5 | targetSdkVersion : 28,
6 | ]
7 |
8 | /* build plugins versions */
9 | build = [
10 | gradle : "7.0.2",
11 | kotlin : "1.5.31",
12 | ]
13 |
14 | buildPlugins = [
15 | gradle : "com.android.tools.build:gradle:$build.gradle",
16 | kotlin : "org.jetbrains.kotlin:kotlin-gradle-plugin:$build.kotlin",
17 | ]
18 |
19 | /* dependencies versions */
20 | def androidx_app_compat_version = '1.3.1'
21 | def androidx_material_version = '1.4.0'
22 | def junit_version = '4.13.2'
23 |
24 | def libs = [
25 | /* android support */
26 | supportAppCompat : "androidx.appcompat:appcompat:$androidx_app_compat_version",
27 | supportDesign : "com.google.android.material:material:$androidx_material_version",
28 |
29 | /* kotlin */
30 | kotlinStdlib : "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$build.kotlin",
31 | ]
32 |
33 | def testLibs = [
34 | jnuit : "junit:junit:$junit_version",
35 | ]
36 |
37 | def modules = [
38 | library : ":library"
39 | ]
40 |
41 | libraryModuleDependencies = [
42 | /* android support */
43 | [configuration: "implementation", dependency: libs.supportDesign],
44 |
45 | /* kotlin */
46 | [configuration: "implementation", dependency: libs.kotlinStdlib],
47 |
48 | /* test */
49 | [configuration: "testImplementation", dependency: testLibs.jnuit],
50 | ]
51 |
52 | sampleModuleDependencies = [
53 | /* android support */
54 | [configuration: "implementation", dependency: libs.supportAppCompat],
55 | [configuration: "implementation", dependency: libs.supportDesign],
56 |
57 | /* kotlin */
58 | [configuration: "implementation", dependency: libs.kotlinStdlib],
59 |
60 | /* modules */
61 | [configuration: "implementation", dependency: project(modules.library)],
62 | ]
63 |
64 | def plugins = [
65 | android_library : 'com.android.library',
66 | android_application : 'com.android.application',
67 | kotlin_android : 'kotlin-android',
68 | kotlin_android_extensions : 'kotlin-android-extensions',
69 | kotlin_kapt : 'kotlin-kapt',
70 | maven_publish : 'maven-publish',
71 | ]
72 |
73 | libraryModulePlugins = [
74 | plugins.android_library,
75 | plugins.kotlin_android,
76 | plugins.maven_publish,
77 | ]
78 |
79 | sampleModulePlugins = [
80 | plugins.android_application,
81 | plugins.kotlin_android,
82 | ]
83 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/conditions/ConditionTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.conditions
2 |
3 | import androidx.annotation.StringRes
4 | import io.github.anderscheow.validator.rules.common.MaxRule
5 | import io.github.anderscheow.validator.rules.common.MinRule
6 | import org.junit.After
7 | import org.junit.Assert.*
8 | import org.junit.Before
9 | import org.junit.Test
10 |
11 | class ConditionTest {
12 |
13 | private lateinit var condition: Condition
14 |
15 | @Before
16 | @Throws(Exception::class)
17 | fun setUp() {
18 | }
19 |
20 | @After
21 | @Throws(Exception::class)
22 | fun tearDown() {
23 | }
24 |
25 | @Test
26 | @Throws(Exception::class)
27 | fun add_OneBaseRule() {
28 | condition = object : Condition(MinRule(5, ""), "") {
29 | override fun validate(value: String?): Boolean {
30 | return false
31 | }
32 | }
33 |
34 | assertEquals(1, condition.rules.size.toLong())
35 | }
36 |
37 | @Test
38 | @Throws(Exception::class)
39 | fun add_TwoBaseRule() {
40 | condition = object : Condition(listOf(MinRule(5, ""), MaxRule(10, "")), "") {
41 | override fun validate(value: String?): Boolean {
42 | return false
43 | }
44 | }
45 |
46 | assertEquals(2, condition.rules.size.toLong())
47 | }
48 |
49 | @Test
50 | @Throws(Exception::class)
51 | fun addAll_TwoBaseRule() {
52 | condition = object : Condition(listOf(MinRule(5, ""), MaxRule(10, "")), "") {
53 | override fun validate(value: String?): Boolean {
54 | return false
55 | }
56 | }
57 |
58 | assertEquals(2, condition.rules.size.toLong())
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun errorMessage_CustomErrorMessage() {
64 | val errorMessage = "This is a custom error message"
65 |
66 | condition = object : Condition(MinRule(5, ""), errorMessage) {
67 | override fun validate(value: String?): Boolean {
68 | return false
69 | }
70 | }
71 |
72 | assertEquals(errorMessage, condition.errorString)
73 | assertTrue(condition.isErrorAvailable)
74 | assertTrue(condition.isErrorMessageAvailable)
75 | assertFalse(condition.isErrorResAvailable)
76 | }
77 |
78 | @Test
79 | @Throws(Exception::class)
80 | fun errorMessage_CustomErrorRes() {
81 | @StringRes val errorRes = 0
82 |
83 | condition = object : Condition(MinRule(5, ""), errorRes) {
84 | override fun validate(value: String?): Boolean {
85 | return false
86 | }
87 | }
88 |
89 | assertEquals(errorRes, condition.errorRes)
90 | assertTrue(condition.isErrorAvailable)
91 | assertTrue(condition.isErrorMessageAvailable)
92 | assertTrue(condition.isErrorResAvailable)
93 | }
94 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/regex/AlphabetRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class AlphabetRuleTest {
10 |
11 | private lateinit var alphabetRule: AlphabetRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | alphabetRule = AlphabetRule("")
27 |
28 | alphabetRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_SampleWithAlphabet_ReturnTrue() {
34 | alphabetRule = AlphabetRule("")
35 |
36 | val sample = "test"
37 |
38 | assertTrue(alphabetRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_SampleWithAlphabetAndNumbers_ReturnFalse() {
44 | alphabetRule = AlphabetRule("")
45 |
46 | val sample = "test 1000"
47 |
48 | assertFalse(alphabetRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_SampleWithAlphabetAndSpace_ReturnFalse() {
54 | alphabetRule = AlphabetRule("")
55 |
56 | val sample = "test test"
57 |
58 | assertFalse(alphabetRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun validate_SampleWithAlphabetAndSymbols_ReturnFalse() {
64 | alphabetRule = AlphabetRule("")
65 |
66 | val sample = "test test.test"
67 |
68 | assertFalse(alphabetRule.validate(sample))
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun validate_SampleWithDigitsOnly_ReturnFalse() {
74 | alphabetRule = AlphabetRule("")
75 |
76 | val sample = "123"
77 |
78 | assertFalse(alphabetRule.validate(sample))
79 | }
80 |
81 | @Test
82 | @Throws(Exception::class)
83 | fun validate_SampleWithSymbolsOnly_ReturnFalse() {
84 | alphabetRule = AlphabetRule("")
85 |
86 | val sample = ",./"
87 |
88 | assertFalse(alphabetRule.validate(sample))
89 | }
90 |
91 | @Test
92 | @Throws(Exception::class)
93 | fun errorMessage_CustomErrorMessage() {
94 | val errorMessage = "This is a custom error message"
95 |
96 | alphabetRule = AlphabetRule(errorMessage)
97 |
98 | assertEquals(errorMessage, alphabetRule.errorString)
99 | }
100 |
101 | @Test
102 | @Throws(Exception::class)
103 | fun errorMessage_CustomErrorRes() {
104 | @StringRes val errorRes = 0
105 |
106 | alphabetRule = AlphabetRule(errorRes)
107 |
108 | assertEquals(errorRes, alphabetRule.errorRes)
109 | }
110 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/regex/AlphanumericRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.regex
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 |
9 | class AlphanumericRuleTest {
10 |
11 | private lateinit var alphanumericRule: AlphanumericRule
12 |
13 | @Before
14 | @Throws(Exception::class)
15 | fun setUp() {
16 | }
17 |
18 | @After
19 | @Throws(Exception::class)
20 | fun tearDown() {
21 | }
22 |
23 | @Test(expected = NullPointerException::class)
24 | @Throws(Exception::class)
25 | fun validate_EmptySample_ThrowNullPointerException() {
26 | alphanumericRule = AlphanumericRule("")
27 |
28 | alphanumericRule.validate(null)
29 | }
30 |
31 | @Test
32 | @Throws(Exception::class)
33 | fun validate_SampleWithAlphanumeric_ReturnTrue() {
34 | alphanumericRule = AlphanumericRule("")
35 |
36 | val sample = "test123"
37 |
38 | assertTrue(alphanumericRule.validate(sample))
39 | }
40 |
41 | @Test
42 | @Throws(Exception::class)
43 | fun validate_SampleWithAlphanumericAndSpace_ReturnFalse() {
44 | alphanumericRule = AlphanumericRule("")
45 |
46 | val sample = "test 123"
47 |
48 | assertFalse(alphanumericRule.validate(sample))
49 | }
50 |
51 | @Test
52 | @Throws(Exception::class)
53 | fun validate_SampleWithAlphanumericAndSymbols_ReturnFalse() {
54 | alphanumericRule = AlphanumericRule("")
55 |
56 | val sample = "test 100,000,000"
57 |
58 | assertFalse(alphanumericRule.validate(sample))
59 | }
60 |
61 | @Test
62 | @Throws(Exception::class)
63 | fun validate_SampleWithAlphabetOnly_ReturnTrue() {
64 | alphanumericRule = AlphanumericRule("")
65 |
66 | val sample = "test"
67 |
68 | assertTrue(alphanumericRule.validate(sample))
69 | }
70 |
71 | @Test
72 | @Throws(Exception::class)
73 | fun validate_SampleWithDigitsOnly_ReturnTrue() {
74 | alphanumericRule = AlphanumericRule("")
75 |
76 | val sample = "123"
77 |
78 | assertTrue(alphanumericRule.validate(sample))
79 | }
80 |
81 | @Test
82 | @Throws(Exception::class)
83 | fun validate_SampleWithSymbolsOnly_ReturnFalse() {
84 | alphanumericRule = AlphanumericRule("")
85 |
86 | val sample = ",./"
87 |
88 | assertFalse(alphanumericRule.validate(sample))
89 | }
90 |
91 | @Test
92 | @Throws(Exception::class)
93 | fun errorMessage_CustomErrorMessage() {
94 | val errorMessage = "This is a custom error message"
95 |
96 | alphanumericRule = AlphanumericRule(errorMessage)
97 |
98 | assertEquals(errorMessage, alphanumericRule.errorString)
99 | }
100 |
101 | @Test
102 | @Throws(Exception::class)
103 | fun errorMessage_CustomErrorRes() {
104 | @StringRes val errorRes = 0
105 |
106 | alphanumericRule = AlphanumericRule(errorRes)
107 |
108 | assertEquals(errorRes, alphanumericRule.errorRes)
109 | }
110 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/PastRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 | import java.text.SimpleDateFormat
9 | import java.util.*
10 |
11 | class PastRuleTest {
12 |
13 | private lateinit var pastRule: PastRule
14 |
15 | @Before
16 | @Throws(Exception::class)
17 | fun setUp() {
18 | }
19 |
20 | @After
21 | @Throws(Exception::class)
22 | fun tearDown() {
23 | }
24 |
25 | @Test(expected = NullPointerException::class)
26 | @Throws(Exception::class)
27 | fun validate_EmptySample_ThrowNullPointerException() {
28 | pastRule = PastRule(VALID_DATE_FORMAT, "")
29 |
30 | pastRule.validate(null)
31 | }
32 |
33 | @Test
34 | @Throws(Exception::class)
35 | fun validate_ValidDateFormatAndValidValue_ReturnTrue() {
36 | pastRule = PastRule(VALID_DATE_FORMAT, "")
37 |
38 | val calendar = Calendar.getInstance()
39 | calendar.time = Date()
40 | calendar.add(Calendar.DAY_OF_YEAR, -1)
41 |
42 | val sample = VALID_DATE_FORMAT.format(calendar.time)
43 |
44 | assertTrue(pastRule.validate(sample))
45 | }
46 |
47 | @Test
48 | @Throws(Exception::class)
49 | fun validate_ValidDateFormatAndInvalidValue_ReturnFalse() {
50 | pastRule = PastRule(VALID_DATE_FORMAT, "")
51 |
52 | val calendar = Calendar.getInstance()
53 | calendar.time = Date()
54 | calendar.add(Calendar.DAY_OF_YEAR, 1)
55 |
56 | val sample = VALID_DATE_FORMAT.format(calendar.time)
57 |
58 | assertFalse(pastRule.validate(sample))
59 | }
60 |
61 | @Test(expected = IllegalArgumentException::class)
62 | @Throws(Exception::class)
63 | fun validate_InvalidDateFormatAndValidValue_ThrowIllegalArgumentException() {
64 | pastRule = PastRule(SimpleDateFormat("abc/dd/ee"), "")
65 |
66 | val sample = "5/12/2017"
67 |
68 | pastRule.validate(sample)
69 | }
70 |
71 | @Test(expected = IllegalArgumentException::class)
72 | @Throws(Exception::class)
73 | fun validate_InvalidDateFormatAndInvalidValue_ThrowIllegalArgumentException() {
74 | pastRule = PastRule(SimpleDateFormat("abc/dd/ee"), "")
75 |
76 | val sample = "100/100/2017"
77 |
78 | pastRule.validate(sample)
79 | }
80 | @Test
81 | @Throws(Exception::class)
82 | fun errorMessage_CustomErrorMessage() {
83 | val errorMessage = "This is a custom error message"
84 |
85 | pastRule = PastRule(VALID_DATE_FORMAT, errorMessage)
86 |
87 | assertEquals(errorMessage, pastRule.errorString)
88 | }
89 |
90 | @Test
91 | @Throws(Exception::class)
92 | fun errorMessage_CustomErrorRes() {
93 | @StringRes val errorRes = 0
94 |
95 | pastRule = PastRule(VALID_DATE_FORMAT, errorRes)
96 |
97 | assertEquals(errorRes, pastRule.errorRes)
98 | }
99 |
100 | companion object {
101 | private val VALID_DATE_FORMAT = SimpleDateFormat("dd/MM/yyyy")
102 | }
103 | }
--------------------------------------------------------------------------------
/library/src/test/java/io/github/anderscheow/validator/rules/common/FutureRuleTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.rules.common
2 |
3 | import androidx.annotation.StringRes
4 | import org.junit.After
5 | import org.junit.Assert.*
6 | import org.junit.Before
7 | import org.junit.Test
8 | import java.text.SimpleDateFormat
9 | import java.util.*
10 |
11 | class FutureRuleTest {
12 |
13 | private lateinit var futureRule: FutureRule
14 |
15 | @Before
16 | @Throws(Exception::class)
17 | fun setUp() {
18 | }
19 |
20 | @After
21 | @Throws(Exception::class)
22 | fun tearDown() {
23 | }
24 |
25 | @Test(expected = NullPointerException::class)
26 | @Throws(Exception::class)
27 | fun validate_EmptySample_ThrowNullPointerException() {
28 | futureRule = FutureRule(VALID_DATE_FORMAT, "")
29 |
30 | futureRule.validate(null)
31 | }
32 |
33 | @Test
34 | @Throws(Exception::class)
35 | fun validate_ValidDateFormatAndValidValue_ReturnTrue() {
36 | futureRule = FutureRule(VALID_DATE_FORMAT, "")
37 |
38 | val calendar = Calendar.getInstance()
39 | calendar.time = Date()
40 | calendar.add(Calendar.DAY_OF_YEAR, 1)
41 |
42 | val sample = VALID_DATE_FORMAT.format(calendar.time)
43 |
44 | assertTrue(futureRule.validate(sample))
45 | }
46 |
47 | @Test
48 | @Throws(Exception::class)
49 | fun validate_ValidDateFormatAndInvalidValue_ReturnFalse() {
50 | futureRule = FutureRule(VALID_DATE_FORMAT, "")
51 |
52 | val calendar = Calendar.getInstance()
53 | calendar.time = Date()
54 | calendar.add(Calendar.DAY_OF_YEAR, -1)
55 |
56 | val sample = VALID_DATE_FORMAT.format(calendar.time)
57 |
58 | assertFalse(futureRule.validate(sample))
59 | }
60 |
61 | @Test(expected = IllegalArgumentException::class)
62 | @Throws(Exception::class)
63 | fun validate_InvalidDateFormatAndValidValue_ThrowIllegalArgumentException() {
64 | futureRule = FutureRule(SimpleDateFormat("abc/dd/ee"), "")
65 |
66 | val sample = "5/12/2017"
67 |
68 | futureRule.validate(sample)
69 | }
70 |
71 | @Test(expected = IllegalArgumentException::class)
72 | @Throws(Exception::class)
73 | fun validate_InvalidDateFormatAndInvalidValue_ThrowIllegalArgumentException() {
74 | futureRule = FutureRule(SimpleDateFormat("abc/dd/ee"), "")
75 |
76 | val sample = "100/100/2017"
77 |
78 | futureRule.validate(sample)
79 | }
80 |
81 | @Test
82 | @Throws(Exception::class)
83 | fun errorMessage_CustomErrorMessage() {
84 | val errorMessage = "This is a custom error message"
85 |
86 | futureRule = FutureRule(VALID_DATE_FORMAT, errorMessage)
87 |
88 | assertEquals(errorMessage, futureRule.errorString)
89 | }
90 |
91 | @Test
92 | @Throws(Exception::class)
93 | fun errorMessage_CustomErrorRes() {
94 | @StringRes val errorRes = 0
95 |
96 | futureRule = FutureRule(VALID_DATE_FORMAT, errorRes)
97 |
98 | assertEquals(errorRes, futureRule.errorRes)
99 | }
100 |
101 | companion object {
102 | private val VALID_DATE_FORMAT = SimpleDateFormat("dd/MM/yyyy")
103 | }
104 | }
--------------------------------------------------------------------------------
/sample/src/main/java/io/github/anderscheow/validator/sample/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package io.github.anderscheow.validator.sample
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import android.widget.Button
6 | import android.widget.Toast
7 | import androidx.appcompat.app.AppCompatActivity
8 | import com.google.android.material.textfield.TextInputLayout
9 | import io.github.anderscheow.validator.Validator
10 | import io.github.anderscheow.validator.conditions.common.and
11 | import io.github.anderscheow.validator.conditions.common.or
12 | import io.github.anderscheow.validator.constant.Mode
13 | import io.github.anderscheow.validator.rules.common.contain
14 | import io.github.anderscheow.validator.rules.common.endsWith
15 | import io.github.anderscheow.validator.rules.common.minimumLength
16 | import io.github.anderscheow.validator.rules.common.notEmpty
17 | import io.github.anderscheow.validator.rules.regex.email
18 | import io.github.anderscheow.validator.validation
19 | import io.github.anderscheow.validator.validator
20 |
21 | class MainActivity : AppCompatActivity() {
22 |
23 | override fun onCreate(savedInstanceState: Bundle?) {
24 | super.onCreate(savedInstanceState)
25 | setContentView(R.layout.activity_main)
26 |
27 | val usernameInput = findViewById(R.id.layout_username)
28 | val passwordInput = findViewById(R.id.layout_password)
29 | val submitButton = findViewById