Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern property is not escaped #17

Closed
sanglierquipue opened this issue Jun 23, 2015 · 5 comments
Closed

Pattern property is not escaped #17

sanglierquipue opened this issue Jun 23, 2015 · 5 comments

Comments

@sanglierquipue
Copy link

Hi ! I have an issue with the following code :

var ajvFissiles = new Ajv().compile({
        "type" : "object",
        "patternProperties": {
            "^.+$" : {
                "type" : "object",
                "properties" :  {
                    "unit" : { "type" : "string", "enum" : ["ap", "ac", "ap", "af"]},
                    "density" : { "type" : ["string", "number"]},
                    "compositions" : { "type" : "array"}
                },
                "required" : ["unit", "compositions"],
                "additionalProperties": false
            }
        },
        "additionalProperties" : false
    });

I get the following message :

Error compiling schema, function code:   validate = function (data, dataPath) { 'use strict'; validate.errors = null; var errors = 0;              if ((data && typeof data === "object" && !Array.isArray(data))) {               var errs0 = errors;var valid1 = true; var propertiesSchema0 = validate.schema.properties || {}; for (var key0 in data) { var isAdditional0 = propertiesSchema0[key0] === undefined;  if (isAdditional0) {  if (/^.+$/.test(key0)) isAdditional0 = false;   }  if (isAdditional0) {   valid1 = false;   validate.errors = [ { keyword: 'additionalProperties', dataPath: (dataPath || '') + "['" + key0 + "']", message: 'additional properties NOT allowed'  }]; return false;   break;    } }   if (valid1) {      for (var key0 in data) { if (/^.+$/.test(key0)) {     var data1 = data[key0];    var errs_1 = errors;      if ((data1 && typeof data1 === "object" && !Array.isArray(data1))) {           if (   data1.unit === undefined   ||  data1.compositions === undefined ) {   var err =   { keyword: 'required', dataPath: (dataPath || '') + "['" + key0 + "']", message: 'properties unit, compositions are required'  }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++;  }  else {             var errs1 = errors;var valid2 = true; var propertiesSchema1 = validate.schema.patternProperties.^.+$.properties || {}; for (var key1 in data1) { var isAdditional1 = propertiesSchema1[key1] === undefined;  if (isAdditional1) {   valid2 = false;   var err =   { keyword: 'additionalProperties', dataPath: (dataPath || '') + "['" + key0 + "']['" + key1 + "']", message: 'additional properties NOT allowed'  }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++;   break;    } }   if (valid2) {          var data2 = data1.unit;   if (data2 === undefined) { valid2 = true; } else {     var errs_2 = errors;             var enumSchema2 = validate.schema.patternProperties.^.+$.properties['unit'].enum , valid2 = false;for (var i2=0; i2<enumSchema2.length; i2++) if (equal(data2, enumSchema2[i2])) { valid2 = true; break; } if (!valid2) {   var err =   { keyword: 'enum', dataPath: (dataPath || '') + "['" + key0 + "'].unit", message: 'should be equal to one of values'  }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++;  }      if (errors === errs_2) {     if (typeof data2 !== "string") {   var err =   { keyword: 'type', dataPath: (dataPath || '') + "['" + key0 + "'].unit", message: 'should be string'  }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++;  } }  var valid2 = errors === errs_2;   }     if (valid2) {          var data2 = data1.density;   if (data2 === undefined) { valid2 = true; } else {     var errs_2 = errors;       if (typeof data2 !== "string" && typeof data2 !== "number") {   var err =   { keyword: 'type', dataPath: (dataPath || '') + "['" + key0 + "'].density", message: 'should be string,number'  }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++;  }   var valid2 = errors === errs_2;   }     if (valid2) {            if (data1.compositions === undefined) { valid2 = true; } else {     var errs_2 = errors;       if (!Array.isArray(data1.compositions)) {   var err =   { keyword: 'type', dataPath: (dataPath || '') + "['" + key0 + "'].compositions", message: 'should be array'  }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++;  }   var valid2 = errors === errs_2;   }     }}} }    }   else {   var err =   { keyword: 'type', dataPath: (dataPath || '') + "['" + key0 + "']", message: 'should be object'  }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++;  }      var valid1 = errors === errs_1;     if (!valid1) break;  }  else valid1 = true;  }   }     }   else {   validate.errors = [ { keyword: 'type', dataPath: (dataPath || '') + "", message: 'should be object'  }]; return false;  }      return errors === 0; }    

Do you have any idee ?

@epoberezkin
Copy link
Member

Yes, I can see the issue
Thanks. Will fix today

@epoberezkin
Copy link
Member

It seems like you may be using some "old" version.
The current one will validate the schema before compiling unless you turn it off with options (items in enum should be unique)
Nevertheless, once I've removed this obstacle I've found another bug (different from the one you were getting) - it is fixed now in version 0.5.11

Thanks a lot!

@epoberezkin epoberezkin changed the title Compil Pattern property is not escaped Jun 23, 2015
@epoberezkin
Copy link
Member

By the way, your schema is equivalent to this one (it is simpler and it will validate faster):

{
    "type" : "object",
    "additionalProperties": {
        "type" : "object",
        "properties" :  {
            "unit" : { "type" : "string", "enum" : ["ap", "ac", "af"]},
            "density" : { "type" : ["string", "number"]},
            "compositions" : { "type" : "array"}
        },
        "required" : ["unit", "compositions"]
    }
}

@sanglierquipue
Copy link
Author

Yes you are right ! I had also a bug in my schema
this part was wrong

["ap", "ac", "ap", "af"]

(should be replace by ["mp", "ac", "ap", "af"])

Ok, I will try the schema you propose, THX !

@epoberezkin
Copy link
Member

You're welcome. Please make sure to use the latest version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants