diff --git a/src/commonMain/kotlin/io/konform/validation/ValidationResult.kt b/src/commonMain/kotlin/io/konform/validation/ValidationResult.kt index 6100ea8..4e3d477 100644 --- a/src/commonMain/kotlin/io/konform/validation/ValidationResult.kt +++ b/src/commonMain/kotlin/io/konform/validation/ValidationResult.kt @@ -31,6 +31,15 @@ public sealed class ValidationResult { } public abstract val errors: List + + /** + * Returns true if the [ValidationResult] is [Valid]. + */ + public val isValid: Boolean = + when (this) { + is Invalid -> false + is Valid -> true + } } public data class Invalid( diff --git a/src/commonTest/kotlin/io/konform/validation/ValidationResultTest.kt b/src/commonTest/kotlin/io/konform/validation/ValidationResultTest.kt index 64e0913..f6fb1b0 100644 --- a/src/commonTest/kotlin/io/konform/validation/ValidationResultTest.kt +++ b/src/commonTest/kotlin/io/konform/validation/ValidationResultTest.kt @@ -5,28 +5,33 @@ import io.konform.validation.jsonschema.minLength import io.konform.validation.jsonschema.pattern import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue class ValidationResultTest { - @Test - fun singleValidation() { - val validation = - Validation { - Person::name { - minLength(1) - } + private val validation = + Validation { + Person::name { + minLength(1) + } - Person::addresses onEach { - Address::city { - City::postalCode { - minLength(4) - maxLength(5) - pattern("\\d{4,5}") hint ("must be a four or five digit number") - } + Person::addresses onEach { + Address::city { + City::postalCode { + minLength(4) + maxLength(5) + pattern("\\d{4,5}") hint ("must be a four or five digit number") } } } + } + @Test + fun singleValidation() { val result = validation(Person("", addresses = listOf(Address(City("", ""))))) + + assertFalse(result.isValid) + assertEquals(3, result.errors.size) val (firstError, secondError, thirdError) = result.errors @@ -40,6 +45,19 @@ class ValidationResultTest { assertEquals("must be a four or five digit number", thirdError.message) } + @Test + fun positiveValidation() { + val result = + validation( + Person( + name = "Jane Doe", + addresses = listOf(Address(City("10115", "Berlin"))), + ), + ) + + assertTrue(result.isValid) + } + private data class Person(val name: String, val addresses: List
) private data class Address(val city: City)