-
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.
Move io.konform.validation.jsonschema to io.konform.validation.const…
…raints (#165)
- Loading branch information
1 parent
7fb9b8e
commit a2dff2b
Showing
14 changed files
with
384 additions
and
136 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
7 changes: 7 additions & 0 deletions
7
src/commonMain/kotlin/io/konform/validation/constraints/AnyConstraints.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,7 @@ | ||
package io.konform.validation.constraints | ||
|
||
import io.konform.validation.Constraint | ||
import io.konform.validation.ValidationBuilder | ||
|
||
public inline fun <reified T> ValidationBuilder<*>.type(): Constraint<*> = | ||
addConstraint("must be of type '${T::class.simpleName}'") { it is T } |
22 changes: 22 additions & 0 deletions
22
src/commonMain/kotlin/io/konform/validation/constraints/EnumConstraints.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,22 @@ | ||
package io.konform.validation.constraints | ||
|
||
import io.konform.validation.Constraint | ||
import io.konform.validation.ValidationBuilder | ||
|
||
/** Restrict a value to a set of allowed values. */ | ||
public fun <T> ValidationBuilder<T>.enum(vararg allowed: T): Constraint<T> { | ||
val set = allowed.toSet() | ||
return addConstraint("must be one of: ${set.joinToString("', '", "'", "'")}") { | ||
it in allowed | ||
} | ||
} | ||
|
||
/** Restrict a [String] to the entry names of an [Enum]. */ | ||
public inline fun <reified T : Enum<T>> ValidationBuilder<String>.enum(): Constraint<String> { | ||
val enumNames = enumValues<T>().mapTo(mutableSetOf()) { it.name } | ||
return addConstraint("must be one of: ${enumNames.joinToString("', '", "'", "'")}") { | ||
it in enumNames | ||
} | ||
} | ||
|
||
public fun <T> ValidationBuilder<T>.const(expected: T): Constraint<T> = addConstraint("must be '$expected'") { expected == it } |
34 changes: 34 additions & 0 deletions
34
src/commonMain/kotlin/io/konform/validation/constraints/IterableConstraints.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,34 @@ | ||
package io.konform.validation.constraints | ||
|
||
import io.konform.validation.Constraint | ||
import io.konform.validation.ValidationBuilder | ||
import kotlin.jvm.JvmName | ||
|
||
public fun <T : Iterable<*>> ValidationBuilder<T>.minItems(minSize: Int): Constraint<T> = | ||
addConstraint("must have at least {0} items", minSize.toString()) { it.count() >= minSize } | ||
|
||
@JvmName("arrayMinItems") | ||
public fun <T> ValidationBuilder<Array<T>>.minItems(minSize: Int): Constraint<Array<T>> = | ||
addConstraint("must have at least {0} items", minSize.toString()) { it.count() >= minSize } | ||
|
||
public fun <T : Iterable<*>> ValidationBuilder<T>.maxItems(maxSize: Int): Constraint<T> = | ||
addConstraint("must have at most {0} items", maxSize.toString()) { | ||
it.count() <= maxSize | ||
} | ||
|
||
@JvmName("arrayMaxItems") | ||
public fun <T> ValidationBuilder<Array<T>>.maxItems(maxSize: Int): Constraint<Array<T>> = | ||
addConstraint("must have at most {0} items", maxSize.toString()) { | ||
it.count() <= maxSize | ||
} | ||
|
||
public fun <T : Iterable<*>> ValidationBuilder<T>.uniqueItems(unique: Boolean = true): Constraint<T> = | ||
addConstraint("all items must be unique") { | ||
!unique || it.distinct().count() == it.count() | ||
} | ||
|
||
@JvmName("arrayUniqueItems") | ||
public fun <T> ValidationBuilder<Array<T>>.uniqueItems(unique: Boolean = true): Constraint<Array<T>> = | ||
addConstraint("all items must be unique") { | ||
!unique || it.distinct().count() == it.count() | ||
} |
25 changes: 25 additions & 0 deletions
25
src/commonMain/kotlin/io/konform/validation/constraints/MapConstraints.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,25 @@ | ||
package io.konform.validation.constraints | ||
|
||
import io.konform.validation.Constraint | ||
import io.konform.validation.ValidationBuilder | ||
|
||
public fun <T : Map<*, *>> ValidationBuilder<T>.minItems(minSize: Int): Constraint<T> = | ||
addConstraint("must have at least {0} items", minSize.toString()) { | ||
it.count() >= minSize | ||
} | ||
|
||
public fun <T : Map<*, *>> ValidationBuilder<T>.maxItems(maxSize: Int): Constraint<T> = | ||
addConstraint("must have at most {0} items", maxSize.toString()) { | ||
it.count() <= maxSize | ||
} | ||
|
||
public fun <T : Map<*, *>> ValidationBuilder<T>.minProperties(minSize: Int): Constraint<T> = | ||
minItems(minSize) hint "must have at least {0} properties" | ||
|
||
public fun <T : Map<*, *>> ValidationBuilder<T>.maxProperties(maxSize: Int): Constraint<T> = | ||
maxItems(maxSize) hint "must have at most {0} properties" | ||
|
||
public fun <T : Map<*, *>> ValidationBuilder<T>.uniqueItems(unique: Boolean = true): Constraint<T> = | ||
addConstraint("all items must be unique") { | ||
!unique || it.values.distinct().count() == it.count() | ||
} |
38 changes: 38 additions & 0 deletions
38
src/commonMain/kotlin/io/konform/validation/constraints/NumberConstraints.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,38 @@ | ||
package io.konform.validation.constraints | ||
|
||
import io.konform.validation.Constraint | ||
import io.konform.validation.ValidationBuilder | ||
import kotlin.math.roundToInt | ||
|
||
public fun <T : Number> ValidationBuilder<T>.multipleOf(factor: Number): Constraint<T> { | ||
val factorAsDouble = factor.toDouble() | ||
require(factorAsDouble > 0) { "multipleOf requires the factor to be strictly larger than 0" } | ||
return addConstraint("must be a multiple of '{0}'", factor.toString()) { | ||
val division = it.toDouble() / factorAsDouble | ||
division.compareTo(division.roundToInt()) == 0 | ||
} | ||
} | ||
|
||
public fun <T : Number> ValidationBuilder<T>.maximum(maximumInclusive: Number): Constraint<T> = | ||
addConstraint( | ||
"must be at most '{0}'", | ||
maximumInclusive.toString(), | ||
) { it.toDouble() <= maximumInclusive.toDouble() } | ||
|
||
public fun <T : Number> ValidationBuilder<T>.exclusiveMaximum(maximumExclusive: Number): Constraint<T> = | ||
addConstraint( | ||
"must be less than '{0}'", | ||
maximumExclusive.toString(), | ||
) { it.toDouble() < maximumExclusive.toDouble() } | ||
|
||
public fun <T : Number> ValidationBuilder<T>.minimum(minimumInclusive: Number): Constraint<T> = | ||
addConstraint( | ||
"must be at least '{0}'", | ||
minimumInclusive.toString(), | ||
) { it.toDouble() >= minimumInclusive.toDouble() } | ||
|
||
public fun <T : Number> ValidationBuilder<T>.exclusiveMinimum(minimumExclusive: Number): Constraint<T> = | ||
addConstraint( | ||
"must be greater than '{0}'", | ||
minimumExclusive.toString(), | ||
) { it.toDouble() > minimumExclusive.toDouble() } |
44 changes: 44 additions & 0 deletions
44
src/commonMain/kotlin/io/konform/validation/constraints/StringConstraints.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,44 @@ | ||
package io.konform.validation.constraints | ||
|
||
import io.konform.validation.Constraint | ||
import io.konform.validation.ValidationBuilder | ||
|
||
public fun ValidationBuilder<String>.notBlank(): Constraint<String> = addConstraint("must not be blank") { it.isNotBlank() } | ||
|
||
/** | ||
* Checks that the string contains a match with the given [Regex]. | ||
* */ | ||
public fun ValidationBuilder<String>.containsPattern(pattern: Regex): Constraint<String> = | ||
addConstraint("must include regex '$pattern'") { | ||
it.contains(pattern) | ||
} | ||
|
||
public fun ValidationBuilder<String>.containsPattern(pattern: String): Constraint<String> = containsPattern(pattern.toRegex()) | ||
|
||
public fun ValidationBuilder<String>.minLength(length: Int): Constraint<String> { | ||
require(length >= 0) { IllegalArgumentException("minLength requires the length to be >= 0") } | ||
return addConstraint( | ||
"must have at least {0} characters", | ||
length.toString(), | ||
) { it.length >= length } | ||
} | ||
|
||
public fun ValidationBuilder<String>.maxLength(length: Int): Constraint<String> { | ||
require(length >= 0) { IllegalArgumentException("maxLength requires the length to be >= 0") } | ||
return addConstraint( | ||
"must have at most {0} characters", | ||
length.toString(), | ||
) { it.length <= length } | ||
} | ||
|
||
public fun ValidationBuilder<String>.pattern(pattern: String): Constraint<String> = pattern(pattern.toRegex()) | ||
|
||
/** Enforces the string must be UUID hex format. */ | ||
public fun ValidationBuilder<String>.uuid(): Constraint<String> = | ||
pattern("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$") hint "must be a valid UUID string" | ||
|
||
public fun ValidationBuilder<String>.pattern(pattern: Regex): Constraint<String> = | ||
addConstraint( | ||
"must match the expected pattern", | ||
pattern.toString(), | ||
) { it.matches(pattern) } |
Oops, something went wrong.