-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to serialize/persist a struct schema? #67
Comments
How do we serialize function based schema? |
I can think about the |
Hey @doodlewind good question! I think there are three possible solutions... Use JSON SchemaThe first one is to just use JSON schema instead. Superstruct aims to solve lots of the verbosity, flexibility and understandability issues of JSON schema, but it does this by getting rid of the hard requirement that everything be expressible as pure JSON. (Specifically by allow validators to be functions, and even allowing inline If your use case requires serializing the schema to a database as JSON, and being able to read it out again to validate with later, there's a strong argument for just using JSON schema. This is especially true if you are exposing the schema definition as an API for end users, since JSON schema is a much less opinionated schema definition choice than a custom one based on Superstruct. It will be a bit more painful 😄 but it's a less opinionated choice. Generate Structs DynamicallyAnother option is to generate structs dynamically, based on some sort of "definition". There are valid use cases for this, especially when the "definition" is actually another piece of your API that is not schema-definition-specific. A good example of this would be if you were building a customizable form editor. You might allow users to add "field types" to their forms. Those "field types" would result in different UI widgets being rendered for the forms, but also in different data validation. This is a good case where using JSON schema would be unnecessarily complex, since the "field types" (and any associated "field options") are already your domain-specific schema definition. In this case, it would totally make sense to generate structs dynamically from the field definition. You'd probably read all of the fields for a form from the database, then generate the corresponding structs, then validate with them. Avoid Function StructsThis is the last option, and the weakest one I think, but if people really want to serialize Superstruct definitions it's possible. Each I wouldn't recommend this for most cases because its a non-standard way to define schemas, and locks you into the API opinions/goals of Superstruct. But it is possible I believe. I hope that helps! I'm going to update the title of the issue and leave it open for others who might be interested. |
this issue as well as #961 (tagged as idea) are closed Is there any update on this? Your mentioned case "Generate Structs Dynamically" is exactly what I am looking for |
Hi Ian, good to see you here, this library hits a pain point!
Previously I've involved in a JS library tracking user behaviour. For each site using it, site admins can configure tracking rules, which results in a dynamic schema to persist in DB. We picked JSON schema for that case, which works but looks verbose. So here comes the point: for dynamic schemas, can we serialize the
struct
to an exchangeable format, them parsing them at runtime? As a side effect supporting this feature, even importing JSON schema asstruct
seems possible.Hope for your thoughts on this idea, thanks.
The text was updated successfully, but these errors were encountered: