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

null is allowed as a valid object #46

Open
davepacheco opened this issue Mar 18, 2014 · 1 comment
Open

null is allowed as a valid object #46

davepacheco opened this issue Mar 18, 2014 · 1 comment

Comments

@davepacheco
Copy link

"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' } ] }
@coryvirok
Copy link

Yup, I see this too.

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