Issues/Ergonomics around Type.Ref, Type.Recursive and TypeCompiler #884
Unanswered
kade-robertson
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
I'm running into some issues attempting to (and potentially misusing) this package to define a schema to validate GeoJSON. The general idea of my project is to define the structure with Typebox as a source-of-truth, and then (try) to use codegen to produce validators for other options: https://github.com/kade-robertson/rfc7946
The trouble I'm running into is around some of the ergonomics issues I've run into with Type.Ref. You'll note in the
master
branch that all of my types (in thedefinitions
package) are defined with$id
s, but do not useType.Ref
to be referenced later. Adding the$id
property was required to have codegen viatypebox-codegen
to work for Zod, which was the first example I wanted to try out. This main branch works reasonably well for those two packages.However, the JSON schema generated is unusable, seemingly due to some of the tuple definitions ending up with duplicate IDs in the output (I think, it's not immediately clear) -- at the very least, it's JSON schema that
ajv
can't resolve (see PR: kade-robertson/rfc7946#1).It seemed the most logical to me that this could be resolved with references, so I set up those changes and got the JSON schema output to be much more reasonable, functional enough to actually get
ajv
working (though with an admittedly awkward process to register all of the dependencies for the mainFeatureCollection
schema). However, this broke Typebox -- bothValue
andTypeCompiler
could no longer resolve the type. I went through old issues on this repo and saw mention ofType.Deref
, which I got working with some similar ugliness to howajv
worked, butTypeCompiler
still failed to work with the dereferenced type, with an error showing up on the recursive type: kade-robertson/rfc7946#2So, this leads to a couple of questions:
Type.Deref
? Should I be exporting Deref'd versions of every type?As an aside, you'll note that repo has a small benchmark in it just to validate the setup. I very well could have set it up wrong, but I seem to be running into performance issues where Typebox is significantly slower than ajv, even with TypeCompiler being used. The repo isn't in a state where any branch has all benchmarks working, but the results on
master
look like:whereas on the second PR that gets ajv working:
None of them are performing in a way that's concerning to me (though at some point I will sort out a larger, more varied dataset), it's moreso just the disparity between ajv and typebox here doesn't line up with some of the benchmarks in the README.
Beta Was this translation helpful? Give feedback.
All reactions