-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae31c2c
commit 508dde1
Showing
8 changed files
with
169 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
package io.konform.validation | ||
|
||
import io.konform.validation.types.EmptyValidation | ||
import io.konform.validation.types.ValidateAll | ||
|
||
public interface Validation<in T> { | ||
public companion object { | ||
public operator fun <T> invoke(init: ValidationBuilder<T>.() -> Unit): Validation<T> { | ||
val builder = ValidationBuilder<T>() | ||
return builder.apply(init).build() | ||
} | ||
public operator fun <T> invoke(init: ValidationBuilder<T>.() -> Unit): Validation<T> = ValidationBuilder.buildWithNew(init) | ||
} | ||
|
||
public fun validate(value: T): ValidationResult<@UnsafeVariance T> | ||
|
||
public operator fun invoke(value: T): ValidationResult<@UnsafeVariance T> = validate(value) | ||
} | ||
|
||
/** Combine a [List] of [Validation]s into a single one that returns all validation errors. */ | ||
public fun <T> List<Validation<T>>.flatten(): Validation<T> = | ||
when (size) { | ||
0 -> EmptyValidation | ||
1 -> first() | ||
else -> ValidateAll(this) | ||
} |
13 changes: 13 additions & 0 deletions
13
src/commonMain/kotlin/io/konform/validation/ValidationError.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,13 @@ | ||
package io.konform.validation | ||
|
||
public interface ValidationError { | ||
public val dataPath: String | ||
public val message: String | ||
|
||
public companion object { | ||
internal operator fun invoke( | ||
dataPath: String, | ||
message: String, | ||
): ValidationError = PropertyValidationError(dataPath, message) | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/commonMain/kotlin/io/konform/validation/path/ValidationPath.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,13 @@ | ||
package io.konform.validation.path | ||
|
||
import io.konform.validation.kotlin.Path | ||
|
||
/** Represents a path to a validation. */ | ||
public data class ValidationPath( | ||
// val segments: List<PathSegment>, | ||
val dataPaths: List<String>, | ||
) { | ||
public companion object { | ||
public fun fromAny(vararg validationPath: Any): ValidationPath = ValidationPath(validationPath.map { Path.toPath(*validationPath) }) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/commonMain/kotlin/io/konform/validation/types/EmptyValidation.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,16 @@ | ||
package io.konform.validation.types | ||
|
||
import io.konform.validation.Valid | ||
import io.konform.validation.Validation | ||
import io.konform.validation.ValidationResult | ||
|
||
/** Validation that always returns [Valid]. `unit` in monadic terms. */ | ||
public object EmptyValidation : Validation<Any?> { | ||
override fun validate(value: Any?): ValidationResult<Any?> = Valid(value) | ||
|
||
override fun toString(): String = "EmptyValidation" | ||
|
||
override fun equals(other: Any?): Boolean = other === this | ||
|
||
override fun hashCode(): Int = 912378 | ||
} |
22 changes: 22 additions & 0 deletions
22
src/commonMain/kotlin/io/konform/validation/types/ValidateAll.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,22 @@ | ||
package io.konform.validation.types | ||
|
||
import io.konform.validation.Invalid | ||
import io.konform.validation.Validation | ||
import io.konform.validation.ValidationResult | ||
import io.konform.validation.flattenOrValid | ||
|
||
/** Validation that runs multiple validations in sequence. */ | ||
public class ValidateAll<T>( | ||
private val validations: List<Validation<T>>, | ||
) : Validation<T> { | ||
override fun validate(value: T): ValidationResult<T> { | ||
val errors = mutableListOf<Invalid>() | ||
for (validation in validations) { | ||
val result = validation.validate(value) | ||
if (result is Invalid) errors += result | ||
} | ||
return errors.flattenOrValid(value) | ||
} | ||
|
||
override fun toString(): String = "ValidateAll(validation=$validations)" | ||
} |
55 changes: 55 additions & 0 deletions
55
src/commonTest/kotlin/io/konform/validation/ListValidationTest.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,55 @@ | ||
package io.konform.validation | ||
|
||
import io.konform.validation.jsonschema.minimum | ||
import io.konform.validation.types.EmptyValidation | ||
import io.konform.validation.types.ValidateAll | ||
import io.kotest.assertions.konform.shouldBeInvalid | ||
import io.kotest.assertions.konform.shouldBeValid | ||
import io.kotest.assertions.konform.shouldContainExactlyErrors | ||
import io.kotest.assertions.konform.shouldContainOnlyError | ||
import io.kotest.matchers.types.shouldBeInstanceOf | ||
import io.kotest.matchers.types.shouldBeSameInstanceAs | ||
import kotlin.test.Test | ||
|
||
class ListValidationTest { | ||
private val validation1 = | ||
Validation<Int> { | ||
minimum(0) | ||
} | ||
private val validation2 = | ||
Validation<Int> { | ||
minimum(10) | ||
} | ||
|
||
@Test | ||
fun flattenNone() { | ||
val result = listOf<Validation<Int>>().flatten() | ||
|
||
result shouldBeSameInstanceAs EmptyValidation | ||
result shouldBeValid 0 | ||
result shouldBeValid -100 | ||
} | ||
|
||
@Test | ||
fun flattenOne() { | ||
val result = listOf(validation1).flatten() | ||
|
||
result shouldBeSameInstanceAs validation1 | ||
result shouldBeValid 0 | ||
result shouldBeInvalid -100 | ||
} | ||
|
||
@Test | ||
fun flattenMore() { | ||
val result = listOf(validation1, validation2).flatten() | ||
|
||
result.shouldBeInstanceOf<ValidateAll<Int>>() | ||
|
||
result shouldBeValid 10 | ||
(result shouldBeInvalid 5) shouldContainOnlyError ValidationError("", "must be at least '10'") | ||
(result shouldBeInvalid -1).shouldContainExactlyErrors( | ||
ValidationError("", "must be at least '0'"), | ||
ValidationError("", "must be at least '10'"), | ||
) | ||
} | ||
} |
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