Skip to content

Commit f1a41ad

Browse files
authored
Merge pull request #30 from CORDEA/feature/no-special-character
Add new validator to ignore special characters
2 parents da5c9fc + 82ffbc5 commit f1a41ad

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 contains special characters.
10+
*/
11+
class NoSpecialCharacterValidator(
12+
private val errorMessage: String
13+
) : VtlValidator {
14+
companion object {
15+
private const val VS15 = '\uFE0E'
16+
private const val VS16 = '\uFE0F'
17+
18+
private const val ZERO_WIDTH_JOINER = '\u200D'
19+
private const val ENCLOSING_KEYCAP = '\u20E3'
20+
}
21+
22+
override fun validateAsCompletable(context: Context, text: String?): Completable =
23+
Completable.fromRunnable {
24+
if (!validate(text)) {
25+
throw VtlValidationFailureException(errorMessage)
26+
}
27+
}.subscribeOn(Schedulers.computation())
28+
29+
override fun validate(text: String?): Boolean {
30+
if (text.isNullOrBlank()) {
31+
return true
32+
}
33+
return text.none {
34+
val type = Character.getType(it).toByte()
35+
it == VS15
36+
|| it == VS16
37+
|| it == ZERO_WIDTH_JOINER
38+
|| it == ENCLOSING_KEYCAP
39+
|| type == Character.SURROGATE
40+
|| type == Character.OTHER_SYMBOL
41+
}
42+
}
43+
44+
override fun getErrorMessage(): String? = errorMessage
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
@RunWith(ParameterizedRobolectricTestRunner::class)
14+
class NoSpecialCharacterValidatorTest(
15+
private val text: String?,
16+
private val result: Boolean,
17+
private val errorMessage: String?
18+
) {
19+
companion object {
20+
private const val ERROR_MESSAGE = "This contains special characters"
21+
22+
@JvmStatic
23+
@ParameterizedRobolectricTestRunner.Parameters
24+
fun data(): List<Array<out Any?>> =
25+
listOf(
26+
// CJK Unified Ideographs Extension B
27+
arrayOf("\uD844\uDE3D", false, ERROR_MESSAGE),
28+
// Folded Hands
29+
arrayOf("\uD83D\uDE4F", false, ERROR_MESSAGE),
30+
// Grinning Face
31+
arrayOf("\uD83D\uDE00", false, ERROR_MESSAGE),
32+
// Telephone
33+
arrayOf("\u260E\uFE0F", false, ERROR_MESSAGE),
34+
// Telephone
35+
arrayOf("\u260E\uFE0E", false, ERROR_MESSAGE),
36+
// Man Astronaut
37+
arrayOf("\uD83D\uDC68\u200D\uD83D\uDE80", false, ERROR_MESSAGE),
38+
// Skin Tone
39+
arrayOf("\uD83C\uDFFD", false, ERROR_MESSAGE),
40+
// Keycap Digit 1
41+
arrayOf("1\uFE0F\u20E3", false, ERROR_MESSAGE),
42+
arrayOf(null, true, null),
43+
arrayOf(" ", true, null),
44+
arrayOf("ABCD", true, null),
45+
arrayOf("あいうえお", true, null),
46+
arrayOf("Aaあ文1!@#$%^&*()_=-+\";:'{}|\\[]<>?,.", true, null),
47+
// CJK Unified Ideographs Extension A
48+
arrayOf("\u4DB5", true, null)
49+
)
50+
}
51+
52+
@get:Rule
53+
val rxImmediateSchedulerRule = RxImmediateSchedulerRule()
54+
55+
private lateinit var validator: VtlValidator
56+
57+
private val context: Context = RuntimeEnvironment.application
58+
59+
@Before
60+
fun setUp() {
61+
validator = NoSpecialCharacterValidator(ERROR_MESSAGE)
62+
}
63+
64+
@Test
65+
fun validate() {
66+
assertEquals(result, validator.validate(text))
67+
}
68+
69+
@Test
70+
fun validateAsCompletable() {
71+
if (errorMessage == null) {
72+
validator.validateAsCompletable(context, text).test().assertNoErrors().assertComplete()
73+
} else {
74+
validator.validateAsCompletable(context, text).test().assertErrorMessage(errorMessage)
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)