rradczewski / flowtypify Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
Saves bundle size for code that will never change. Output should be good enough to spot errors and make sense of the types, but doesn't need to be too fancy.
"foo-bar-123"
is a valid name for referencing a type in JSON schemata, but it's not a valid identifier in flowtype/javascript
. Should be transformed to FooBar123
(or fail in a strict mode, but be allowed when transformed)
flowtypify creates a flowtype definition from a JSON schema. This has been highly useful to me because it gives me compile-time types for an API contract so I can build a service for an API without having to reverse-engineer the types.
TL;DR: This needs to be a proper parser and I don't know where to start.
flowtypify can already generate types for lots of different schemas by very crudely using strings instead of properly generating an AST and generating flowtype definitions from it. While that works for simple types, it fails for complex types involving references.
The problem (illustrated by this failing testcase) is that $id
and in general paths outside of #/definitions
are not resolved to distinct type definitions in the current implementation.
Even worse, the current definition just parses a $ref
that matches /^#\/definitions\/([^\/]+)$/
(src) and prints out the matching group as the target type and "hopes for the best".
The missing piece to this is probably keeping track of all the types that are to be extracted and their names ($id
and their path inside the schema) so we can reference them later when transforming the schema into a flowtype definition.
It's a fundamental problem I can't seem to wrap my head around. What's something I could look into that would teach me to write such a parser? Is there some prior art in other languages that I could look at?
Superseeds Issue #1 as I feel like this is more broad.
Right now, flowtypify
only supports $ref
pointing to #/
or everything below #/definitions/
. Implementing these was easy because we know all root-level types that will ever exist in this schema right after parsing .definitions
.
With $id
and how it is used in the core schema, any subschema can be given a name through $id
and can then be referenced from anywhere else.
This means we probably would need to rework the parser so it can create new (exported) types and resolve references to them.
json-schema-to-typescript
uses json-schema-ref-parser
for that - their bundled approach is interesting but only partially solves the problem.
A new parser would need to work in two steps (hooray for lexers and parsers):
$id
and $ref
fieldsReferenceType
, if it...
$id
field$ref
(be it through #/definitions
or #/properties/address/type
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.