Giter Site home page Giter Site logo

Comments (4)

ianstormtaylor avatar ianstormtaylor commented on May 22, 2024 2

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.

from superstruct.

szhangpitt avatar szhangpitt commented on May 22, 2024

How do we serialize function based schema?

from superstruct.

doodlewind avatar doodlewind commented on May 22, 2024

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?

from superstruct.

andreas-sqsh avatar andreas-sqsh commented on May 22, 2024

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

from superstruct.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.