#Dynamic JSON with validation

9 messages · Page 1 of 1 (latest)

gleaming crescent
#

Hello All,

I am looking to figure out a somewhat sensible way to create a tool like a form builder. What I am looking to achieve is to allow for user generated forms, however I am struggling with how I should approach the structure in the api and in the db side. So far I have just have a blueprint json column, which would just be parsed into a map[interface], however I would much prefer something more on the lines of a struct.

So I am unsure if I should just try to create the structs at runtime some how or do something a little different. I was thinking of doing something similar to EAV, but instead of 1 field = 1 row, it will be 1 row has a json column with the form fields in side of a json list like: [{ "Name": "", "Type": "", "Value", "Validation": "" }, ...etc] and then via the "type" I can cast the values, and in the validation I can insert some form of logic ( jsonlogic lib or something like omniparser lib) would this make sense or are there better patterns for this?

( I am just jumping into go, trying to re-write a few api's I have built in it, so I am more familiar with python, in python I kept the normal json structure with no types, and had a separate config json for validation and types to generate a pydantic model at runtime )

mystic lynx
#

You can implement your own UnmarshalJSON and do your validation there. The nice part is that you only have to do this for structs that are dynamic in nature

gleaming crescent
#

Would you have any reading recommendations for examples?

mystic lynx
#

Specifically "What do we do if we have to unmarshal JSON data that does not completely adhere to the struct we define?"

gleaming crescent
#

happened to be looking at the smae one haha

mystic lynx
#

😄

gleaming crescent
#

I'm exploring using GraphJin

#

looks like a fun little project, so this will be interesting