I have a class ```data class Login(@get:NotBlank @...
# announcements
r
I have a class
Copy code
data class Login(@get:NotBlank @get:Size(min=3) @JsonProperty(value = "login", required = true) val login: String, 
                  @get:NotBlank @JsonProperty(value = "password", required = true) val password: String)
annotations are used for validation, validation is executed after constructor but before validation, I need to trim
login
and
password
, in order to reject values as " r " for
login
, because real value is "r" and size is 1, which is less than 3 how to do this in Kotlin?
r
you can try:
Copy code
class Login(login: String, password: String){
    @get:NotBlank @get:Size(min=3) @JsonProperty(value = "login", required = true) val login: String = login.trim()
    @get:NotBlank @JsonProperty(value = "password", required = true) val password: String = password.trim()
}
however with this approach you cannot have
data
class
The other approach might be private constructor and factory method
Copy code
data class Login private constructor(@get:NotBlank @get:Size(min=3) @JsonProperty(value = "login", required = true) val login: String,
                 @get:NotBlank @JsonProperty(value = "password", required = true) val password: String){
    companion object Factory{
        operator fun invoke(login: String, password: String) = Login(login.trim(), password.trim())
    }
}
You then use it as if it was constructor invocation, whereas in fact it is invoking operator
invoke
Copy code
val login = Login("login", "pass")