diff --git a/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Any.kt b/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Any.kt index 8fbe9975..4186b547 100644 --- a/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Any.kt +++ b/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Any.kt @@ -22,14 +22,116 @@ import dev.nesk.akkurate.constraints.constrain import dev.nesk.akkurate.constraints.otherwise import dev.nesk.akkurate.validatables.Validatable +/** + * The validatable value must be null when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator { isNull() } + * validate(null) // Success + * validate(1234) // Failure (message: Must be null) + * ``` + */ public fun Validatable.isNull(): Constraint = constrain { it == null } otherwise { "Must be null" } + +/** + * The validatable value must not be null when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator { isNotNull() } + * validate(1234) // Success + * validate(null) // Failure (message: Must not be null) + * ``` + */ public fun Validatable.isNotNull(): Constraint = constrain { it != null } otherwise { "Must not be null" } +/** + * The validatable value must be equal to [other] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator { isEqualTo("foo") } + * validate("foo") // Success + * validate("bar") // Failure (message: Must be equal to "foo") + * ``` + */ public fun Validatable.isEqualTo(other: T?): Constraint = constrain { it == other } otherwise { "Must be equal to \"$other\"" } + +/** + * The validatable value must be equal to [other] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { first.isEqualTo(second) } + * validate("foo" to "foo") // Success + * validate("foo" to "bar") // Failure (message: Must be equal to "bar") + * ``` + */ public fun Validatable.isEqualTo(other: Validatable): Constraint = constrain { it == other.unwrap() } otherwise { "Must be equal to \"${other.unwrap()}\"" } +/** + * The validatable value must be different from [other] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator { isNotEqualTo("foo") } + * validate("bar") // Success + * validate("foo") // Failure (message: Must be different from "foo") + * ``` + */ public fun Validatable.isNotEqualTo(other: T?): Constraint = constrain { it != other } otherwise { "Must be different from \"$other\"" } + +/** + * The validatable value must be different from [other] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { first.isNotEqualTo(second) } + * validate("foo" to "bar") // Success + * validate("foo" to "foo") // Failure (message: Must be different from "foo") + * ``` + */ public fun Validatable.isNotEqualTo(other: Validatable): Constraint = constrain { it != other.unwrap() } otherwise { "Must be different from \"${other.unwrap()}\"" } +/** + * The validatable value must be identical to [other] when this constraint is applied. + * + * The values are considered identical [if they are referentially equal.](https://kotlinlang.org/docs/equality.html#referential-equality) + * + * Code example: + * + * ``` + * data object Foo + * data object Bar + * + * val validate = Validator { isIdenticalTo(Foo) } + * validate(Foo) // Success + * validate(Bar) // Failure (message: Must be identical to "Foo") + * ``` + */ public fun Validatable.isIdenticalTo(other: T?): Constraint = constrain { it === other } otherwise { "Must be identical to \"$other\"" } + +/** + * The validatable value must not be identical to [other] when this constraint is applied. + * + * The values are considered not identical [if they are not referentially equal.](https://kotlinlang.org/docs/equality.html#referential-equality) + * + * Code example: + * + * ``` + * data object Foo + * data object Bar + * + * val validate = Validator { isNotIdenticalTo(Foo) } + * validate(Bar) // Success + * validate(Foo) // Failure (message: Must not be identical to "Foo") + * ``` + */ public fun Validatable.isNotIdenticalTo(other: T?): Constraint = constrain { it !== other } otherwise { "Must not be identical to \"$other\"" } diff --git a/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Array.kt b/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Array.kt index 84740579..d9e28ee7 100644 --- a/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Array.kt +++ b/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Array.kt @@ -31,46 +31,171 @@ import dev.nesk.akkurate.validatables.Validatable * union types in Kotlin, we must duplicate the code for each of those types. */ +/** + * The validatable [Array] must be empty when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isNotEmpty() } + * validate(arrayOf('a', 'b', 'c')) // Success + * validate(emptyArray()) // Failure (message: Must not be empty) + * ``` + */ @JvmName("arrayIsEmpty") public fun Validatable?>.isEmpty(): Constraint = constrainIfNotNull { it.isEmpty() } otherwise { "Must be empty" } +/** + * The validatable [Array] must not be empty when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isEmpty() } + * validate(emptyArray()) // Success + * validate(arrayOf('a', 'b', 'c')) // Failure (message: Must be empty) + * ``` + */ @JvmName("arrayIsNotEmpty") public fun Validatable?>.isNotEmpty(): Constraint = constrainIfNotNull { it.isNotEmpty() } otherwise { "Must not be empty" } +/** + * The validatable [Array] must have a size equal to [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeEqualTo(3) } + * validate(arrayOf('a', 'b', 'c')) // Success + * validate(arrayOf('a', 'b')) // Failure (message: The number of items must be equal to 3) + * ``` + */ @JvmName("arrayHasSizeEqualTo") public fun Validatable?>.hasSizeEqualTo(size: Int): Constraint = constrainIfNotNull { it.size == size } otherwise { "The number of items must be equal to $size" } +/** + * The validatable [Array] must have a size different from [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeNotEqualTo(3) } + * validate(arrayOf('a', 'b')) // Success + * validate(arrayOf('a', 'b', 'c')) // Failure (message: The number of items must be different from 3) + * ``` + */ @JvmName("arrayHasSizeNotEqualTo") public fun Validatable?>.hasSizeNotEqualTo(size: Int): Constraint = constrainIfNotNull { it.size != size } otherwise { "The number of items must be different from $size" } +/** + * The validatable [Array] must have a size lower than [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeLowerThan(3) } + * validate(arrayOf('a', 'b')) // Success + * validate(arrayOf('a', 'b', 'c')) // Failure (message: The number of items must be lower than 3) + * ``` + */ @JvmName("arrayHasSizeLowerThan") public fun Validatable?>.hasSizeLowerThan(size: Int): Constraint = constrainIfNotNull { it.size < size } otherwise { "The number of items must be lower than $size" } +/** + * The validatable [Array] must have a size lower than or equal to [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeLowerThanOrEqualTo(2) } + * validate(arrayOf('a')) // Success + * validate(arrayOf('a', 'b')) // Success + * validate(arrayOf('a', 'b', 'c')) // Failure (message: The number of items must be lower than or equal to 2) + * ``` + */ @JvmName("arrayHasSizeLowerThanOrEqualTo") public fun Validatable?>.hasSizeLowerThanOrEqualTo(size: Int): Constraint = constrainIfNotNull { it.size <= size } otherwise { "The number of items must be lower than or equal to $size" } +/** + * The validatable [Array] must have a size greater than [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeGreaterThan(2) } + * validate(arrayOf('a', 'b', 'c')) // Success + * validate(arrayOf('a', 'b')) // Failure (message: The number of items must be greater than 2) + * ``` + */ @JvmName("arrayHasSizeGreaterThan") public fun Validatable?>.hasSizeGreaterThan(size: Int): Constraint = constrainIfNotNull { it.size > size } otherwise { "The number of items must be greater than $size" } +/** + * The validatable [Array] must have a size greater than or equal to [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeGreaterThanOrEqualTo(2) } + * validate(arrayOf('a', 'b', 'c')) // Success + * validate(arrayOf('a', 'b')) // Success + * validate(arrayOf('a')) // Failure (message: The number of items must be greater than or equal to 2) + * ``` + */ @JvmName("arrayHasSizeGreaterThanOrEqualTo") public fun Validatable?>.hasSizeGreaterThanOrEqualTo(size: Int): Constraint = constrainIfNotNull { it.size >= size } otherwise { "The number of items must be greater than or equal to $size" } +/** + * The validatable [Array] must have a size [within the provided range][range] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeBetween(1..2) } + * validate(emptyArray()) // Failure (message: The number of items must be between 1 and 2) + * validate(arrayOf('a')) // Success + * validate(arrayOf('a', 'b')) // Success + * validate(arrayOf('a', 'b', 'c')) // Failure (message: The number of items must be between 1 and 2) + * ``` + */ @JvmName("arrayHasSizeBetween") public fun Validatable?>.hasSizeBetween(range: IntRange): Constraint = constrainIfNotNull { it.size in range } otherwise { "The number of items must be between ${range.first} and ${range.last}" } +/** + * The validatable [Array] must contain [element] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isContaining('b') } + * validate(arrayOf('a', 'b', 'c')) // Success + * validate(emptyArray()) // Failure (message: Must contain "b") + * ``` + */ @JvmName("arrayIsContaining") public fun Validatable?>.isContaining(element: T): Constraint = constrainIfNotNull { it.contains(element) } otherwise { "Must contain \"$element\"" } +/** + * The validatable [Array] must not contain [element] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isNotContaining('b') } + * validate(emptyArray()) // Success + * validate(arrayOf('a', 'b', 'c')) // Failure (message: Must not contain "b") + * ``` + */ @JvmName("arrayIsNotContaining") public fun Validatable?>.isNotContaining(element: T): Constraint = constrainIfNotNull { !it.contains(element) } otherwise { "Must not contain \"$element\"" } diff --git a/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Collection.kt b/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Collection.kt index 5e1e9395..92622cff 100644 --- a/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Collection.kt +++ b/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Collection.kt @@ -31,46 +31,171 @@ import dev.nesk.akkurate.validatables.Validatable * union types in Kotlin, we must duplicate the code for each of those types. */ +/** + * The validatable [Collection] must be empty when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isNotEmpty() } + * validate(listOf('a', 'b', 'c')) // Success + * validate(emptyList()) // Failure (message: Must not be empty) + * ``` + */ @JvmName("collectionIsEmpty") public fun Validatable?>.isEmpty(): Constraint = constrainIfNotNull { it.isEmpty() } otherwise { "Must be empty" } +/** + * The validatable [Collection] must not be empty when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isEmpty() } + * validate(emptyList()) // Success + * validate(listOf('a', 'b', 'c')) // Failure (message: Must be empty) + * ``` + */ @JvmName("collectionIsNotEmpty") public fun Validatable?>.isNotEmpty(): Constraint = constrainIfNotNull { it.isNotEmpty() } otherwise { "Must not be empty" } +/** + * The validatable [Collection] must have a size equal to [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeEqualTo(3) } + * validate(listOf('a', 'b', 'c')) // Success + * validate(listOf('a', 'b')) // Failure (message: The number of items must be equal to 3) + * ``` + */ @JvmName("collectionHasSizeEqualTo") public fun Validatable?>.hasSizeEqualTo(size: Int): Constraint = constrainIfNotNull { it.size == size } otherwise { "The number of items must be equal to $size" } +/** + * The validatable [Collection] must have a size different from [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeNotEqualTo(3) } + * validate(listOf('a', 'b')) // Success + * validate(listOf('a', 'b', 'c')) // Failure (message: The number of items must be different from 3) + * ``` + */ @JvmName("collectionHasSizeNotEqualTo") public fun Validatable?>.hasSizeNotEqualTo(size: Int): Constraint = constrainIfNotNull { it.size != size } otherwise { "The number of items must be different from $size" } +/** + * The validatable [Collection] must have a size lower than [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeLowerThan(3) } + * validate(listOf('a', 'b')) // Success + * validate(listOf('a', 'b', 'c')) // Failure (message: The number of items must be lower than 3) + * ``` + */ @JvmName("collectionHasSizeLowerThan") public fun Validatable?>.hasSizeLowerThan(size: Int): Constraint = constrainIfNotNull { it.size < size } otherwise { "The number of items must be lower than $size" } +/** + * The validatable [Collection] must have a size lower than or equal to [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeLowerThanOrEqualTo(2) } + * validate(listOf('a')) // Success + * validate(listOf('a', 'b')) // Success + * validate(listOf('a', 'b', 'c')) // Failure (message: The number of items must be lower than or equal to 2) + * ``` + */ @JvmName("collectionHasSizeLowerThanOrEqualTo") public fun Validatable?>.hasSizeLowerThanOrEqualTo(size: Int): Constraint = constrainIfNotNull { it.size <= size } otherwise { "The number of items must be lower than or equal to $size" } +/** + * The validatable [Collection] must have a size greater than [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeGreaterThan(2) } + * validate(listOf('a', 'b', 'c')) // Success + * validate(listOf('a', 'b')) // Failure (message: The number of items must be greater than 2) + * ``` + */ @JvmName("collectionHasSizeGreaterThan") public fun Validatable?>.hasSizeGreaterThan(size: Int): Constraint = constrainIfNotNull { it.size > size } otherwise { "The number of items must be greater than $size" } +/** + * The validatable [Collection] must have a size greater than or equal to [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeGreaterThanOrEqualTo(2) } + * validate(listOf('a', 'b', 'c')) // Success + * validate(listOf('a', 'b')) // Success + * validate(listOf('a')) // Failure (message: The number of items must be greater than or equal to 2) + * ``` + */ @JvmName("collectionHasSizeGreaterThanOrEqualTo") public fun Validatable?>.hasSizeGreaterThanOrEqualTo(size: Int): Constraint = constrainIfNotNull { it.size >= size } otherwise { "The number of items must be greater than or equal to $size" } +/** + * The validatable [Collection] must have a size [within the provided range][range] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeBetween(1..2) } + * validate(emptyList()) // Failure (message: The number of items must be between 1 and 2) + * validate(listOf('a')) // Success + * validate(listOf('a', 'b')) // Success + * validate(listOf('a', 'b', 'c')) // Failure (message: The number of items must be between 1 and 2) + * ``` + */ @JvmName("collectionHasSizeBetween") public fun Validatable?>.hasSizeBetween(range: IntRange): Constraint = constrainIfNotNull { it.size in range } otherwise { "The number of items must be between ${range.first} and ${range.last}" } +/** + * The validatable [Collection] must contain [element] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isContaining('b') } + * validate(listOf('a', 'b', 'c')) // Success + * validate(emptyList()) // Failure (message: Must contain "b") + * ``` + */ @JvmName("collectionIsContaining") public fun Validatable?>.isContaining(element: T): Constraint = constrainIfNotNull { it.contains(element) } otherwise { "Must contain \"$element\"" } +/** + * The validatable [Collection] must not contain [element] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isNotContaining('b') } + * validate(emptyList()) // Success + * validate(listOf('a', 'b', 'c')) // Failure (message: Must not contain "b") + * ``` + */ @JvmName("collectionIsNotContaining") public fun Validatable?>.isNotContaining(element: T): Constraint = constrainIfNotNull { !it.contains(element) } otherwise { "Must not contain \"$element\"" } diff --git a/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Map.kt b/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Map.kt index 636f728e..77183622 100644 --- a/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Map.kt +++ b/library/src/jvmMain/kotlin/dev/nesk/akkurate/constraints/builders/Map.kt @@ -31,50 +31,197 @@ import dev.nesk.akkurate.validatables.Validatable * union types in Kotlin, we must duplicate the code for each of those types. */ +/** + * The validatable [Map] must be empty when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isNotEmpty() } + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Success + * validate(emptyMap()) // Failure (message: Must not be empty) + * ``` + */ @JvmName("mapIsEmpty") public fun Validatable?>.isEmpty(): Constraint = constrainIfNotNull { it.isEmpty() } otherwise { "Must be empty" } +/** + * The validatable [Map] must not be empty when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isEmpty() } + * validate(emptyMap()) // Success + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Failure (message: Must be empty) + * ``` + */ @JvmName("mapIsNotEmpty") public fun Validatable?>.isNotEmpty(): Constraint = constrainIfNotNull { it.isNotEmpty() } otherwise { "Must not be empty" } +/** + * The validatable [Map] must have a size equal to [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeEqualTo(3) } + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Success + * validate(mapOf('a' to 1, 'b' to 2)) // Failure (message: The number of items must be equal to 3) + * ``` + */ @JvmName("mapHasSizeEqualTo") public fun Validatable?>.hasSizeEqualTo(size: Int): Constraint = constrainIfNotNull { it.size == size } otherwise { "The number of items must be equal to $size" } +/** + * The validatable [Map] must have a size different from [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeNotEqualTo(3) } + * validate(mapOf('a' to 1, 'b' to 2)) // Success + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Failure (message: The number of items must be different from 3) + * ``` + */ @JvmName("mapHasSizeNotEqualTo") public fun Validatable?>.hasSizeNotEqualTo(size: Int): Constraint = constrainIfNotNull { it.size != size } otherwise { "The number of items must be different from $size" } +/** + * The validatable [Map] must have a size lower than [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeLowerThan(3) } + * validate(mapOf('a' to 1, 'b' to 2)) // Success + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Failure (message: The number of items must be lower than 3) + * ``` + */ @JvmName("mapHasSizeLowerThan") public fun Validatable?>.hasSizeLowerThan(size: Int): Constraint = constrainIfNotNull { it.size < size } otherwise { "The number of items must be lower than $size" } +/** + * The validatable [Map] must have a size lower than or equal to [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeLowerThanOrEqualTo(2) } + * validate(mapOf('a' to 1)) // Success + * validate(mapOf('a' to 1, 'b' to 2)) // Success + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Failure (message: The number of items must be lower than or equal to 2) + * ``` + */ @JvmName("mapHasSizeLowerThanOrEqualTo") public fun Validatable?>.hasSizeLowerThanOrEqualTo(size: Int): Constraint = constrainIfNotNull { it.size <= size } otherwise { "The number of items must be lower than or equal to $size" } +/** + * The validatable [Map] must have a size greater than [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeGreaterThan(2) } + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Success + * validate(mapOf('a' to 1, 'b' to 2)) // Failure (message: The number of items must be greater than 2) + * ``` + */ @JvmName("mapHasSizeGreaterThan") public fun Validatable?>.hasSizeGreaterThan(size: Int): Constraint = constrainIfNotNull { it.size > size } otherwise { "The number of items must be greater than $size" } +/** + * The validatable [Map] must have a size greater than or equal to [size] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeGreaterThanOrEqualTo(2) } + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Success + * validate(mapOf('a' to 1, 'b' to 2)) // Success + * validate(mapOf('a' to 1)) // Failure (message: The number of items must be greater than or equal to 2) + * ``` + */ @JvmName("mapHasSizeGreaterThanOrEqualTo") public fun Validatable?>.hasSizeGreaterThanOrEqualTo(size: Int): Constraint = constrainIfNotNull { it.size >= size } otherwise { "The number of items must be greater than or equal to $size" } +/** + * The validatable [Map] must have a size [within the provided range][range] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { hasSizeBetween(1..2) } + * validate(emptyMap()) // Failure (message: The number of items must be between 1 and 2) + * validate(mapOf('a' to 1)) // Success + * validate(mapOf('a' to 1, 'b' to 2)) // Success + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Failure (message: The number of items must be between 1 and 2) + * ``` + */ @JvmName("mapHasSizeBetween") public fun Validatable?>.hasSizeBetween(range: IntRange): Constraint = constrainIfNotNull { it.size in range } otherwise { "The number of items must be between ${range.first} and ${range.last}" } +/** + * The validatable [Map] must contain [the provided key][key] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isContainingKey('b') } + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Success + * validate(emptyMap()) // Failure (message: Must contain key "b") + * ``` + */ public fun Validatable?>.isContainingKey(key: K): Constraint = constrainIfNotNull { it.containsKey(key) } otherwise { "Must contain key \"$key\"" } +/** + * The validatable [Map] must not contain [the provided key][key] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isNotContainingKey('b') } + * validate(emptyMap()) // Success + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Failure (message: Must not contain key "b") + * ``` + */ public fun Validatable?>.isNotContainingKey(key: K): Constraint = - constrainIfNotNull { !it.containsKey(key) } otherwise { "Must contain key \"$key\"" } + constrainIfNotNull { !it.containsKey(key) } otherwise { "Must not contain key \"$key\"" } +/** + * The validatable [Map] must contain [the provided value][value] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isContainingValue(2) } + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Success + * validate(emptyMap()) // Failure (message: Must contain value "2") + * ``` + */ public fun Validatable?>.isContainingValue(value: V): Constraint = constrainIfNotNull { it.containsValue(value) } otherwise { "Must contain value \"$value\"" } +/** + * The validatable [Map] must not contain [the provided value][value] when this constraint is applied. + * + * Code example: + * + * ``` + * val validate = Validator> { isNotContainingValue(2) } + * validate(emptyMap()) // Success + * validate(mapOf('a' to 1, 'b' to 2, 'c' to 3)) // Failure (message: Must not contain value "2") + * ``` + */ public fun Validatable?>.isNotContainingValue(value: V): Constraint = - constrainIfNotNull { !it.containsValue(value) } otherwise { "Must contain value \"$value\"" } + constrainIfNotNull { !it.containsValue(value) } otherwise { "Must not contain value \"$value\"" }