diff --git a/data/api/data.api b/data/api/data.api index 45eaad1..0038fb8 100644 --- a/data/api/data.api +++ b/data/api/data.api @@ -400,6 +400,10 @@ public final class com/inkapplications/data/validator/PassingValidator : com/ink public fun validate (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } +public final class com/inkapplications/data/validator/TransformedValidatorKt { + public static final fun transformedWith (Lcom/inkapplications/data/validator/DataValidator;Lcom/inkapplications/data/transformer/DataTransformer;)Lcom/inkapplications/data/validator/DataValidator; +} + public abstract class com/inkapplications/data/validator/ValidationError : java/lang/Error { public fun (Ljava/lang/String;Ljava/lang/Throwable;)V public synthetic fun (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V diff --git a/data/src/commonMain/kotlin/com/inkapplications/data/validator/TransformedValidator.kt b/data/src/commonMain/kotlin/com/inkapplications/data/validator/TransformedValidator.kt new file mode 100644 index 0000000..7c94251 --- /dev/null +++ b/data/src/commonMain/kotlin/com/inkapplications/data/validator/TransformedValidator.kt @@ -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( + private val transformer: DataTransformer, + private val validator: DataValidator, +): DataValidator { + 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 DataValidator.transformedWith( + transformer: DataTransformer +): DataValidator { + return TransformedValidator(transformer, this) +} diff --git a/data/src/commonTest/kotlin/com/inkapplications/data/validator/TransformedValidatorTest.kt b/data/src/commonTest/kotlin/com/inkapplications/data/validator/TransformedValidatorTest.kt new file mode 100644 index 0000000..9c1b044 --- /dev/null +++ b/data/src/commonTest/kotlin/com/inkapplications/data/validator/TransformedValidatorTest.kt @@ -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 { + override suspend fun validate(input: String): ValidationResult { + validationInput = input + return ValidationResult.Failed(validationError) + } + } + val transformer = object: DataTransformer { + 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") + } +}