Arnab
09/11/2025, 8:13 AMdata class MyInput(val foo: Foo?, val bar: Bar?)
How do we validate that one of these are non null?
What is the best practice? Is it:
requireNotNull(input.foo) { "field foo cannot be null" }
Or is it something like:
val foo = input.foo ?: throw SomeException("...")
What is the most idiomatic way?thanksforallthefish
09/11/2025, 8:47 AMArnab
09/11/2025, 9:05 AMthanksforallthefish
09/11/2025, 9:10 AMKlitos Kyriacou
09/11/2025, 10:02 AMfoo has to be non-null to be valid, you could simply make it non-nullable:
data class MyInput(val foo: Foo, val bar: Bar?)
and if you try to deserialize a null JSON from an HTTP request, it will respond with an error (usually just where you want it)kqr
09/11/2025, 11:30 AMkqr
09/11/2025, 11:31 AMkqr
09/11/2025, 11:31 AMthanksforallthefish
09/12/2025, 6:58 AMthanksforallthefish
09/12/2025, 7:00 AMArnab
09/12/2025, 8:12 AMfoo is sometimes null, but other times it cannot be. So the validation needs to be a bit more complex.