Skip to content
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

Closed
doodlewind opened this issue Dec 21, 2017 · 4 comments
Closed

How to serialize/persist a struct schema? #67

doodlewind opened this issue Dec 21, 2017 · 4 comments
Labels
question Issues that are usage questions

Comments

@doodlewind
Copy link

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 as struct seems possible.

Hope for your thoughts on this idea, thanks.

@szhangpitt
Copy link
Contributor

How do we serialize function based schema?

@doodlewind
Copy link
Author

I can think about the new Function workaround while it seems to be needlessly hacky. Guess this is the point why JSON schema works verbosely: declarative approach without function isn't enough for validation. So since we looks more like a superset of JSON schema, does it make sense importing JSON?

@ianstormtaylor ianstormtaylor added the question Issues that are usage questions label Dec 21, 2017
@ianstormtaylor
Copy link
Owner

Hey @doodlewind good question! I think there are three possible solutions...


Use JSON Schema

The 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 struct.function schemas directly, as @szhangpitt mentioned.)

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 Dynamically

Another 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 Structs

This is the last option, and the weakest one I think, but if people really want to serialize Superstruct definitions it's possible. Each Struct object actually has a Struct.schema property which is the schema that was passed into it. (Sometimes this is another Struct.) You could iterate a tree of structs and use that schema definition to serialize it.

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.

@ianstormtaylor ianstormtaylor changed the title Serializer for schema persistence How to serialize/persist a struct schema? Dec 21, 2017
@andreas-sqsh
Copy link

Hi @ianstormtaylor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that are usage questions
Projects
None yet
Development

No branches or pull requests

4 participants