pandastrike/jsck

TypeError with Nested Schema, Required, and Nulls

Closed this issue · 11 comments

I ran into an issue while validating with nested schemas. The below code executes and returns an invalid result (if my understanding is correct, [object Object] !== [object Null]).

However, if I add a required tag to my userSettingsSchema ...
"required": ["language"],
... then the code results in ...
TypeError: Cannot read property 'language' of null

I'm assuming both scenarios should result in the same "invalid" return, or is the proper practice to wrap the validate call in a try/catch block?

'use strict';

var JSCK = require('jsck');

var userSettingsSchema = {
    "type": "object",
    "properties": {
        "language": {
            "type": "string"
        }
    }
};

var userSchema = {
    "type": "object",
    "required": ["fullName"],
    "properties": {
        "fullName": {
            "type": "string"
        },
        "settings": {
            "$ref": "#/definitions/userSettings"
        }
    },
    "definitions": {
        "userSettings": userSettingsSchema
    }
};

var jsck = new JSCK.draft4(userSchema);
var user = { fullName: 'Homer Simpson', settings: null };

var results = jsck.validate(user);
console.log(results.errors || 'Mistakes were made');

is the proper practice to wrap the validate call in a try/catch block?

No, if you have to do that, then JSCK has a bug.

With node 0.12.7, using your script against master, I get this output:

[ { schema: 
     { pointer: '#/definitions/userSettings/type',
       attribute: 'type',
       definition: 'object' },
    document: { pointer: '#/settings', value: null } } ]

With the langage required tag? (The script was the working example).

For reference, I'm running node 0.12.1

I reproduced the problem you saw when setting 'required'. Bug fix shortly.

I can't reproduce the other problem you mentioned, yet.

The exception was happening because the implementation of 'required' in draft4 was not checking to see if the input document was an object.

2741e25#diff-c86eed14718d02722159b14e6d7813d3L16

Here's the test I adapted from your script:

https://github.com/pandastrike/jsck/blob/94-bug/test/draft4/adhoc.coffee#L35-L57

It passes for me on that branch.

I can't reproduce the other problem you mentioned, yet.

I think there's only the one issue, unless I'm wrong on the expected result of this test.

Ah. I misunderstood your report.

My bad. Too many re-writes :oP

I've monkey-patched my copy and so-far-so-good. I'll look forward to the npm update. Thanks for the (ridiculously fast) response!

published version 0.2.9

Thanks for the (ridiculously fast) response!

You're welcome. You happened to catch me at exactly the right time.