Skip to content

Commit

Permalink
Move io.konform.validation.jsonschema to io.konform.validation.const…
Browse files Browse the repository at this point in the history
…raints (#165)
  • Loading branch information
dhoepelman authored Nov 13, 2024
1 parent 7fb9b8e commit a2dff2b
Show file tree
Hide file tree
Showing 14 changed files with 384 additions and 136 deletions.
50 changes: 46 additions & 4 deletions api/konform.api
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,60 @@ public abstract class io/konform/validation/ValidationResult {
public final fun map (Lkotlin/jvm/functions/Function1;)Lio/konform/validation/ValidationResult;
}

public final class io/konform/validation/constraints/EnumConstraintsKt {
public static final fun const (Lio/konform/validation/ValidationBuilder;Ljava/lang/Object;)Lio/konform/validation/Constraint;
public static final fun enum (Lio/konform/validation/ValidationBuilder;[Ljava/lang/Object;)Lio/konform/validation/Constraint;
}

public final class io/konform/validation/constraints/IterableConstraintsKt {
public static final fun arrayMaxItems (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun arrayMinItems (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun arrayUniqueItems (Lio/konform/validation/ValidationBuilder;Z)Lio/konform/validation/Constraint;
public static synthetic fun arrayUniqueItems$default (Lio/konform/validation/ValidationBuilder;ZILjava/lang/Object;)Lio/konform/validation/Constraint;
public static final fun maxItems (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun minItems (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun uniqueItems (Lio/konform/validation/ValidationBuilder;Z)Lio/konform/validation/Constraint;
public static synthetic fun uniqueItems$default (Lio/konform/validation/ValidationBuilder;ZILjava/lang/Object;)Lio/konform/validation/Constraint;
}

public final class io/konform/validation/constraints/MapConstraintsKt {
public static final fun maxItems (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun maxProperties (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun minItems (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun minProperties (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun uniqueItems (Lio/konform/validation/ValidationBuilder;Z)Lio/konform/validation/Constraint;
public static synthetic fun uniqueItems$default (Lio/konform/validation/ValidationBuilder;ZILjava/lang/Object;)Lio/konform/validation/Constraint;
}

public final class io/konform/validation/constraints/NumberConstraintsKt {
public static final fun exclusiveMaximum (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
public static final fun exclusiveMinimum (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
public static final fun maximum (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
public static final fun minimum (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
public static final fun multipleOf (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
}

public final class io/konform/validation/constraints/StringConstraintsKt {
public static final fun containsPattern (Lio/konform/validation/ValidationBuilder;Ljava/lang/String;)Lio/konform/validation/Constraint;
public static final fun containsPattern (Lio/konform/validation/ValidationBuilder;Lkotlin/text/Regex;)Lio/konform/validation/Constraint;
public static final fun maxLength (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun minLength (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun notBlank (Lio/konform/validation/ValidationBuilder;)Lio/konform/validation/Constraint;
public static final fun pattern (Lio/konform/validation/ValidationBuilder;Ljava/lang/String;)Lio/konform/validation/Constraint;
public static final fun pattern (Lio/konform/validation/ValidationBuilder;Lkotlin/text/Regex;)Lio/konform/validation/Constraint;
public static final fun uuid (Lio/konform/validation/ValidationBuilder;)Lio/konform/validation/Constraint;
}

public final class io/konform/validation/jsonschema/JsonSchemaKt {
public static final fun const (Lio/konform/validation/ValidationBuilder;Ljava/lang/Object;)Lio/konform/validation/Constraint;
public static final fun enum (Lio/konform/validation/ValidationBuilder;[Ljava/lang/Object;)Lio/konform/validation/Constraint;
public static final fun exclusiveMaximum (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
public static final fun exclusiveMinimum (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
public static final fun maxLength (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun maxProperties (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun maximum (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
public static final fun minLength (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun minProperties (Lio/konform/validation/ValidationBuilder;I)Lio/konform/validation/Constraint;
public static final fun minimum (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
public static final fun multipleOf (Lio/konform/validation/ValidationBuilder;Ljava/lang/Number;)Lio/konform/validation/Constraint;
public static final fun pattern (Lio/konform/validation/ValidationBuilder;Ljava/lang/String;)Lio/konform/validation/Constraint;
Expand All @@ -140,10 +186,6 @@ public final class io/konform/validation/path/ValidationPath$Companion {
public final fun fromAny ([Ljava/lang/Object;)Lio/konform/validation/path/ValidationPath;
}

public final class io/konform/validation/string/ValidationBuilderStringKt {
public static final fun notBlank (Lio/konform/validation/ValidationBuilder;)Lio/konform/validation/Constraint;
}

public final class io/konform/validation/types/EmptyValidation : io/konform/validation/Validation {
public static final field INSTANCE Lio/konform/validation/types/EmptyValidation;
public fun equals (Ljava/lang/Object;)Z
Expand Down
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 }
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 }
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()
}
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()
}
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() }
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) }
Loading

0 comments on commit a2dff2b

Please sign in to comment.