<@U12AGS8JG> Forgive the ping here, but there's no...
# random
g
@sam Forgive the ping here, but there's no channel for Tribune or GH Discussions enabled on it I have a question -- I have value classes which get deserialized from primitive values like
String
using Jackson Currently, these are modeled like this:
Copy code
data class ColumnName
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
constructor(@JsonValue val value: String) {
    init {
        require(value.isNotBlank()) { "Column name cannot be blank" }
        require(value.all { it.isLetterOrDigit() || it == '_' }) { "Column name must be alphanumeric" }
    }
}
Would the best practice using your library be to just replace the
require()
with
require(parse(value))
?
Something like this?
Copy code
data class ColumnName
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
constructor(@JsonValue val value: String) {
    init {
        parser.parse(value).tapInvalid { error(it) }
    }

    companion object {
        val parser = Parser.fromNullableString()
            .notNullOrBlank { "Column name cannot be blank" }
            .filter({ it.all { it.isLetterOrDigit() || it == '_' } }, { "Column name must be alphanumeric" })
            .map { it }
    }
}
s
It's kind of the other way around when you do parse not validate. You would have a data type representing the possible input - in this case a nullable field I guess. Then you would write a parser to turn NullableColumnName to ColumnName. All your logic would accept ColumnName and you know if supplied, it has already been parsed/validated.
g
Ahh okay -- ty Sam!