natesilva / jayschema

[Unmaintained] - A comprehensive JSON Schema validator for Node.js
BSD 3-Clause "New" or "Revised" License
217 stars 22 forks source link

Callback not invoked for undefined enum with custom loader #43

Open enicholson opened 10 years ago

enicholson commented 10 years ago

I ran into a case where an invalid schema was used that had an enum property with the array undefined. JaySchema didn't throw an exception or invoke the callback.

Here's a quick example to illustrate the problem:

var JaySchema = require('jayschema');
var instance = "hello";
var schema = { enum : undefined };

function loader(ref, callback) {
    console.log('loader referenced');
    callback('The schema referenced, ' + ref + ', could not be found.');
}

var js = new JaySchema(loader);  // if no loader is specified I get a synchronous exception
js.validate(instance, schema, function(errs) {
    if (errs) { console.error('async validation failed!', errs); }  // never called
    else { console.log('async validation OK!'); }  // also never called
});

console.log('validation started');

With 0.3.1 I get a stack trace on the console, but with 0.2.7 I didn't get anything at all (so at least that's an improvement).

natesilva commented 10 years ago

Unfortunately the schema itself does not get validated.

The original thinking was that schemas would be valid/tested so validating the schema itself would not be worth the performance hit. But JaySchema strives for correctness, even at the expense of speed, and it’s obviously not cool to throw there. So that may have to change (or have automatic schema validation be an option in a future version).

In the meantime, you can do something like this to validate the schema before validating the instance:

js.validate(schema, 'http://json-schema.org/draft-04/schema#');

(The draft-04 schema is built-in so this does not have to go out and load it from somewhere.)

enicholson commented 10 years ago

Thanks Nate. I'll add in calls to validate like you mentioned.

Would it make sense to add some generic error handling around the validation method in Jayschema so the callback could at least be invoked with something like "Unexpected error"?

Cheers!

On Wed, Oct 1, 2014 at 1:45 AM, Nate Silva notifications@github.com wrote:

Unfortunately the schema itself does not get validated.

The original thinking was that schemas would be valid/tested so validating the schema itself would not be worth the performance hit. But JaySchema strives for correctness, even at the expense of speed, and it’s obviously not cool to throw there. So that may have to change (or have automatic schema validation be an option in a future version).

In the meantime, you can do something like this to validate the schema before validating the instance:

js.validate(schema, 'http://json-schema.org/draft-04/schema#');

(The draft-04 schema is built-in so this does not have to go out and load it from somewhere.)

— Reply to this email directly or view it on GitHub https://github.com/natesilva/jayschema/issues/43#issuecomment-57421613.