Skip to content

Commit

Permalink
update(API) : remove vararg from CardScheme (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
yveskalume authored May 30, 2024
1 parent 3df5e24 commit bed4b6a
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 15 deletions.
6 changes: 0 additions & 6 deletions docs/validables/cardscheme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ val cardField = CardSchemeValidable(
"Only MasterCard is supported"
)

// Supporting multiple cardScheme
val multipleCardField = CardSchemeValidable(
CardScheme.MasterCard,CardScheme.Visa,
"Only MasterCard and Visa are supported"
)

// Merging multiple CardSchemes into one
val mixedCardScheme = CardScheme.merge(CardScheme.MasterCard, CardScheme.Visa)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fun InputScreen() {
val multipleCardField = remember {
CardSchemeValidable(
CardScheme.merge(CardScheme.MasterCard, CardScheme.Visa),
message = "Invalid Card"
"Invalid Card"
)
}

Expand Down
4 changes: 2 additions & 2 deletions validable/api/validable.api
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public final class tech/devscast/validable/CardScheme$Visa : tech/devscast/valid

public final class tech/devscast/validable/CardSchemeValidable : tech/devscast/validable/BaseValidable {
public static final field $stable I
public fun <init> ([Ltech/devscast/validable/CardScheme;Ljava/lang/String;)V
public synthetic fun <init> ([Ltech/devscast/validable/CardScheme;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ltech/devscast/validable/CardScheme;Ljava/lang/String;)V
public synthetic fun <init> (Ltech/devscast/validable/CardScheme;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class tech/devscast/validable/EmailValidable : tech/devscast/validable/BaseValidable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import java.util.regex.Pattern
* @param message
* (Optional) Custom error message for validation failure.
*/
class CardSchemeValidable(vararg schemes: CardScheme, message: String? = null) : BaseValidable(
class CardSchemeValidable(scheme: CardScheme, message: String? = null) : BaseValidable(
validator = { value ->
schemes.any { scheme ->
val compiledPatterns = scheme.patterns.map { Pattern.compile(it) }
compiledPatterns.any { pattern -> pattern.matcher(value).matches() }
}
val compiledPatterns = scheme.patterns.map { Pattern.compile(it) }
compiledPatterns.any { pattern -> pattern.matcher(value).matches() }
},
errorFor = { _ -> message ?: "Unsupported card type or invalid card number." }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ class CardSchemeValidableTest {
// to test the vararg constructor
@Test
fun `valid card numbers with multiple scheme validable are valid`() {
val mergedScheme = CardScheme.merge(CardScheme.Visa, CardScheme.MasterCard)
getValidCardMap()
.filter { it.key == CardScheme.Visa || it.key == CardScheme.MasterCard }
.forEach { card ->
validable = CardSchemeValidable(CardScheme.Visa, CardScheme.MasterCard)
validable = CardSchemeValidable(mergedScheme)
validable.enableShowErrors()
validable.value = card.value
Assert.assertFalse(validable.errorMessage, validable.hasError())
Expand Down

0 comments on commit bed4b6a

Please sign in to comment.