-
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.
Open up ValidationBuilder for extension (#170)
- Loading branch information
1 parent
6ec40a4
commit 15b871b
Showing
4 changed files
with
66 additions
and
6 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
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
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
53 changes: 53 additions & 0 deletions
53
...mmonTest/kotlin/io/konform/validation/validationbuilder/ExtendingValidationBuilderTest.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,53 @@ | ||
package io.konform.validation.validationbuilder | ||
|
||
import io.konform.validation.Validation | ||
import io.konform.validation.ValidationBuilder | ||
import io.konform.validation.ValidationError | ||
import io.konform.validation.constraints.minLength | ||
import io.konform.validation.path.ValidationPath | ||
import io.kotest.assertions.konform.shouldBeInvalid | ||
import io.kotest.assertions.konform.shouldBeValid | ||
import io.kotest.assertions.konform.shouldContainExactlyErrors | ||
import kotlin.reflect.KProperty1 | ||
import kotlin.test.Test | ||
|
||
class ExtendingValidationBuilderTest { | ||
@Test | ||
fun allowsExtending() { | ||
val validation = | ||
MyValidationBuilder<User> { | ||
User::name trimmed { | ||
minLength(1) | ||
} | ||
} | ||
|
||
validation shouldBeValid User("John") | ||
(validation shouldBeInvalid User("")).shouldContainExactlyErrors( | ||
ValidationError.of(User::name, "must have at least 1 characters"), | ||
) | ||
(validation shouldBeInvalid User("\t")).shouldContainExactlyErrors( | ||
ValidationError.of(User::name, "must have at least 1 characters"), | ||
) | ||
} | ||
} | ||
|
||
data class User( | ||
val name: String, | ||
) | ||
|
||
class MyValidationBuilder<T> : ValidationBuilder<T>() { | ||
infix fun KProperty1<T, String>.trimmed(init: ValidationBuilder<String>.() -> Unit): Unit = | ||
this { | ||
validate(ValidationPath.EMPTY, { it.trim() }) { | ||
run(buildWithNew(init)) | ||
} | ||
} | ||
|
||
companion object { | ||
operator fun <T> invoke(init: MyValidationBuilder<T>.() -> Unit): Validation<T> { | ||
val builder = MyValidationBuilder<T>() | ||
init(builder) | ||
return builder.build() | ||
} | ||
} | ||
} |