How validation is done? for example we need to che...
# serialization
r
How validation is done? for example we need to check that a property length should not be more than 100 characters
s
You can do it in
init
blocks which are executed after deserialization
r
usually on deserialization we want to collect all validation errors and return them in response, in this case validation about not null and other validation will be separated, so we can not collect all errors at once is this correct?
s
not-null validation is performed automatically by the framework
r
I mean for example we have a class
class Data(val name: String, val age: Int, val email: String)
when we send a json
{"email": "<mailto:xyz@xyz.com|xyz@xyz.com>", "name": "A"}
we should return a response where to indicate that
age
can not be null and
name
size must be greater than 2 Is this possible?
s
I think yes. If you throw, say
ValidationException
from init block of
Data
, you can write
Copy code
try {
   deserialize(request)
} catch (e: MissingFieldException) {
...
} catch (e: ValidationException) {
...
}
r
but how we will pass the constructor if we do not have value for
age
field?
s
Ah, I got it. Yes, you will get only first exception in this case, unfortunately
r
Oh, bad 😞 BTW, what do you think about to do validation before object creation? We had an interesting thread today on similar issue https://kotlinlang.slack.com/conversation/C0B8Q383C/p1545658515001900
s
It would be complicated to design, because validation is natural to do in
init
block, and its execution likely requires already constructed object...