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

Failure to validate union properties that are schemas #19

Open
ggoodman opened this issue Jun 15, 2012 · 6 comments
Open

Failure to validate union properties that are schemas #19

ggoodman opened this issue Jun 15, 2012 · 6 comments

Comments

@ggoodman
Copy link
Contributor

I've been forced to switch libraries because the current structure of the code would require sweeping changes to allow support for union properties wherein on of the possible options in the union is a schema itself.

The code assumes that union properties are a list of types while the spec states that these can be types or entire schemas.

@Marak
Copy link
Contributor

Marak commented Jun 16, 2012

What is a union properties? I don't understand what you are trying to say.

@pksunkara
Copy link
Contributor

Is it this?

{"type":["string","number"]}

The property can be either string or number?

@ggoodman
Copy link
Contributor Author

Hi PK, Marak, sorry about the vague bug report. Here is some more context.

Consider a Post that can optionally have a source which is defined by a schema itself. Basically, I want to validate some json such that the source property is either not present (null) or conforms to a schema. To achieve this, I referred to section 5.1 of the draft.

Here is an example json-schema (v3) to describe a Post:

{
  "type": "object",
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "body": {
      "type": "string",
      "required": true
    },
    "source": {
      "type": [
        {
          "type": "null"
        }, {
          "type": "object",
          "properties": {
            "name": {
              "type": "string",
              "required": true
            },
            "url": {
              "type": "string",
              "required": true
            }
          }
       ]
    }
  }
}

@Marak
Copy link
Contributor

Marak commented Jun 16, 2012

I see. If you need a 100% compliant validator, you might want to check out https://github.com/garycourt/JSV

@ggoodman
Copy link
Contributor Author

Hi Marak, this is what I'm using and seems to be more or less API compatible: https://github.com/kriszyp/json-schema/blob/master/lib/validate.js

@elad
Copy link

elad commented Nov 13, 2014

I think this is already supported. Here's some code:

var revalidator = require('revalidator');

var schema = {
    properties: {
        title: {
            type: 'string',
            required: true
        },

        body: {
            type: 'string',
            required: true
        },

        source: {
            type: ['object', 'null'],
            properties: {
                name: {
                    type: 'string',
                    required: true
                },
                url: {
                    type: 'string',
                    required: true
                }
            }
        }
    }
};

// good: missing, null, or conforming to schema.
var object1 = {
    title: 'a title',
    body: 'this is the body'
}, object2 = {
    title: 'a title',
    body: 'this is the body',
    source: null
}, object3 = {
    title: 'a title',
    body: 'this is the body',
    source: {
        name: 'foo bar',
        url: 'http://www.example.com'
    }
};

// bad: empty, not conforming to schema.
var object4 = {
    title: 'a title',
    body: 'this is the body',
    source: {}
}, object5 = {
    title: 'a title',
    body: 'this is the body',
    source: {
        name: 'foo bar',
        a: 1337
    }
}, object6 = {
    title: 'a title',
    body: 'this is the body',
    source: {
        a: 1337,
        url: 'http://www.example.com'
    }
};

console.log(revalidator.validate(object1, schema));
console.log(revalidator.validate(object2, schema));
console.log(revalidator.validate(object3, schema));
console.log(revalidator.validate(object4, schema));
console.log(revalidator.validate(object5, schema));
console.log(revalidator.validate(object6, schema));

Output:

$ node opt
{ valid: true, errors: [] }
{ valid: true, errors: [] }
{ valid: true, errors: [] }
{ valid: false,
  errors: 
   [ { attribute: 'required',
       property: 'source.name',
       expected: true,
       actual: undefined,
       message: 'is required' },
     { attribute: 'required',
       property: 'source.url',
       expected: true,
       actual: undefined,
       message: 'is required' } ] }
{ valid: false,
  errors: 
   [ { attribute: 'required',
       property: 'source.url',
       expected: true,
       actual: undefined,
       message: 'is required' } ] }
{ valid: false,
  errors: 
   [ { attribute: 'required',
       property: 'source.name',
       expected: true,
       actual: undefined,
       message: 'is required' } ] }
$ 

I think this issue can be closed as well.

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

4 participants