Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derivation of json schemas #4

Open
disteph opened this issue Jan 23, 2021 · 1 comment
Open

Derivation of json schemas #4

disteph opened this issue Jan 23, 2021 · 1 comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.

Comments

@disteph
Copy link

disteph commented Jan 23, 2021

Feature request:
I'd be interested in automatically generating a json schema (in the sense of https://json-schema.org) out of an OCaml type definition. A json schema is describing a subset of all jsons and can be seen as a json type or a validity criterion on a json structure. Say if I have an OCaml type ty = ... [@@deriving yojson], the json structures produced by yojson_of_ty, when applied to OCaml values of type ty, would be json structures abiding by the schema.

Ideally the schema would capture exactly the image of yojson_of_ty, but I expect the notion of json schema to be less expressive than OCaml's type system (e.g., I don't think json schemas have polymorphism and I think that in a json schema, all elements of JSON arrays must have the same type --which would be problematic for the encoding of OCaml's tuples as json arrays)...
...so for the moment what would be enough is producing a json schema that captures the image of yojson_of_ty for a simple class of OCaml types ty (no tuples, all constructors have "record-like / named arguments", etc), and perhaps producing a reasonable superset of that image when ty is outside that class.

Has anybody heard of a project doing what I need?

  • Json_schema in ocplib-json-typed has a representation of schemas (which are themselves jsons) but I see no deriver;
  • ATD offers its own notion of json type (from what I understand), whose expressivity may be well-suited to my use case, but the tool seems to offer a one way street towards OCaml code generation, while I need the reverse direction: producing json schemas from types in existing OCaml code;
  • ppx_deriving_yojson seems to be the same kind of ppx deriver as ppx_yojson_conv and, just like Milk, they both seem to offer value-level serialization/deserialization while I need type-level "serialization";
  • ppx_yojson does something different (though very useful).
@github-iron github-iron added the forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system. label Feb 5, 2021
@GoPavel
Copy link

GoPavel commented Mar 30, 2022

Also, there is json-data-encoding. It provides Json_schema module that "contains an OCaml intermediate representation for the JSON schema document grammar description language, along with translators to / from the concrete JSON schema format."

But it cannot autoderive schema from the OCaml datatype as well. I had the same thoughts as yours and decided that the only way is to write own ppx extention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.
Projects
None yet
Development

No branches or pull requests

3 participants