I have a go struct which I'm using for my POST of an entity
type Student struct {
ID string `json:"id" firestore:"id"`
Name string `json:"name" validate:"required" firestore:"name"`
}
From the POST body request I can send body as
{
"id" : 123,
"name" : "Student Name"
}
I want to implement a functionality where the request should fail while doing the validation saying "id" field in POST body is not allowed.
As I'm planning to reuse the same struct for GET I'm unable to skip the "id" in json marshalling and unmarshalling.
Is there any struct tag like allowed:true or false ?
I tried to skip the json validation but I want to reuse the same struct i was unable to proceed.
In the code logic i can just every time override it to empty value but it doesn't seem to be good way to add custom logic for updating the fields after converting into object.
Saw various validate struct tag but didn't find any that will match the use case
From what I understood, one of the various solutions that can work for you is by using the
lenvalitor. Thanks to it, you can enforce the provided struct to not have theIDvalue set. I saw that in your example theIDis treated as a string which means that the zero-value for it is"".Below, you can find a complete working example:
Every possible scenario that you might face should be covered:
""Let me know if this solves your question.