-
Notifications
You must be signed in to change notification settings - Fork 82
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
Comments
What is a |
Is it this? {"type":["string","number"]} The property can be either string or number? |
Hi PK, Marak, sorry about the vague bug report. Here is some more context. Consider a Here is an example json-schema (v3) to describe a {
"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
}
}
]
}
}
} |
I see. If you need a 100% compliant validator, you might want to check out https://github.com/garycourt/JSV |
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 |
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. |
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.
The text was updated successfully, but these errors were encountered: