-
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
15b871b
commit e151f6c
Showing
10 changed files
with
357 additions
and
50 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
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
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
36 changes: 36 additions & 0 deletions
36
src/commonMain/kotlin/io/konform/validation/types/DynamicValidation.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,36 @@ | ||
package io.konform.validation.types | ||
|
||
import io.konform.validation.Invalid | ||
import io.konform.validation.Valid | ||
import io.konform.validation.Validation | ||
import io.konform.validation.ValidationBuilder | ||
import io.konform.validation.ValidationResult | ||
import io.konform.validation.path.ValidationPath | ||
|
||
internal class DynamicValidation<T>( | ||
private val creator: (T) -> Validation<T>, | ||
) : Validation<T> { | ||
override fun validate(value: T): ValidationResult<T> { | ||
val validation = creator(value) | ||
return validation.validate(value) | ||
} | ||
} | ||
|
||
internal class DynamicCallableValidation<T, R>( | ||
private val path: ValidationPath, | ||
private val callable: (T) -> R, | ||
private val builder: ValidationBuilder<R>.(T) -> Unit, | ||
) : Validation<T> { | ||
override fun validate(value: T): ValidationResult<T> { | ||
val validation = | ||
ValidationBuilder<R>() | ||
.also { | ||
builder(it, value) | ||
}.build() | ||
val toValidate = callable(value) | ||
return when (val callableResult = validation(toValidate)) { | ||
is Valid -> Valid(value) | ||
is Invalid -> callableResult.prependPath(path) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -23,6 +23,12 @@ import io.konform.validation.constraints.type | |
import io.konform.validation.constraints.uniqueItems | ||
import io.konform.validation.constraints.uuid | ||
import io.konform.validation.countFieldsWithErrors | ||
import io.konform.validation.path.ValidationPath | ||
import io.kotest.assertions.konform.shouldBeInvalid | ||
import io.kotest.assertions.konform.shouldBeValid | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldContain | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
@@ -176,7 +182,10 @@ class ConstraintsTest { | |
assertEquals(1, countFieldsWithErrors(validation(10.00001))) | ||
assertEquals(1, countFieldsWithErrors(validation(11))) | ||
assertEquals(1, countFieldsWithErrors(validation(Double.POSITIVE_INFINITY))) | ||
assertEquals(1, countFieldsWithErrors(Validation<Number> { exclusiveMaximum(Double.POSITIVE_INFINITY) }(Double.POSITIVE_INFINITY))) | ||
assertEquals( | ||
1, | ||
countFieldsWithErrors(Validation<Number> { exclusiveMaximum(Double.POSITIVE_INFINITY) }(Double.POSITIVE_INFINITY)), | ||
) | ||
|
||
assertEquals("must be less than '10'", validation(11).get()[0]) | ||
} | ||
|
@@ -219,7 +228,10 @@ class ConstraintsTest { | |
assertEquals(1, countFieldsWithErrors(validation(9.99999999999))) | ||
assertEquals(1, countFieldsWithErrors(validation(8))) | ||
assertEquals(1, countFieldsWithErrors(validation(Double.NEGATIVE_INFINITY))) | ||
assertEquals(1, countFieldsWithErrors(Validation<Number> { exclusiveMinimum(Double.NEGATIVE_INFINITY) }(Double.NEGATIVE_INFINITY))) | ||
assertEquals( | ||
1, | ||
countFieldsWithErrors(Validation<Number> { exclusiveMinimum(Double.NEGATIVE_INFINITY) }(Double.NEGATIVE_INFINITY)), | ||
) | ||
|
||
assertEquals("must be greater than '10'", validation(9).get()[0]) | ||
} | ||
|
@@ -254,24 +266,26 @@ class ConstraintsTest { | |
fun patternConstraint() { | ||
val validation = Validation<String> { pattern(".+@.+") } | ||
|
||
assertEquals(Valid("a@a"), validation("a@a")) | ||
assertEquals(Valid("a@a@a@a"), validation("a@a@a@a")) | ||
assertEquals(Valid(" a@a "), validation(" a@a ")) | ||
validation shouldBeValid "a@a" | ||
validation shouldBeValid "a@a@a@a" | ||
validation shouldBeValid " a@a " | ||
|
||
assertEquals(1, countFieldsWithErrors(validation("a"))) | ||
assertEquals("must match the expected pattern", validation("").get()[0]) | ||
val invalid = validation shouldBeInvalid "a" | ||
invalid.errors shouldHaveSize 1 | ||
invalid.errors[0].path shouldBe ValidationPath.EMPTY | ||
invalid.errors[0].message shouldContain "must match pattern '" | ||
|
||
val compiledRegexValidation = | ||
Validation<String> { | ||
pattern("^\\w+@\\w+\\.\\w+$".toRegex()) | ||
} | ||
|
||
assertEquals(Valid("[email protected]"), compiledRegexValidation("[email protected]")) | ||
assertEquals(1, countFieldsWithErrors(compiledRegexValidation("tester@example"))) | ||
assertEquals(1, countFieldsWithErrors(compiledRegexValidation(" [email protected]"))) | ||
assertEquals(1, countFieldsWithErrors(compiledRegexValidation("[email protected] "))) | ||
|
||
assertEquals("must match the expected pattern", compiledRegexValidation("").get()[0]) | ||
compiledRegexValidation shouldBeValid "[email protected]" | ||
val invalidComplex = (compiledRegexValidation shouldBeInvalid "tester@example") | ||
invalidComplex.errors shouldHaveSize 1 | ||
invalidComplex.errors[0].message shouldContain "must match pattern '" | ||
compiledRegexValidation shouldBeInvalid " [email protected]" | ||
compiledRegexValidation shouldBeInvalid "[email protected] " | ||
} | ||
|
||
@Test | ||
|
Oops, something went wrong.