Skip to content

Commit

Permalink
Merge pull request #39 from ReneeVandervelde/data-module
Browse files Browse the repository at this point in the history
Add Transformed validator functions
  • Loading branch information
ReneeVandervelde authored Mar 30, 2024
2 parents 36f1f12 + 6c274b6 commit 932335e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions data/api/data.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down
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)
}
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")
}
}

0 comments on commit 932335e

Please sign in to comment.