Cies
11/04/2024, 9:42 AMdata class Person(val name: String?, val dateOfBirth: LocalDate?) {
fun validate(): ValidationResult { ... }
}
When we submit an empty form name
and dateOfBirth
are null
. Say this is fine for dateOfBirth
(it's not a required field) and name
fails validation. So we present the user with the validation errors and allow 'm to try again.
Now user submits again with a name
and the dto validates. Only all the required fields (in this case only one) are still nullable! So mapping the dto to db records (we use jOOQ for that) we have a lot of `!!`s, i.e. record.name = dto.name!!
. Ideally I'd like the validate method to return a ValidatedPerson
data class which has all required fields marked as non-nullable. And the only way I can imagine to have this, is by doing A LOT of boiler plate: defining the ``ValidatedPerson`` myself, mapping to it by hand, etc. This is so much work that I rather do the `!!`s on the mapping to the record.
Like I said: just curious is you are doing something similar to me, or have found another way to handle this.Johann Pardanaud
11/04/2024, 1:06 PMname
should'nt be nullable since its not optional. If the form is sent without filing name
, you should receive an empty string and it should fail validation.
Bonus:
• You don't have to check for null and empty to check if the name is invalid.
• You don't have to cast the value.Johann Pardanaud
11/04/2024, 1:07 PMCies
11/04/2024, 2:22 PMdave08
11/05/2024, 11:40 AMdave08
11/05/2024, 11:41 AMdave08
11/05/2024, 11:43 AMdave08
11/05/2024, 11:44 AMJohann Pardanaud
11/05/2024, 1:13 PMdave08
11/05/2024, 1:16 PMR
...dave08
11/05/2024, 1:17 PMdave08
11/05/2024, 1:18 PMdave08
11/05/2024, 1:18 PMdave08
11/05/2024, 1:19 PM@Validate(mapTo = SomeStrictTypedClass::class)
dave08
11/05/2024, 1:20 PMJohann Pardanaud
11/05/2024, 1:21 PMdave08
11/05/2024, 1:57 PMJohann Pardanaud
11/05/2024, 1:58 PMdave08
11/05/2024, 1:59 PMdave08
11/05/2024, 2:02 PMaudax
11/19/2024, 9:32 AM