Skip to content

Commit

Permalink
Fix pattern test
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoepelman committed Nov 15, 2024
1 parent 9157424 commit 5d93d84
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public data class ValidationError(
public fun prependPath(pathSegment: PathSegment): ValidationError = mapPath { it.prepend(pathSegment) }

internal companion object {
internal fun of(
public fun of(
pathSegment: Any,
message: String,
): ValidationError = ValidationError(ValidationPath.of(pathSegment), message)

internal fun ofEmptyPath(message: String): ValidationError = ValidationError(ValidationPath.EMPTY, message)
public fun ofEmptyPath(message: String): ValidationError = ValidationError(ValidationPath.EMPTY, message)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.konform.validation.constraints

import io.konform.validation.Valid
import io.konform.validation.Validation
import io.konform.validation.ValidationError
import io.konform.validation.ValidationResult
import io.konform.validation.constraints.ConstraintsTest.TCPPacket.ACK
import io.konform.validation.constraints.ConstraintsTest.TCPPacket.SYN
Expand All @@ -23,6 +24,10 @@ 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.assertions.konform.shouldContainOnlyError
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand Down Expand Up @@ -176,7 +181,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])
}
Expand Down Expand Up @@ -219,7 +227,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])
}
Expand Down Expand Up @@ -254,24 +265,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])
(validation shouldBeInvalid "a") shouldContainOnlyError
ValidationError.of(
ValidationPath.EMPTY,
"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])
val expectedError = ValidationError.ofEmptyPath("must match pattern '^\\w+@\\w+\\.\\w+\$'")
compiledRegexValidation shouldBeValid "[email protected]"
(compiledRegexValidation shouldBeInvalid "tester@example") shouldContainOnlyError expectedError
(compiledRegexValidation shouldBeInvalid " [email protected]") shouldContainOnlyError expectedError
(compiledRegexValidation shouldBeInvalid "[email protected] ") shouldContainOnlyError expectedError
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import io.konform.validation.Validation
import io.konform.validation.ValidationBuilder
import io.konform.validation.ValidationError
import io.konform.validation.constraints.pattern
import io.konform.validation.path.ValidationPath
import io.konform.validation.types.AlwaysInvalidValidation
import io.konform.validation.types.EmptyValidation
import io.kotest.assertions.konform.shouldBeInvalid
import io.kotest.assertions.konform.shouldBeValid
import io.kotest.assertions.konform.shouldContainOnlyError
Expand Down Expand Up @@ -88,6 +91,27 @@ class DynamicValidationTest {
"must be larger than from",
)
}

@Test
fun runDynamic() {
val validation =
Validation<String> {
runDynamic {
if (it == "a") {
AlwaysInvalidValidation
} else {
EmptyValidation
}
}
}

validation shouldBeValid "b"
(validation shouldBeInvalid "a") shouldContainOnlyError
ValidationError(
ValidationPath.EMPTY,
"always invalid",
)
}
}

data class Address(
Expand Down

0 comments on commit 5d93d84

Please sign in to comment.