Comments (4)
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.
How do we serialize function based schema?
from superstruct.
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.
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)
- [Question|Feature] Add functions to an interface
- [Feature] Support for `Required` HOT 1
- Please provide security reporting information or enable advisories
- `intersection` flattens unions too much HOT 1
- [Feature] `one of` or `or` type HOT 2
- [Feature] Discriminated unions (fast lookup) HOT 1
- `size` on `array` uses a permissive type
- Defaulted optional to undefined does not fill field with default value
- Never type creates a typescript issue
- Dynamic context property access
- Unexpected behaviour when combining `assign` + `refine`
- pattern (regex) does not return consistent output when using the global modifier
- Source maps seem broken HOT 4
- `instance` causes a TypeScript error with abstract classes
- Unions of instances produce unhelpful error message
- Fix compatibility with Node16/NodeNext moduleResolution HOT 4
- Mask fails on `interface aaa { foo: string; [key: string]: any }` interface with any key.
- Move tests to Vitest
- Describe does not work well with union types
- Does "create" supports "passthrough"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from superstruct.