-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Telefonica/appscore/APPS-7094_AddValidati…
…onsForInputs Add email, phone and custom validations for TextInput.
- Loading branch information
Showing
5 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
library/src/main/java/com/telefonica/mistica/input/validations/Validations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.telefonica.mistica.input.validations | ||
|
||
import java.util.regex.Pattern | ||
|
||
interface TextInputValidation { | ||
fun isValid(text: String?): TextInputValidationResult | ||
} | ||
|
||
sealed class TextInputValidationResult { | ||
object Success : TextInputValidationResult() | ||
class Invalid(val invalidMessage: String) : TextInputValidationResult() | ||
} | ||
|
||
class EmailTextInputValidation(private val invalidMessage: String) : TextInputValidation { | ||
override fun isValid(text: String?): TextInputValidationResult = | ||
if (!text.isNullOrBlank() && emailPattern.matcher(text).matches()) { | ||
TextInputValidationResult.Success | ||
} else { | ||
TextInputValidationResult.Invalid(invalidMessage) | ||
} | ||
} | ||
|
||
class PhoneTextInputValidation(private val invalidMessage: String) : TextInputValidation { | ||
override fun isValid(text: String?): TextInputValidationResult = | ||
if (!text.isNullOrBlank() && phonePattern.matcher(text).matches()) { | ||
TextInputValidationResult.Success | ||
} else { | ||
TextInputValidationResult.Invalid(invalidMessage) | ||
} | ||
|
||
} | ||
|
||
class NoValidation() : TextInputValidation { | ||
override fun isValid(text: String?) = TextInputValidationResult.Success | ||
} | ||
|
||
private val emailPattern = Pattern.compile( | ||
"""[a-zA-Z0-9\+\.\_\%\-\+]{1,256}\@[a-zA-Z0-9][a-zA-Z0-9\-]{0,64}(\.[a-zA-Z0-9][a-zA-Z0-9\-]{0,25})+""" | ||
) | ||
|
||
private val phonePattern: Pattern = Pattern.compile( | ||
"""(\+[0-9]+[\- \.]*)?(\([0-9]+\)[\- \.]*)?([0-9][0-9\- \.]+[0-9])""" | ||
) |
149 changes: 149 additions & 0 deletions
149
library/src/test/java/com/telefonica/mistica/input/validations/TextInputValidationsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package com.telefonica.mistica.input.validations | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
|
||
class TextInputValidationsTest { | ||
|
||
@Test | ||
fun `should return Success when validate a valid email`() { | ||
val emailTextInputValidation = givenAnEmailTextInputValidation() | ||
|
||
val validationResult = emailTextInputValidation.isValid(validEmail) | ||
|
||
assertTrue(validationResult is TextInputValidationResult.Success) | ||
} | ||
|
||
@Test | ||
fun `should return Invalid with message when validate an invalid email`() { | ||
val emailTextInputValidation = givenAnEmailTextInputValidation() | ||
|
||
val validationResult = emailTextInputValidation.isValid(invalidEmail) | ||
|
||
(validationResult as TextInputValidationResult.Invalid).apply { | ||
assertEquals(invalidMessageEmail, this.invalidMessage) | ||
} | ||
} | ||
|
||
@Test | ||
fun `should return Invalid with message when validate a null email`() { | ||
val emailTextInputValidation = givenAnEmailTextInputValidation() | ||
|
||
val validationResult = emailTextInputValidation.isValid(null) | ||
|
||
assertTrue(validationResult is TextInputValidationResult.Invalid) | ||
} | ||
|
||
@Test | ||
fun `should return Invalid with message when validate an empty email`() { | ||
val emailTextInputValidation = givenAnEmailTextInputValidation() | ||
|
||
val validationResult = emailTextInputValidation.isValid("") | ||
|
||
assertTrue(validationResult is TextInputValidationResult.Invalid) | ||
} | ||
|
||
@Test | ||
fun `should return Success when validate a valid phone`() { | ||
val phoneTextInputValidation = givenAPhoneTextInputValidation() | ||
|
||
val validationResult = phoneTextInputValidation.isValid(validPhone) | ||
|
||
assertTrue(validationResult is TextInputValidationResult.Success) | ||
} | ||
|
||
@Test | ||
fun `should return Invalid with message when validate an invalid phone`() { | ||
val phoneTextInputValidation = givenAPhoneTextInputValidation() | ||
|
||
val validationResult = phoneTextInputValidation.isValid(invalidPhone) | ||
|
||
(validationResult as TextInputValidationResult.Invalid).apply { | ||
assertEquals(invalidMessagePhone, this.invalidMessage) | ||
} | ||
} | ||
|
||
@Test | ||
fun `should return Invalid with message when validate a null phone`() { | ||
val phoneTextInputValidation = givenAPhoneTextInputValidation() | ||
|
||
val validationResult = phoneTextInputValidation.isValid(null) | ||
|
||
assertTrue(validationResult is TextInputValidationResult.Invalid) | ||
} | ||
|
||
@Test | ||
fun `should return Invalid with message when validate an empty phone`() { | ||
val phoneTextInputValidation = givenAPhoneTextInputValidation() | ||
|
||
val validationResult = phoneTextInputValidation.isValid("") | ||
|
||
assertTrue(validationResult is TextInputValidationResult.Invalid) | ||
} | ||
|
||
@Test | ||
fun `should return Success when validate with a custom validator a valid String`() { | ||
val customTextInputValidation = givenACustomTextInputValidation() | ||
|
||
val validationResult = customTextInputValidation.isValid(validReversedText) | ||
|
||
assert(validationResult is TextInputValidationResult.Success) | ||
} | ||
|
||
@Test | ||
fun `should return Invalid when validate with a custom validator an invalid String`() { | ||
val customTextInputValidation = givenACustomTextInputValidation() | ||
|
||
val validationResult = customTextInputValidation.isValid(invalidReversedText) | ||
|
||
(validationResult as TextInputValidationResult.Invalid).apply { | ||
assertEquals(invalidReversedTextMessage, this.invalidMessage) | ||
} | ||
} | ||
|
||
@Test | ||
fun `should return Invalid when validate with a custom validator a null String`() { | ||
val customTextInputValidation = givenACustomTextInputValidation() | ||
|
||
val validationResult = customTextInputValidation.isValid(null) | ||
|
||
assertTrue(validationResult is TextInputValidationResult.Invalid) | ||
} | ||
|
||
@Test | ||
fun `should return Invalid when validate with a custom validator an empty String`() { | ||
val customTextInputValidation = givenACustomTextInputValidation() | ||
|
||
val validationResult = customTextInputValidation.isValid("") | ||
|
||
assertTrue(validationResult is TextInputValidationResult.Invalid) | ||
} | ||
|
||
private fun givenAnEmailTextInputValidation() = EmailTextInputValidation(invalidMessageEmail) | ||
|
||
private fun givenAPhoneTextInputValidation() = PhoneTextInputValidation(invalidMessagePhone) | ||
|
||
private fun givenACustomTextInputValidation(): TextInputValidation = | ||
object : TextInputValidation { | ||
override fun isValid(text: String?): TextInputValidationResult = | ||
if (text?.equals(text.reversed()) == true && !text.isNullOrEmpty()) { | ||
TextInputValidationResult.Success | ||
} else { | ||
TextInputValidationResult.Invalid(invalidReversedTextMessage) | ||
} | ||
} | ||
|
||
companion object { | ||
const val invalidMessageEmail = "Invalid email" | ||
const val invalidMessagePhone = "Invalid phone" | ||
const val validEmail = "[email protected]" | ||
const val invalidEmail = "email.telefonica.com" | ||
const val validPhone = "666666666" | ||
const val invalidPhone = "66" | ||
const val invalidReversedTextMessage = "Invalid palindrome" | ||
const val validReversedText = "qwertyuiopoiuytrewq" | ||
const val invalidReversedText = "qwertyuiopqwertyuiop" | ||
} | ||
|
||
} |