jsontypedef / json-typedef-spec

The JSON Type Definition specification and official test suite
17 stars 4 forks source link

question: can JTD self-describe ? #9

Closed jeremiegirault closed 3 years ago

jeremiegirault commented 3 years ago

Hello,

I'm trying to produce some tools for our company using JTD as a basis and wondering : Is it possible to describe JTD schemas using JTD ? Apparently not because discriminated schemas must hold a discriminator field. Do you intend to introduce a discriminator for each schema sub-type to allow JTD to be self-describing ?

Thank you,

ucarion commented 3 years ago

Hi, if your question is: is there a JTD schema for describing correct JTD schemas? Then the answer is indeed "no". There are a few reasons for this:

  1. You can't validate that the value of ref points to a real definition. This is a limitation you'll find in most schema languages.
  2. You can't validate that only particular combination of keywords are in use. I believe this is what you're referring to.
  3. You can't validate that discriminator mappings don't also have the discriminator tag as a property.
  4. You can't validate that properties and optionalProperties don't share properties.

On point (2): I think what you're saying is that if JTD schemas had to have some kind of form property telling you what "form" the schema is, then this could be addressed. Since there are other things we can't validate anyway, it didn't seem worth it to make schemas more verbose without actually fixing the "self-validation" problem.

I agree this seems weird. This limitation was known from the beginning of the design of JTD, and it's called out in RFC 8927's introduction section:

JTD is intentionally designed as a rather minimal schema language. Thus, although JTD can describe some categories of JSON, it is not able to describe its own structure; this document uses Concise Data Definition Language (CDDL) [RFC8610] to describe JTD's syntax. By keeping the expressiveness of the schema language minimal, JTD makes code generation and standardized error indicators easier to implement.

https://tools.ietf.org/html/rfc8927#section-1

The way I often see it, JTD is meant to describe common, mostly mundane business data models. A schema language is not a common sort of thing, and is far more complex than the average business data model. I didn't want to make JTD more powerful (and thus harder to implement and build tooling for) or more awkward to write (i.e. forcing schemas to have a discriminator) just for the elegance of being able to self-describe.

As one way of addressing this problem, every implementation of JTD listed in https://jsontypedef.com/docs/implementations/ implements all the rules to say whether the schema is correct. Would one of those work for you? If not, could you expand on why having self-describing JTD schemas would be useful for you?

jeremiegirault commented 3 years ago

Hello,

I was just toying with the thing and see what could be done in terms of customization. Thank you for the clear and complete answer.

Have a good day,

forivall commented 3 years ago

for search reference, and a summary: JTD does not have a meta-schema (in comparison to JSON Schema, which does have a self-describing meta-schema)

I would like to see a JSON schema definition of JTD, at least, for tooling support in things like vscode (if i do ever use JTD, i'll write one, and probably an extension for vscode)

jeremiegirault commented 3 years ago

@forivall That's a great idea ! I made this draft : https://gist.github.com/jeremiegirault/5afa4d52e4f8ef6ea63b01caf27d9f24 I just drafted this in 5minutes; It's not tested I may look at it this week. It would be possible to publish an elaborate version to json schema store with filetypes *.jtd.json to make it available to IDE (I'm using vscode). What do you think @ucarion ?