-
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.
Allow customizing hint in required (#172)
- Loading branch information
1 parent
e151f6c
commit ae36b18
Showing
8 changed files
with
171 additions
and
53 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 |
---|---|---|
|
@@ -216,6 +216,8 @@ val validateEvent = Validation<Event> { | |
Event::organizer { | ||
// even though the email is nullable you can force it to be set in the validation | ||
Person::email required { | ||
// Optionally set a hint, default hint is "is required" | ||
hint = "Email address must be given" | ||
pattern("[email protected]") hint "Organizers must have a BigCorp email address" | ||
} | ||
} | ||
|
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
32 changes: 32 additions & 0 deletions
32
src/commonMain/kotlin/io/konform/validation/builders/RequiredValidationBuilder.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,32 @@ | ||
package io.konform.validation.builders | ||
|
||
import io.konform.validation.ValidationBuilder | ||
import io.konform.validation.types.RequireNotNullValidation | ||
import io.konform.validation.types.RequireNotNullValidation.Companion.DEFAULT_REQUIRED_HINT | ||
|
||
/** | ||
* A [ValidationBuilder] for [RequireNotNullValidation]. | ||
* Allows setting the hint for the validation when the value is null with the following syntax: | ||
* ``` | ||
* User::name required { | ||
* hint = "Please fill in your name" | ||
* // other validations on name as normal | ||
* } | ||
* ``` | ||
*/ | ||
public class RequiredValidationBuilder<T : Any> : ValidationBuilder<T>() { | ||
public var hint: String = DEFAULT_REQUIRED_HINT | ||
|
||
override fun build(): RequireNotNullValidation<T> { | ||
val subValidation = super.build() | ||
return RequireNotNullValidation(hint, subValidation) | ||
} | ||
|
||
public companion object { | ||
public inline fun <T : Any> buildWithNew(block: RequiredValidationBuilder<T>.() -> Unit): RequireNotNullValidation<T> { | ||
val builder = RequiredValidationBuilder<T>() | ||
block(builder) | ||
return builder.build() | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,21 +111,6 @@ class ValidationBuilderTest { | |
Register(referredBy = "poweruser@").let { assertEquals(1, countErrors(nullableFieldValidation(it), Register::referredBy)) } | ||
} | ||
|
||
@Test | ||
fun validatingRequiredFields() { | ||
val nullableFieldValidation = | ||
Validation<Register> { | ||
Register::referredBy required { | ||
pattern(".+@.+".toRegex()).hint("must have correct format") | ||
} | ||
} | ||
|
||
Register(referredBy = "[email protected]").let { assertEquals(Valid(it), nullableFieldValidation(it)) } | ||
|
||
Register(referredBy = null).let { assertEquals(1, countErrors(nullableFieldValidation(it), Register::referredBy)) } | ||
Register(referredBy = "poweruser@").let { assertEquals(1, countErrors(nullableFieldValidation(it), Register::referredBy)) } | ||
} | ||
|
||
@Test | ||
fun validatingNestedTypesDirectly() { | ||
val nestedTypeValidation = | ||
|
@@ -155,21 +140,6 @@ class ValidationBuilderTest { | |
"poweruser@".let { assertEquals(1, countErrors(nullableTypeValidation(it))) } | ||
} | ||
|
||
@Test | ||
fun validatingRequiredNullableValues() { | ||
val nullableRequiredValidation = | ||
Validation<String?> { | ||
required { | ||
pattern(".+@.+".toRegex()).hint("must have correct format") | ||
} | ||
} | ||
|
||
"[email protected]".let { assertEquals(Valid(it), nullableRequiredValidation(it)) } | ||
|
||
null.let { assertEquals(1, countErrors(nullableRequiredValidation(it))) } | ||
"poweruser@".let { assertEquals(1, countErrors(nullableRequiredValidation(it))) } | ||
} | ||
|
||
@Test | ||
fun functionAccessorSyntax() { | ||
val splitDoubleValidation = | ||
|
80 changes: 80 additions & 0 deletions
80
src/commonTest/kotlin/io/konform/validation/validationbuilder/RequiredTest.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,80 @@ | ||
package io.konform.validation.validationbuilder | ||
|
||
import io.konform.validation.Valid | ||
import io.konform.validation.Validation | ||
import io.konform.validation.ValidationError | ||
import io.konform.validation.constraints.minLength | ||
import io.konform.validation.constraints.pattern | ||
import io.konform.validation.countErrors | ||
import io.konform.validation.required | ||
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 | ||
|
||
class RequiredTest { | ||
@Test | ||
fun validatingRequiredFields() { | ||
val nullableFieldValidation = | ||
Validation<Register> { | ||
Register::referredBy required { | ||
pattern(".+@.+".toRegex()).hint("must have correct format") | ||
} | ||
} | ||
|
||
Register("[email protected]").let { assertEquals(Valid(it), nullableFieldValidation(it)) } | ||
|
||
Register(null).let { assertEquals(1, countErrors(nullableFieldValidation(it), Register::referredBy)) } | ||
Register("poweruser@").let { assertEquals(1, countErrors(nullableFieldValidation(it), Register::referredBy)) } | ||
} | ||
|
||
@Test | ||
fun setRequiredHint() { | ||
val validation = | ||
Validation<Register> { | ||
Register::referredBy required { | ||
hint = "a referral is required" | ||
} | ||
} | ||
|
||
(validation shouldBeInvalid Register(null)) shouldContainOnlyError | ||
ValidationError.of(Register::referredBy, "a referral is required") | ||
} | ||
|
||
@Test | ||
fun validatingRequiredNullableValues() { | ||
val nullableRequiredValidation = | ||
Validation<String?> { | ||
required { | ||
pattern(".+@.+".toRegex()).hint("must have correct format") | ||
} | ||
} | ||
|
||
"[email protected]".let { assertEquals(Valid(it), nullableRequiredValidation(it)) } | ||
|
||
null.let { assertEquals(1, countErrors(nullableRequiredValidation(it))) } | ||
"poweruser@".let { assertEquals(1, countErrors(nullableRequiredValidation(it))) } | ||
} | ||
|
||
@Test | ||
fun requiredFunction() { | ||
val validation = | ||
Validation<String?> { | ||
required("trimmed", { it?.trim() }) { | ||
hint = "string must be present" | ||
minLength(2) | ||
} | ||
} | ||
|
||
validation shouldBeValid "abc" | ||
(validation shouldBeInvalid null) shouldContainOnlyError | ||
ValidationError.of("trimmed", "string must be present") | ||
(validation shouldBeInvalid " a") shouldContainOnlyError | ||
ValidationError.of("trimmed", "must have at least 2 characters") | ||
} | ||
|
||
private data class Register( | ||
val referredBy: String? = null, | ||
) | ||
} |