Fluent guard clause validation for Kotlin with descriptive error messages
implementation com.philiprehberger:guard-clauseFluent guard clause validation for Kotlin with descriptive error messages.
implementation("com.philiprehberger:guard-clause:0.2.0")
<dependency>
<groupId>com.philiprehberger</groupId>
<artifactId>guard-clause</artifactId>
<version>0.2.0</version>
</dependency>
import com.philiprehberger.guardclause.*
fun createUser(name: String, age: Int, email: String) {
guard(name, "name").notBlank().minLength(2).maxLength(50)
guard(age, "age").positive().inRange(1.0, 150.0)
guard(email, "email").notBlank().matches(Regex(".+@.+\\..+"))
// proceed with valid data...
}
guard(password, "password")
.minLength(8)
.must("must contain a digit") { it.any { c -> c.isDigit() } }
.must("must contain uppercase") { it.any { c -> c.isUpperCase() } }
val errors = guardAll {
check(name, "name") { notBlank().minLength(2) }
check(age, "age") { positive() }
check(email, "email") { notBlank().matches(Regex(".+@.+")) }
}
if (errors.isNotEmpty()) {
errors.forEach { println("${it.parameterName}: ${it.condition}") }
}
guard(email, "email").notBlank().isEmail()
guard(website, "website").notBlank().isUrl()
guard(filename, "filename")
.startsWith("report_")
.endsWith(".csv")
.contains("2026")
guard(role, "role").isIn(listOf("admin", "user", "moderator"))
guard(password, "password")
.minLength(8)
.satisfies("must contain a digit") { it.any { c -> c.isDigit() } }
.satisfies("must contain uppercase") { it.any { c -> c.isUpperCase() } }
| Function / Class | Description |
|---|---|
guard(value, name) | Create a fluent guard clause |
GuardClause.notNull() | Assert value is not null |
GuardClause.notBlank() | Assert string is not blank |
GuardClause.minLength(n) | Assert minimum string length |
GuardClause.maxLength(n) | Assert maximum string length |
GuardClause.matches(regex) | Assert string matches pattern |
GuardClause.positive() | Assert number is positive |
GuardClause.negative() | Assert number is negative |
GuardClause.inRange(min, max) | Assert number is in range |
GuardClause.notEmpty() | Assert collection is not empty |
GuardClause.minSize(n) | Assert minimum collection size |
GuardClause.maxSize(n) | Assert maximum collection size |
GuardClause.must { } | Custom predicate validation |
GuardClause.isEmail() | Assert string is a valid email |
GuardClause.isUrl() | Assert string is a valid URL |
GuardClause.startsWith(prefix) | Assert string starts with prefix |
GuardClause.endsWith(suffix) | Assert string ends with suffix |
GuardClause.contains(substring) | Assert string contains substring |
GuardClause.isIn(collection) | Assert value is in collection |
GuardClause.satisfies(name) { } | Named custom predicate validation |
guardAll { } | Collect all errors instead of fail-fast |
GuardException | Thrown on validation failure |
./gradlew test # Run tests
./gradlew check # Run all checks
./gradlew build # Build JAR
If you find this project useful: