Code Monkey home page Code Monkey logo

Comments (9)

jaysw avatar jaysw commented on May 17, 2024

+1 for this issue -- I expected the first scenario above to work also. @tdegrunt Do you agree here?

from jsonschema.

tdegrunt avatar tdegrunt commented on May 17, 2024

I can see the benefit, but I'm having trouble locating information on this. I've checked http://json-schema.org/latest/json-schema-core.html#anchor7 . This project aims to be a faithful follower of the json-schema.org. If you can find any pointers/info, please let me know. /cc @ACubed

from jsonschema.

PauloASilva avatar PauloASilva commented on May 17, 2024

@tdegrunt I am not familiar with JSON Schema but I was reading some examples and I am not sure whether I missunderstood how to use the "required" keyword.

Now I am confused because I think I can accomplish what I was looking for doing something like

"use strict";

var Validator = require('jsonschema').Validator;
var v = new Validator();

var subSchema = {
    "id": "/subSchema",
    "type": "object",
    "properties": {
        "prop1": { "type": "string" },
        "prop2": { "type": "string" }
    },
    "required": [ "prop1", "prop2" ]
};

var schema = {
    "id": "/schema",
    "type": "object",
    "properties": {
        "prop1": { "$ref": "/subSchema"},
        "prop2": { "type": "string" }
    },
    "required": [ "prop1", "prop2" ]
};

var anotherSchema = {
    "id": "/schema",
    "type": "object",
    "properties": {
        "prop1": { "$ref": "/subSchema"},
        "prop2": { "type": "string" }
    },
    "required": [ "prop2" ]
};

var p = {
    "prop1": {
        "prop1": "Hello",
        "prop2": "World"
    },
    "prop2": "Hello World"
};

var p1 = {
    "prop2": "Hello World"
};

v.addSchema(subSchema, '/subSchema');
console.log(v.validate(p, schema));
console.log(v.validate(p1, schema));
console.log(v.validate(p, anotherSchema));
console.log(v.validate(p1, anotherSchema));

@jaysw can you test if this approach fits your use case?

from jsonschema.

Prestaul avatar Prestaul commented on May 17, 2024

I don't think that required:true on a property is a defined feature of json schema... The required: [] array of required properties is and works fine with this library today although both seem to work under some circumstances.

from jsonschema.

PauloASilva avatar PauloASilva commented on May 17, 2024

@Prestaul I do agree. I don't know how I didn't notice the required: [] at first glance.

from jsonschema.

Prestaul avatar Prestaul commented on May 17, 2024

@PauloASilva, you were right that this library seems to support required:true in some circumstances and it is demonstrated in the example in the readme. @tdegrunt, would it be wise to not feature this feature in the readme?

from jsonschema.

awwright avatar awwright commented on May 17, 2024

required:true is how JSON Schema draft-3 was defined, so it's allowed in the library for historical reasons.

In the next draft, we'll likely be abandoning the keyword altogether and getting "requiredProperties" in addition to the list of optional "properties".

from jsonschema.

awwright avatar awwright commented on May 17, 2024

@PauloASilva Does #100 I merged from @Prestaul fix your issue? If so, feel free to close this. Thanks!

from jsonschema.

PauloASilva avatar PauloASilva commented on May 17, 2024

Documentation update is enough to clarify how to deal with required. I'm closing this issue.
Thanks,

from jsonschema.

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.