Validate generic struct inner values using existing tags even with private fields #1232
-
Hi, Goal of this struct is to differentiate, when decoding JSON, if a field is unset, if a field is null, or if a field is set to something else (e. g. A string). Example usage might be I would like to be able to use existing tags to validate the inner value, so for example, I would like to do the following
In my mind this would achieve the following: custom_just would use the other validations only if isset is true. Is it possible to achieve this? If yes, how? I'm open to alternatives, I was considering decoding to a map. Maybe struct is defined as:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
@Fire-Dragon-DoL currently only custom validations, custom struct level validations or Registering a Custom type function to handle each individual type would be the only ways to handle that. I am thinking about how to achieve this in a more generic way without the package needing to know each type beforehand to support my own similar Option generic struct. I'm thinking about adding something similar to registering a custom type function but a custom interface function, the only issue is it's a very implicit thing to be doing which could cause unintended side-effects if anything else implements or changes to implement unknowingly the registered interface and so continuing to think about it more. Until something like that can be added I think the best way is to use registering a custom type function, and example can be found in the examples here. |
Beta Was this translation helpful? Give feedback.
@Fire-Dragon-DoL currently only custom validations, custom struct level validations or Registering a Custom type function to handle each individual type would be the only ways to handle that.
I am thinking about how to achieve this in a more generic way without the package needing to know each type beforehand to support my own similar Option generic struct.
I'm thinking about adding something similar to registering a custom type function but a custom interface function, the only issue is it's a very implicit thing to be doing which could cause unintended side-effects if anything else implements or changes to implement unknowingly the registered interface and so continuing to think about i…