Generate JSON Schema from dhall type

I have a hypothetical use case where I want to:

  • have some kind of dhall type / schema (let’s call it Foo)
  • write instances of this type in dhall (as in let instance = Foo::{ ... })
  • generate a JSON schema for this type as well

I want both the outputs (as JSON/YAML) and the JSON schema, because I want to build a kubernetes CustomResource representing Foo, which I’d generate using a dhall pipeline, but I others would also publish (and not all would be using dhall).

There’s been some talk of generating dhall schemas from JSON schemas, but is the opposite possible?

I imagine you could easily write a dhall type for JSON schemas, i.e. allow writing JSON Schemas in dhall. But I want a single source which can be used to derive both a a JSON schema and a dhall type.

1 Like

@timbertson: I like this idea! It seems doable, at least for the simple case where everything is “inline” (i.e. intermediate schemas are not named in the JSON schema).

I’d also be happy with a third type - i.e. there’s some “schema for Foo” dhall term, which allows me to both generate JSON from a Foo instance and also generate a JSON schema while asserting that it matches the Foo type. It doesn’t seem possible to do that in dhall though, since it’d need recursion and probably the ability to turn a text value into a record key e.g. from "foo TextSchema", constructing{ foo: Text }`

But thinking further about it, using dhall types doesn’t feel like enough power – if you generate a schema from a raw dhall type, there’s no real place I can see to put extra annotations (validations that dhall’s type system can’t represent). I don’t need those, but if it’s supposed to be a generic tool it seems a shame to rule it out.

Which actually makes me come full circle to the thought that a tool which generates dall types from JSON schemas is going to cover more use cases :thinking: . I mean, you could write the “JSON Schema” in dhall form rather than unstructured JSON, but in practice it’d be able to parse raw JSONSchema (json-to-dhall ./schema < json | json-schema-to-dhall).

Which I’m guessing is sounding very similar to the dhall-kubernetes stuff?

@timbertson: Your latter idea sounds similar in spirit to OpenAPI, which seems like the successor to JSON schema based on what little I know:

Perhaps we could generate Dhall types from OpenAPI schemas, which I believe has also been requested in other contexts.