Skip to main content
These constraints accept a value of any type: a string, a number, an enum, a data class. They compare it with values you give, so they also cover rules across fields, such as a confirmation equal to the password. or, and and not combine other constraints into one rule, as Combining constraints explains.
A null argument checks nothing. A rule that reads a value that may be missing, such as max(customer.limit), never fails because of it: required on that value reports it once.
Preferences.kt

Equality

equalTo

equalTo(other) on any value Checks that the value is equal to other. BigDecimal values are compared by compareTo, so 1.0 equals 1.00, and zoned or offset dates and times by the instant they stand for. equalTo

notEqualTo

notEqualTo(other) on any value Checks that the value is not equal to other. notEqualTo

Membership

oneOf

oneOf(values) on any value Checks that the value is one of values. oneOf

noneOf

noneOf(values) on any value Checks that the value is none of values. noneOf

Null

notNull

notNull() on a nullable value Checks that the value is not null. Prefer required on a property: it also gives the block the non-null type. notNull

Combining constraints

or

or { } on any value Checks that at least one of the constraints of block holds. Each call in the block is a branch, and an and groups several constraints into one branch. An empty block holds. or

and

and { } on any value Checks that every constraint of block holds, as one rule. Inside an or, it makes one branch of several constraints. On its own, it reports one violation instead of one per constraint. and

not

not { } on any value Checks that none of the constraints of block holds. not
BigDecimal values compare by value, so 1.0 equals 1.00, and zoned or offset dates and times by the instant they stand for.