Skip to content

Commit ba30644

Browse files
authored
Merge pull request #26 from Kyash/modify_validator
Added MinLengthValidator and test
2 parents 508e748 + b65084a commit ba30644

File tree

4 files changed

+138
-6
lines changed

4 files changed

+138
-6
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package co.kyash.vtl.validators
2+
3+
import android.content.Context
4+
import co.kyash.vtl.VtlValidationFailureException
5+
import io.reactivex.Completable
6+
import io.reactivex.schedulers.Schedulers
7+
8+
/**
9+
* Validation error when the text length is shorter
10+
*/
11+
class MinLengthValidator(
12+
private val errorMessage: String,
13+
private val minLength: Int,
14+
private val trim: Boolean = true
15+
) : VtlValidator {
16+
17+
/**
18+
* Validate and return completable
19+
*
20+
* @param context
21+
* @param text
22+
* @return Completable
23+
* @throws Exception which contains the error message
24+
*/
25+
override fun validateAsCompletable(context: Context, text: String?): Completable {
26+
return Completable.fromRunnable {
27+
if (!validate(text)) {
28+
throw VtlValidationFailureException(errorMessage)
29+
}
30+
}.subscribeOn(Schedulers.computation())
31+
}
32+
33+
/**
34+
* Validate immediately
35+
*
36+
* @param text
37+
* @return result
38+
*/
39+
override fun validate(text: String?): Boolean {
40+
return text?.let {
41+
if (trim) it.trim() else it
42+
}?.length ?: 0 >= minLength
43+
}
44+
45+
/**
46+
* @return error message
47+
*/
48+
override fun getErrorMessage(): String {
49+
return errorMessage
50+
}
51+
52+
}

library/src/main/java/co/kyash/vtl/validators/RequiredValidator.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import io.reactivex.schedulers.Schedulers
1010
* Validation error when the text is empty.
1111
*/
1212
class RequiredValidator(
13-
private val errorMessage: String
13+
private val errorMessage: String,
14+
private val trim: Boolean = true
1415
) : VtlValidator {
1516

1617
/**
@@ -36,7 +37,7 @@ class RequiredValidator(
3637
* @return result
3738
*/
3839
override fun validate(text: String?): Boolean {
39-
return !TextUtils.isEmpty(text)
40+
return !TextUtils.isEmpty(text?.let { if (trim) it.trim() else it })
4041
}
4142

4243
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package co.kyash.vtl.validators
2+
3+
import android.content.Context
4+
import co.kyash.vtl.testing.RxImmediateSchedulerRule
5+
import junit.framework.Assert.assertEquals
6+
import org.junit.Before
7+
import org.junit.Rule
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import org.robolectric.ParameterizedRobolectricTestRunner
11+
import org.robolectric.RuntimeEnvironment
12+
13+
@Suppress("unused")
14+
@RunWith(ParameterizedRobolectricTestRunner::class)
15+
class MinLengthValidatorTest(
16+
private val text: String?,
17+
private val trim: Boolean,
18+
private val result: Boolean,
19+
private val errorMessage: String?
20+
) {
21+
22+
companion object {
23+
private val MIN_LENGTH = 5
24+
private val ERROR_MESSAGE = "This field has error"
25+
26+
@JvmStatic
27+
@ParameterizedRobolectricTestRunner.Parameters
28+
fun data(): List<Array<out Any?>> {
29+
return listOf(
30+
// Failure
31+
arrayOf(null, true, false, ERROR_MESSAGE),
32+
arrayOf("", true, false, ERROR_MESSAGE),
33+
arrayOf(" ", true, false, ERROR_MESSAGE),
34+
arrayOf("abcd", true, false, ERROR_MESSAGE),
35+
36+
// Success
37+
arrayOf(" ", false, true, null),
38+
arrayOf("abcde", true, true, null),
39+
arrayOf("abcdef", true, true, null)
40+
)
41+
}
42+
}
43+
44+
@get:Rule
45+
val rxImmediateSchedulerRule = RxImmediateSchedulerRule()
46+
47+
private lateinit var subject: VtlValidator
48+
49+
private val context: Context = RuntimeEnvironment.application
50+
51+
@Before
52+
@Throws(Exception::class)
53+
fun setUp() {
54+
subject = MinLengthValidator(ERROR_MESSAGE, MIN_LENGTH, trim)
55+
}
56+
57+
@Test
58+
fun validate() {
59+
assertEquals(result, subject.validate(text))
60+
}
61+
62+
@Test
63+
fun validateAsCompletable() {
64+
if (errorMessage == null) {
65+
subject.validateAsCompletable(context, text).test().assertNoErrors().assertComplete()
66+
} else {
67+
subject.validateAsCompletable(context, text).test().assertErrorMessage(errorMessage)
68+
}
69+
}
70+
71+
}

library/src/test/java/co/kyash/vtl/validators/RequiredValidatorTest.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.robolectric.RuntimeEnvironment
1414
@RunWith(ParameterizedRobolectricTestRunner::class)
1515
class RequiredValidatorTest(
1616
private val text: String?,
17+
private val trim: Boolean,
1718
private val result: Boolean,
1819
private val errorMessage: String?
1920
) {
@@ -25,9 +26,16 @@ class RequiredValidatorTest(
2526
@ParameterizedRobolectricTestRunner.Parameters
2627
fun data(): List<Array<out Any?>> {
2728
return listOf(
28-
arrayOf(null, false, ERROR_MESSAGE),
29-
arrayOf("", false, ERROR_MESSAGE),
30-
arrayOf("konifar", true, null)
29+
// Failure
30+
arrayOf(null, true, false, ERROR_MESSAGE),
31+
arrayOf("", true, false, ERROR_MESSAGE),
32+
arrayOf(" ", true, false, ERROR_MESSAGE),
33+
arrayOf(" ", true, false, ERROR_MESSAGE),
34+
35+
// Success
36+
arrayOf(" ", false, true, null),
37+
arrayOf(" ", false, true, null),
38+
arrayOf("konifar", true, true, null)
3139
)
3240
}
3341
}
@@ -42,7 +50,7 @@ class RequiredValidatorTest(
4250
@Before
4351
@Throws(Exception::class)
4452
fun setUp() {
45-
subject = RequiredValidator(ERROR_MESSAGE)
53+
subject = RequiredValidator(ERROR_MESSAGE, trim)
4654
}
4755

4856
@Test

0 commit comments

Comments
 (0)