Git Product home page Git Product logo

Comments (2)

ucarion avatar ucarion commented on August 18, 2024

Hi, thanks for taking the time to report this bug report!

This is indeed a bug. The schema you provided is in fact not a valid JSON Type Definition schema -- the schema at /properties/bar/mapping/foo is of the empty form, but mapping schemas must be of the properties form -- but jtd-codegen should be providing a clear error message to this effect, and certainly shouldn't be panicking.

A bit more details on what schemas can appear inside mappings is covered here: https://jsontypedef.com/docs/jtd-in-5-minutes/

You can only use properties / optionalProperties / additionalProperties in the schemas you put directly in mapping. You can’t use any other kind of schema, otherwise things would become ambiguous.

Admittedly, that language is a bit loose, and I understand how you would think that using no keywords at all would be kosher. The RFC is a bit more explicit: https://tools.ietf.org/html/rfc8927#section-2

   ; discriminator describes the "discriminator" schema form.
   ;
   ; There are additional constraints on this form that cannot be
   ; expressed in CDDL. Section 2.2.8 describes these additional
   ; constraints in detail.
   discriminator = (
     discriminator: tstr,
     ; Note well: this rule is defined in terms of the "properties"
     ; CDDL rule, not the "schema" CDDL rule.
     mapping: { * tstr => { properties } }
     shared,
   )

But again: jtd-codegen should not be leaving you out to dry like this.

In the immediate term, I suspect you'll have success if you alter your schema to be a valid JTD schema. For instance, just adding "properties": {} to foo would make your schema valid. This schema seems to work, at least for me:

{
    "properties": {
        "tag": {
            "enum": [
                "foo"
            ]
        },
        "bar": {
            "discriminator": "tag",
            "mapping": {
                "foo": {
                    "properties": {}
                }
            }
        }
    }
}

In the longer term, the jtd-codegen bug here is that the tool should validate a schema's correctness before attempting to generate code from it, and produce an error message rather than generate code (and ultimately crash).

from json-typedef-codegen.

karvus avatar karvus commented on August 18, 2024

Thank you for the prompt reply, and for taking the time to explain!

from json-typedef-codegen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.