Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

required fails if used in the global space #34

Closed
jdarling opened this Issue · 3 comments

3 participants

@jdarling

From JSON-schema.org sample:

{
    "title": "Example Schema",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        }
    },
    "required": ["firstName", "lastName"]
}

If you use this schema and don't pass in a firstName or lastName it still validates. Looks like the current code base looks for each property to have a required on each.

If this isn't supposed to support the json-schema.org definition then this is something to note some place :)

@tJener

The spec described at json-schema.org is most likely the latest draft, v4. For the most part, it seems that this library supports v3, and as such the definition for the required keyword is described as

This attribute indicates if the instance must have a value, and not
be undefined. This is false by default, making the instance
optional.

http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.7

@tdegrunt
Owner

This library indeed supports v3. It has some v4 additions (in master).
I've changed the README to make this more clear.

http://json-schema.org/ is not really clear on this.

@tdegrunt tdegrunt closed this
@jdarling

Thanks for the update. As I just ran across JSON-Schema I didn't realize that v4 was a newer addition version. I have found a work around that allows for the v4 requires to work, just in case someone else wants to use that style as well;

var enhanceRequired = function(root){
  var required;
  if(typeof(root)!=='object'){
    return;
  }
  if((root.required instanceof Array)||(root.required&&root.required.length)){
    required = Array.prototype.slice.call(root.required);
  }else{
    required = [];
  }
  if(root.properties){
    var keys = Object.keys(root.properties), value;
    keys.forEach(function(key){
      value = root.properties[key];
      enhanceRequired(value);
      if(required.indexOf(key)>-1){
        value.required = true;
      }
    });
  }
}
enhanceRequired(schema);

Long and short of it, this runs through each each value in the passed in schema and if it find the required array outside the properties it adds the required flag to each property. Probably a better way of doing this, but it is a quick work around that works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.