I tried to find some insight in how to handle the duplication of client-side and server-side validation in my app. If, for example, I have an User Entity like this on my back end:
type User struct {
Id int `json:"id" validade:"required"`
Email string `json:"email" validate:"required,email"`
Name string `json:"name" validate:"required,max=20"`
Nickname string `json:"nickname"`
}
When creating/updating a new user, for a better user experience, I have to validate this on the front-end with something like this (I use React to manage this, but this is similar):
<input type="email" name="email" required>
<input type="text" name="name" required maxlength="20">
<input type="text" name="nickname">
So, if I want to change the nickname field to be mandatory, I would have to change this validation logic in 2 completely different apps, on different teams, which is pretty complicated.
When I was studying REST a few years ago, I learned about HATEOAS, in which the server tells the client where the URIs are located. And I was thinking if there is something like this for validation metadata so that I don't have to create this unnecessary coupling.
I tried to find something like this on my search, but with no luck. My idea was to make something like a OPTIONS request to my back-end and it would return the basic data schema for my front, and it would use this for validation:
{
"id": {
"type": "integer",
"required": true
},
"email": {
"type": "email",
"required": true
},
"name": {
"type": "string",
"required": true,
"maxlength": 20
},
"nickname": {
"type": "string",
"required": false
}
}
I think there is some kind of tool or best practice to handle this, but I couldn't find it. Right now, I have this validation metadata duplicated and it's kinda bad, but I can manage. Does someone knows how to handle this in a smart way?