Skip to content

Commit daeb6a8

Browse files
committed
Add new validator to ignore special characters
1 parent da5c9fc commit daeb6a8

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-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+
}

0 commit comments

Comments
 (0)