kriszyp/json-schema

null is allowed as a valid object

Opened this issue · 1 comments

"null" is allowed in places where an "object" is required, even one with required properties. Here's a test case:

/*
 * test case for json-schema validation issue with "null" objects.
 */
var assert = require('assert');
var jsonschema = require('json-schema');

var schema, report;

/*
 * BUG: This reports as valid, but I think it should not.  The RFC is not
 * super-clear on this, but it seems to me that 'object' was not intended to
 * include null, since there's a separate null type that's explicitly for
 * creating nullable objects.
 */
schema = { 'type': 'object' };
console.log('attempting to validate "null" as an object');
report = jsonschema.validate(null, schema);
console.log(report);

/*
 * "null" definitely doesn't have the required property "someprop", but this
 * reports as valid, too.
 */
schema = {
    'type': 'object',
    'properties': {
        'someprop': {
        'type': 'object',
        'required': true
    }
    }
};
console.log(
    'attempting to validate "null" as an object with a required property');
report = jsonschema.validate(null, schema);
console.log(report);

/*
 * As expected, an empty object is not valid for this schema.
 */
console.log(
    'attempting to validate "{}" as an object with a required property');
report = jsonschema.validate({}, schema);
console.log(report);

Here's the output on my system:

dap@sharptooth jst $ uname -a
Darwin sharptooth.local 12.5.0 Darwin Kernel Version 12.5.0: Sun Sep 29 13:33:47 PDT 2013; root:xnu-2050.48.12~1/RELEASE_X86_64 x86_64
dap@sharptooth jst $ node -v
v0.10.18
dap@sharptooth jst $ node issue-null.js 
attempting to validate "null" as an object
{ valid: true, errors: [] }
attempting to validate "null" as an object with a required property
{ valid: true, errors: [] }
attempting to validate "{}" as an object with a required property
{ valid: false,
  errors: 
   [ { property: 'someprop',
       message: 'is missing and it is required' } ] }

Yup, I see this too.