Skip to content
Bluetape4k docs1.11

Validation and invariants

Latest stable Based on Bluetape4k release 1.11.0

Validation is the boundary that prevents invalid state from entering the system. Exception meaning follows the owner of the failed rule.

Validation boundaries for caller arguments, object state, and domain rules

Failed ruleDefault toolSurface
caller argumentrequire, requireNotBlank, requirePositiveNumber, etc.IllegalArgumentException
state of an existing objectcheck or explicit state guardIllegalStateException
business/domain ruledomain validator/result/exceptiondomain-specific surface

Replacing domain failure with a generic precondition prevents callers from distinguishing retry, user feedback, and state transitions.

Most require* extensions return the validated receiver.

class SearchRequest(rawQuery: String?, limit: Int) {
val query: String = rawQuery.requireNotBlank("query").trim()
val limit: Int = limit.requireInRange(1, 100, "limit")
}

The helper set covers null/empty/blank, text containment, equality/comparison, closed/open ranges, numeric signs, and array/collection/map conditions. Keep plain require when it is clearer.

fun createAccount(command: CreateAccount): AccountId {
val email = command.email.requireNotBlank("email")
command.initialCredit.requireZeroOrPositiveNumber("initialCredit")
return repository.insert(email, command.initialCredit)
}

Racy invariants still require transactions or uniqueness constraints; preconditions alone cannot make them atomic.

  • include parameter name and expected condition;
  • never include passwords, tokens, or raw sensitive payloads;
  • count caller argument failures separately from server-state violations;
  • keep high-cardinality raw values out of metric labels.

Cover values immediately below and above boundaries, null, empty, blank, open/closed endpoints, and returned receiver identity. Assert exception type and parameter name when they are public contract.

Next, keep validated data inside a fixed memory budget with Bounded collections.