JSON Schema type inference
Hey, I'm unsure if I'm using @ark/json-schema incorrectly, but while supplying a const type that contains a JSON schema, I get unknown on the other side. Is this intended behavior, and if so, could it be tweaked to contain the type information inherited from the JSON schema?
3 Replies


Unfortunately the initial version of
@ark/json-schema
does not include inference for raw JSON Schema.
@TizzySaurus had actually worked on a prototype for this and its the kind of thing we'd be interested in integrating in the future, but there's a lot of complexity to having the type inference be as performant as the core of arktype.
We didn't want to release a version that would choke prematurely for large schemas, so for now, you would have to use another tool to infer the JSON schema, then cast your arktype Type
to use that declaration. I remember looking at a package that handled this, but it had significant performance issues even for relatively innocuous schemas.
Generally, if it all possible we'd recommend starting from an ArkType definition and using toJsonSchema
to go in the other direction, but for cases where it's not, you'll have to find or implement an external solution that works for inferring your JSON schema until we are able to build that back in a way we're confident in.Fwiw I believe https://www.npmjs.com/package/json-schema-to-ts is the package David is referring to. This is largely what I've seen people using, but yeah, the performance of it isn't great (it may be "good enough" for you though, so figured I'd link)
npm
json-schema-to-ts
Infer typescript types from your JSON schemas!. Latest version: 3.1.1, last published: a year ago. Start using json-schema-to-ts in your project by running
npm i json-schema-to-ts
. There are 223 other projects in the npm registry using json-schema-to-ts.