tdegrunt / jsonschema

JSON Schema validation
Other
1.83k stars 262 forks source link

Improve error message for invalid (undefined) schema #290

Closed BradPrit09 closed 4 years ago

BradPrit09 commented 5 years ago
  return console.log(console.error(v.validate(doc,**schema**)));

When ever I try to get this schema from NoSql DB preferred :+1: mongoDB ,the validator is throwing error statementing that Cannot read property of _id is .

F:\Data Bucket\node_modules\mongodb\lib\utils.js:97
    process.nextTick(function() { throw err; });
                                  ^

TypeError: Cannot read property 'id' of undefined
    at Validator.validate (F:\Data Bucket\node_modules\jsonschema\lib\validator.js:111:65)
    at F:\Data Bucket\testFolder\json.js:494:48
    at handleCallback (F:\Data Bucket\node_modules\mongodb\lib\utils.js:95:12)
    at F:\Data Bucket\node_modules\mongodb\lib\collection.js:861:5
    at handleCallback (F:\Data Bucket\node_modules\mongodb-core\lib\cursor.js:234:5)
    at nextFunction (F:\Data Bucket\node_modules\mongodb-core\lib\cursor.js:604:5)
    at F:\Data Bucket\node_modules\mongodb-core\lib\cursor.js:538:7
    at queryCallback (F:\Data Bucket\node_modules\mongodb-core\lib\cursor.js:215:5)
    at Callbacks.emit (F:\Data Bucket\node_modules\mongodb-core\lib\topologies\server.js:84:3)
    at Connection.messageHandler (F:\Data Bucket\node_modules\mongodb-core\lib\topologies\server.js:219:23)

But :+1: When I parse the Json data of that document into respective Json Schema and passing it into var schema ,its working but when I fetch the schema(string) that I saved in the sub-sub document and passing as str into validate function its not working

          var str = ' ' ;
          var collection = Database.collection('users');   

          collection.findOne({email:Username},function (err,doc) {
          if (err) throw err;
          {
            usdData=doc.farm;
             for (let index = 0; index < usdData.length; index++)
              {
                console.time("RESPONSE TIME")
                const element =usdData[index].device;
              for (let index2 = 0; index2 < element.length; index2++)
               {
                  str=element[index2].DeviceJson; 
                  // console.log(str); 
                }
              }

          }
            return console.log(console.error(v.validate(doc,str)));

    });
awwright commented 4 years ago

Yeah I would say this sort of error should never happen. I'll take a look at this. Are you able to reduce this to a test case?

awwright commented 4 years ago

This error is probably happening because you're passing undefined as the schema.

awwright commented 4 years ago

Passing an invalid schema (anything but object/boolean) now throws a cleaner error, as of 7e1ace0559249b4bba018803c9eae46e75dbfdbd.

Please open a new issue if you have any further problems, and if possible, provide a code sample with expected and actual output.