Giter Club home page Giter Club logo

Comments (11)

jakevdp avatar jakevdp commented on July 18, 2024

If you don't use additionalProperties: false on the anyOf schema, but instead use it in each of the components of the anyOf, does that work?

Agreed on the lack of expressible inheritance: that's a real point of weakness.

from ts-json-schema-generator.

kanitw avatar kanitw commented on July 18, 2024

The end of this page is probably relevant:

https://json-schema.org/draft-06/json-schema-release-notes.html

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024

If you don't use additionalProperties: false on the anyOf schema, but instead use it in each of the components of the anyOf, does that work?

Actually, the problem is when you have additionalProperties: false on all of the components. Imagine one with {a: number, b: number} and one with {c: number, d: number}. Now, an object with c will violate the first and one with a will violate the second (each object individually does not allow additional properties).

from ts-json-schema-generator.

jakevdp avatar jakevdp commented on July 18, 2024

Maybe I'm not understanding... here's a concrete example where instances pass even with additionalProperties: false:

import jsonschema

schema = {
    'anyOf': [
        {'$ref': '#/definitions/A'},
        {'$ref': '#/definitions/B'}
    ],
    'definitions': {
        'A': {
            'properties': {
                'a': {'type': 'number'},
                'b': {'type': 'number'}
            },
            'additionalProperties': False
        },
        'B': {
            'properties': {
                'c': {'type': 'number'},
                'd': {'type': 'number'}
            },
            'additionalProperties': False
        }
    }
}

jsonschema.validate({'a': 1, 'b': 2}, schema)  # passes
jsonschema.validate({'c': 1, 'd': 2}, schema)  # passes

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024

hmm, I tried the schema below in https://www.jsonschemavalidator.net/.

{
  "$ref": "#/definitions/BC",
  "$schema": "http://json-schema.org/draft-06/schema#",
  "definitions": {
    "B": {
      "additionalProperties": false,
      "properties": {
        "b": { "type": "number" },
        "x": { "type": "number" }
      },
      "type": "object"
    },
    "BC": {
      "allOf": [{"$ref": "#/definitions/B" },
                { "$ref": "#/definitions/C" }]
    },
    "C": {
      "additionalProperties": false,
      "properties": {
        "c": { "type": "number" },
        "x": { "type": "number" }
      },
      "type": "object"
    }
  }
}

Screen Shot 2019-03-25 at 23 15 30

from ts-json-schema-generator.

jakevdp avatar jakevdp commented on July 18, 2024

Difference is allOf vs anyOf

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024

Ahh, I missed that. The thing is that intersection types need allOf.

from ts-json-schema-generator.

kayahr avatar kayahr commented on July 18, 2024

I have to admit I'm not using Vega or Vega-Lite, I just use the schema generator. But i want to comment on it anyway: In my opinion it would be very sad if you abandon JSON schemas because I guess we would then loose an important driving force in the typescript-to-json-schema world. So out of selfish reasons let me use my Jedi powers and tell you this blunt lie: There IS no alternative to JSON schema out there, don't look for it! Instead keep up the good work on the schema generator, please! :-)

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024

Thank you for the kind words @kayahr. I don't think you have anything to worry about.

Btw, I think the issue we are discussing here with intersection types will be resolved with draft 8: json-schema-org/json-schema-spec#656.

from ts-json-schema-generator.

rijenkii avatar rijenkii commented on July 18, 2024

As of draft 2019-09 unevaluatedProperties was officially added.

Example schema for (A | B) & (C | D):

{
  "$ref": "#/$defs/root",
  "$defs": {
    "root": {
      "allOf": [
        {
          "oneOf": [
            { "$ref": "#/$defs/a" },
            { "$ref": "#/$defs/b" },
          ],
        },
        {
          "oneOf": [
            { "$ref": "#/$defs/c" },
            { "$ref": "#/$defs/d" },
          ]
        },
      ],
      "unevaluatedProperties": false,
    },
    "a": {
      "type": "object",
      "properties": { "a": { "type": "string" } },
      "required": ["a"],
    },
    "b": {
      "type": "object",
      "properties": { "b": { "type": "string" } },
      "required": ["b"],
    },
    "c": {
      "type": "object",
      "properties": { "c": { "type": "string" } },
      "required": ["c"],
    },
    "d": {
      "type": "object",
      "properties": { "d": { "type": "string" } },
      "required": ["d"],
    },
  }
}

You can test this schema on https://json-everything.net/json-schema/.
Official json-schema blog post mentioning unevaluatedProperties: https://json-schema.org/blog/posts/modelling-inheritance.

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024

Oh cool. Thanks for the link. It would be nice to support that pattern for inheritance but it also looks tricky as we would need to enumerate all implementing classes. Definitely a bigger refactor.

from ts-json-schema-generator.

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.