-
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.
* rewrite the validator class * add isValid State to the validator * update Validator documentation * fix codestyle errors
- Loading branch information
1 parent
6dd7f69
commit d2df31f
Showing
8 changed files
with
171 additions
and
112 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
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
20 changes: 0 additions & 20 deletions
20
validable/src/main/java/tech/devscast/validable/WithValidable.kt
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
validable/src/main/java/tech/devscast/validable/core/Validator.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,53 @@ | ||
package tech.devscast.validable.core | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.produceState | ||
import androidx.compose.runtime.remember | ||
import tech.devscast.validable.BaseValidable | ||
|
||
/** | ||
* Creates a new instance of a [Validator] | ||
* | ||
* @param fields | ||
* all fields to validate | ||
*/ | ||
@Composable | ||
fun rememberValidator( | ||
vararg fields: BaseValidable, | ||
) = remember { | ||
Validator(fields = fields) | ||
} | ||
|
||
class Validator(private vararg val fields: BaseValidable) { | ||
|
||
/** | ||
* Represents the validity of the fields. | ||
* Can trigger re-composition. | ||
*/ | ||
val isValid: Boolean | ||
@Composable | ||
get() { | ||
val isValidValue by produceState(initialValue = false, fields.map { it.isValid }) { | ||
this.value = fields.none { it.isValid.not() } | ||
} | ||
return isValidValue | ||
} | ||
|
||
/** | ||
* Executes the provided action only if all tracked fields are currently valid. | ||
* | ||
* @param action | ||
* The action to be executed if all fields are valid. | ||
*/ | ||
fun validate(action: () -> Unit) { | ||
val isThereAnyError = fields.map { | ||
it.enableShowErrors() | ||
it.hasError() | ||
}.contains(true) | ||
|
||
if (isThereAnyError.not()) { | ||
action() | ||
} | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
validable/src/main/java/tech/devscast/validable/delegates/ValidablesDelegates.kt
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
validable/src/test/java/tech/devscast/validable/core/ValidatorTest.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,39 @@ | ||
package tech.devscast.validable.core | ||
|
||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import tech.devscast.validable.EmailValidable | ||
import tech.devscast.validable.EqualToValidable | ||
|
||
class ValidatorTest { | ||
|
||
@Test | ||
fun `test validator when input are not valid`(){ | ||
val validable1 = EmailValidable() | ||
validable1.value = "example" | ||
val validable2 = EqualToValidable("Hello world") | ||
validable2.value = "Hello" | ||
val validator = Validator(validable1,validable2) | ||
var isValid = false | ||
validator.validate { | ||
isValid = true | ||
} | ||
assertFalse("Should be false",isValid) | ||
} | ||
|
||
@Test | ||
|
||
fun `test validator when input are valid`(){ | ||
val validable1 = EmailValidable() | ||
validable1.value = "[email protected]" | ||
val validable2 = EqualToValidable("Hello world") | ||
validable2.value = "Hello world" | ||
val validator = Validator(validable1,validable2) | ||
var isValid = false | ||
validator.validate { | ||
isValid = true | ||
} | ||
assertTrue("Should be true",isValid) | ||
} | ||
} |