Skip to main content
Query strings, form fields and CSV columns carry numbers, dates and enums as text. A convertTo call checks that the text converts, and runs its block on the converted value, with the constraints of that type:
SortOrder.kt
SearchQuery.kt
When the text does not convert, there is one violation with the code of the conversion, and the block does not run. When it converts, the block checks the value, and its violations keep the path of the text.

The shortcuts

The dates and times are ISO 8601. Each shortcut has its own message, such as must be a valid integer, which can be changed for one call (convertToInt("must be a page number") { }) or for all of them, in the messages.
A block can be empty: convertToEnum<SortOrder>(ignoreCase = true) {} only checks that the text names an entry.

Your own conversion

convert takes any function that returns the converted value, or null when it cannot convert:
It reports has an invalid format, with the code convert, when the function returns null. Exceptions it throws are not caught: return null instead.

Convert or check?

When you only need to know that the text has a shape, without validating the value, a format constraint is enough: numeric(), integer() or boolean() on a string, or isoDate(). Convert when the value has rules of its own, such as a page that starts at 1.