-
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.
Give deprecation warning if ifPresent or required are used on non-nul…
…lable properties (#173)
- Loading branch information
1 parent
ae36b18
commit e953442
Showing
5 changed files
with
124 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,20 +97,6 @@ class ValidationBuilderTest { | |
) | ||
} | ||
|
||
@Test | ||
fun validatingNullableFields() { | ||
val nullableFieldValidation = | ||
Validation<Register> { | ||
Register::referredBy ifPresent { | ||
pattern(".+@.+".toRegex()).hint("must have correct format") | ||
} | ||
} | ||
|
||
Register(referredBy = null).let { assertEquals(Valid(it), nullableFieldValidation(it)) } | ||
Register(referredBy = "[email protected]").let { assertEquals(Valid(it), nullableFieldValidation(it)) } | ||
Register(referredBy = "poweruser@").let { assertEquals(1, countErrors(nullableFieldValidation(it), Register::referredBy)) } | ||
} | ||
|
||
@Test | ||
fun validatingNestedTypesDirectly() { | ||
val nestedTypeValidation = | ||
|
45 changes: 45 additions & 0 deletions
45
src/commonTest/kotlin/io/konform/validation/validationbuilder/IfPresentTest.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,45 @@ | ||
package io.konform.validation.validationbuilder | ||
|
||
import io.konform.validation.Valid | ||
import io.konform.validation.Validation | ||
import io.konform.validation.constraints.pattern | ||
import io.konform.validation.countErrors | ||
import io.kotest.assertions.konform.shouldBeValid | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class IfPresentTest { | ||
@Test | ||
fun validatingNullableFields() { | ||
val nullableFieldValidation = | ||
Validation<Register> { | ||
Register::referredBy ifPresent { | ||
pattern(".+@.+".toRegex()).hint("must have correct format") | ||
} | ||
} | ||
|
||
Register(referredBy = null).let { assertEquals(Valid(it), nullableFieldValidation(it)) } | ||
Register(referredBy = "[email protected]").let { assertEquals(Valid(it), nullableFieldValidation(it)) } | ||
Register(referredBy = "poweruser@").let { assertEquals(1, countErrors(nullableFieldValidation(it), Register::referredBy)) } | ||
} | ||
|
||
@Test | ||
fun deprecationWhenPropertyIsNotNull() { | ||
val validation = | ||
Validation<Foo> { | ||
// This itself will give a warning if no deprecation is suppressed | ||
@Suppress("DEPRECATION") | ||
Foo::bar ifPresent {} | ||
} | ||
|
||
validation shouldBeValid Foo("") | ||
} | ||
|
||
private data class Register( | ||
val referredBy: String? = null, | ||
) | ||
|
||
private data class Foo( | ||
val bar: String, | ||
) | ||
} |
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