Skip to content

Built in Rules

tessoir edited this page Mar 24, 2026 · 2 revisions

Built-in Rules

All built-in rules accept an optional reason parameter. When omitted, a sensible default message is used. Supply your own wherever a more precise explanation adds value:

verify(user::age).atLeast(18, reason = "User must be at least 18 years old")

String rules

Applied to Verification<String>.

Rule Fails when Violation type
notBlank() The string is blank NotBlankViolation
minLength(min) The string is shorter than min characters MinLengthViolation
maxLength(max) The string is longer than max characters MaxLengthViolation
exactLength(length) The string length does not equal length ExactLengthViolation
lengthRange(min, max) The string length is outside the range [min, max] LengthRangeViolation
validateCollecting {
    verify(user::username).notBlank().minLength(3).maxLength(20)
    verify(user::bio).maxLength(160)
    verify(user::countryCode).exactLength(2)
}

Comparable rules

Applied to Verification<T> where T : Comparable<T>.

Rule Fails when Violation type
atLeast(min) The value is less than min (inclusive) AtLeastViolation
atMost(max) The value is greater than max (inclusive) AtMostViolation
between(min, max) The value is outside the range [min, max] BetweenViolation
greaterThan(min) The value is not greater than min (exclusive) GreaterThanViolation
lessThan(max) The value is not less than max (exclusive) LessThanViolation
validateCollecting {
    verify(order::total).greaterThan(0.0)
    verify(product::rating).between(1, 5)
    verify(user::age).atLeast(18)
}

Collection rules

Applied to Verification<C> where C : Collection<*>.

Rule Fails when Violation type
minSize(min) The collection has fewer than min elements MinSizeViolation
maxSize(max) The collection has more than max elements MaxSizeViolation
exactSize(size) The collection size does not equal size ExactSizeViolation
sizeRange(min, max) The collection size is outside the range [min, max] SizeRangeViolation
distinct() The collection contains duplicate elements DistinctViolation
validateCollecting {
    verify(order::items).minSize(1).maxSize(100)
    verify(user::tags).distinct().maxSize(10)
}

Equality rules

Applied to Verification<T>.

Rule Fails when Violation type
notNull() The value is null NotNullViolation
equalTo(expected) The value does not equal expected EqualToViolation
notEqualTo(forbidden) The value equals forbidden NotEqualToViolation
oneOf(allowed) The value is not in allowed OneOfViolation
noneOf(forbidden) The value is in forbidden NoneOfViolation
validateCollecting {
    verify(user::role).oneOf(setOf("admin", "editor", "viewer"))
    verify(user::status).notEqualTo("banned")
    verify(user::referralCode).notNull()
}

Violation types

Every built-in rule produces a typed violation that implements PathAwareViolation. In addition to reason, each violation carries the validationPath that identifies where in the validated structure the failure occurred.

All violation types are in the io.github.kverify.violations package.

Violation type Carries
NotBlankViolation validationPath, reason
MinLengthViolation minLengthAllowed, actualLength, validationPath, reason
MaxLengthViolation maxLengthAllowed, actualLength, validationPath, reason
ExactLengthViolation expectedLength, actualLength, validationPath, reason
LengthRangeViolation minLengthAllowed, maxLengthAllowed, actualLength, validationPath, reason
AtLeastViolation minAllowed, actual, validationPath, reason
AtMostViolation maxAllowed, actual, validationPath, reason
BetweenViolation min, max, actual, validationPath, reason
GreaterThanViolation minExclusive, actual, validationPath, reason
LessThanViolation maxExclusive, actual, validationPath, reason
MinSizeViolation minSizeAllowed, actualSize, validationPath, reason
MaxSizeViolation maxSizeAllowed, actualSize, validationPath, reason
ExactSizeViolation expectedSize, actualSize, validationPath, reason
SizeRangeViolation minSizeAllowed, maxSizeAllowed, actualSize, validationPath, reason
DistinctViolation actualSize, distinctSize, validationPath, reason
NotNullViolation validationPath, reason
EqualToViolation expected, actual, validationPath, reason
NotEqualToViolation forbidden, validationPath, reason
OneOfViolation allowed, actual, validationPath, reason
NoneOfViolation forbidden, actual, validationPath, reason

These types let you handle specific failures by type rather than by parsing strings:

result.violations
    .filterIsInstance<MinLengthViolation>()
    .forEach { println("${it.validationPath}: must be at least ${it.minLengthAllowed} characters") }

Next steps

  • Violation — understand the typed violations produced by these rules
  • Verification — learn how rules are chained and applied

Clone this wiki locally