-
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.
Implement constraints using
Comparable
interface
Allow building the constraints - `maximum` and `exclusiveMaximum`, - `minimum` and `exclusiveMinimum` for any type `T`, using a `Comparable<T>`. Previously, these constraints were only available for subtypes of `Number` and relied on converting to `Double` for comparing numbers of different types. This could cause issues when comparing values which cannot be cleanly converted from the original number type. For compatibility and convenience, we provide overloads for these constraints that accept an `Int` as the limit value as this seems like a rather common case. Closes #180
- Loading branch information
1 parent
f3fc6d3
commit 24eb7d6
Showing
4 changed files
with
229 additions
and
80 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/commonMain/kotlin/io/konform/validation/constraints/ComparableConstraints.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,16 @@ | ||
package io.konform.validation.constraints | ||
|
||
import io.konform.validation.Constraint | ||
import io.konform.validation.ValidationBuilder | ||
|
||
public fun <T : Any> ValidationBuilder<T>.maximum(maximumInclusive: Comparable<T>): Constraint<T> = | ||
constrain("must be at most '$maximumInclusive'") { maximumInclusive >= it } | ||
|
||
public fun <T : Any> ValidationBuilder<T>.exclusiveMaximum(maximumExclusive: Comparable<T>): Constraint<T> = | ||
constrain("must be less than '$maximumExclusive'") { maximumExclusive > it } | ||
|
||
public fun <T : Any> ValidationBuilder<T>.minimum(minimumInclusive: Comparable<T>): Constraint<T> = | ||
constrain("must be at least '$minimumInclusive'") { minimumInclusive <= it } | ||
|
||
public fun <T : Any> ValidationBuilder<T>.exclusiveMinimum(minimumExclusive: Comparable<T>): Constraint<T> = | ||
constrain("must be greater than '$minimumExclusive'") { minimumExclusive < it } |
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
Oops, something went wrong.