bpdusk / jsonschema

Automatically exported from code.google.com/p/jsonschema
0 stars 0 forks source link

Tuple typing not working. #6

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The following:

{
  "tupleTyping":[2,"a"]
} 

will validate (incorrectly) with the following tupletyping schema:

{
  "type":"object",
  "properties":{
    "tupleTyping":{
      "type":"array",
      "items":[
        {"type":"string"},
        {"type":"number"}
      ] 
    }
  }
} 

I solved that with the following (near line 102):

if (schema.items) {
  if(schema.items instanceof Array) {
    for (var i =0,l=value.length; i < l; i++) {
        errors2.concat(checkProp(value[i],schema.items[i],path,i));
    }
  }
  else {
    for (var i =0,l=value.length; i < l; i++) {
        errors2.concat(checkProp(value[i],schema.items,path,i));
    }   
  }                     
}

And I think the (line 78) following code might be useless, because it looks
like it is from an old tuple typing implementation:

if (type instanceof Array) {
    var unionErrors=[];
    for (var j = 0; j < type.length; j++) // a union type 
        if (!(unionErrors=checkType(type[j],value)).length)
            break;
    if (unionErrors.length)
        return unionErrors;
}

Original issue reported on code.google.com by bruno.p.reis@gmail.com on 4 Dec 2008 at 5:33

GoogleCodeExporter commented 8 years ago
I made some improvements to check optional and additionalParameters issues:
if (schema.items) {
  if(schema.items instanceof Array) {
    for (var k = 0,l=value.length; k < l; k++) {
          if(k < schema.items.length) {
        errors2.concat(checkProp(value[k],schema.items[k],path,k));
      }
      else {
        if(schema.additionalProperties !== undefined) {
          if(schema.additionalProperties === false) {
            addError("The item " + i + "[" + k + "] is not defined in the objTypeDef
and the objTypeDef does not allow additional properties");
          }
          else {
            errors2.concat(checkProp(value[k],schema.additionalProperties,path,k));
          }
        }
      }
    }
    if(value.length < schema.items.length) {
      for (var k = value.length; k < schema.items.length; k++) {
        errors2.concat(checkProp(undefined,schema.items[k],path,k));
      }
    }
  }
  else { ...

Original comment by bruno.gradua@gmail.com on 4 Dec 2008 at 7:34

GoogleCodeExporter commented 8 years ago

Original comment by kris...@gmail.com on 16 Apr 2009 at 8:36