-
Notifications
You must be signed in to change notification settings - Fork 2
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 #39 from ReneeVandervelde/data-module
Add Transformed validator functions
- Loading branch information
Showing
3 changed files
with
63 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
27 changes: 27 additions & 0 deletions
27
data/src/commonMain/kotlin/com/inkapplications/data/validator/TransformedValidator.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,27 @@ | ||
package com.inkapplications.data.validator | ||
|
||
import com.inkapplications.data.transformer.DataTransformer | ||
|
||
/** | ||
* Runs a validation check after first transforming the input to a different type. | ||
*/ | ||
private class TransformedValidator<INPUT, VALIDATION>( | ||
private val transformer: DataTransformer<INPUT, VALIDATION>, | ||
private val validator: DataValidator<VALIDATION>, | ||
): DataValidator<INPUT> { | ||
override suspend fun validate(input: INPUT): ValidationResult { | ||
return validator.validate(transformer.transform(input)) | ||
} | ||
} | ||
|
||
/** | ||
* Run this validator after first transforming the data. | ||
* | ||
* This is useful when needing to use a validator for a different type than | ||
* the necessary input type. | ||
*/ | ||
fun <INPUT, VALIDATION> DataValidator<VALIDATION>.transformedWith( | ||
transformer: DataTransformer<INPUT, VALIDATION> | ||
): DataValidator<INPUT> { | ||
return TransformedValidator(transformer, this) | ||
} |
32 changes: 32 additions & 0 deletions
32
data/src/commonTest/kotlin/com/inkapplications/data/validator/TransformedValidatorTest.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,32 @@ | ||
package com.inkapplications.data.validator | ||
|
||
import com.inkapplications.data.transformer.DataTransformer | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.* | ||
|
||
class TransformedValidatorTest { | ||
@Test | ||
fun transforms() = runTest { | ||
val validationError = object: ValidationError( | ||
message = "test-message", | ||
) {} | ||
var validationInput: String? = null | ||
val spyValidator = object: DataValidator<String> { | ||
override suspend fun validate(input: String): ValidationResult { | ||
validationInput = input | ||
return ValidationResult.Failed(validationError) | ||
} | ||
} | ||
val transformer = object: DataTransformer<Int, String> { | ||
override fun transform(data: Int): String = "test-transformation" | ||
override fun reverseTransform(data: String): Int = TODO() | ||
} | ||
|
||
val testValidator = spyValidator.transformedWith(transformer) | ||
val result = testValidator.validate(5) | ||
|
||
assertTrue(result is ValidationResult.Failed, "Result from validator is used") | ||
assertSame(validationError, result.reasons.single(), "Exact result from validator is used") | ||
assertEquals("test-transformation", validationInput, "Input is transformed") | ||
} | ||
} |