Coder Social home page Coder Social logo

Comments (4)

fge avatar fge commented on July 22, 2024

You are yet another victim of the misunderstanding of how extends works ;)

extends adds constraints to a schema, it does not make any attempts to merge schemas at all.

Both of your schemas expect an object instance. So far so good.

The first schema forbids any other members than "foo" to be present, so, when it see "bar", it complains -- rightfully so.

And the second schema forbids any other members than "bar" to be present, so, when it see "foo", it complains -- rightfully so as well.

This is not a bug: this is how it is supposed to work. And this is why the next proposed specification replaces extends with allOf, which has the same meaning, without the ambiguities. With allOf, your schema becomes:

{
    "allOf": [
        {
            "properties": {
                "foo": {
                    "type": "string"
                }
            },
            "additionalProperties": false
         },
         {
            "properties": {
                "bar": {
                    "type": "string"
                }
            },
            "additionalProperties": false
        }
    ]
}

from json-schema-validator.

bpfoster avatar bpfoster commented on July 22, 2024

Doh, well sorry for polluting your tracker then.
Based on that, is there no way using the current spec to accomplish what I'm describing here (instance of A to allow both "foo" and "bar", but an instance of B to only allow "bar")? I love the reuse that $ref gives me, but it sounds like I can't utilize that if I want to restrict the additional properties?

from json-schema-validator.

fge avatar fge commented on July 22, 2024

Doh, well sorry for polluting your tracker then.

No problem... Actually, it is a recurring demand, and JSON Schema currently has no mechanism for that :/ And given the set of current keywords, I don't see how this is possible.

Last time someone had this problem on the group, I suggested that he just ignored properties he didn't want instead of using additionalProperties... I know of no way to achieve what you want. What would actually be needed is indeed schema merging, and it leads to a whole new class of problems!

from json-schema-validator.

bpfoster avatar bpfoster commented on July 22, 2024

Ok, thanks! If it's possible to bring something like this in for consideration on the spec, I would certainly love to see it!

from json-schema-validator.

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.