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

Callback not invoked for undefined enum with custom loader #43

Open
enicholson opened this issue Sep 30, 2014 · 2 comments
Open

Callback not invoked for undefined enum with custom loader #43

enicholson opened this issue Sep 30, 2014 · 2 comments

Comments

@enicholson
Copy link

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
Copy link
Owner

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
Copy link
Author

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 [email protected] 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
#43 (comment).

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

No branches or pull requests

2 participants