Reputation: 79
I'm a complete newbie to JSON Schema Validator, but I think is very powerful. However, I just can't validate one JSON.
This is my Schema
{
title: "Example Schema",
type: "object",
properties: {
original_image:{
type: "object",
properties: {
temp_id: {type: "string"},
url: {type: "string"},
scale:{
type: "object",
properties:{
new_width: {type: "number"},
new_height: {type: "number"}
},
required:["new_width","new_height"]
}
},
required:["url","temp_id","scale"]
}
},
required:["image"]
}
And this is the actual JSON:
{
"original_image": {
"temp_id": "this is my id",
"scale": {
"new_width": null,
"new_height": 329
}
}
}
So as you can see the "url" property from "original_image" is not there, but the validation returns true! And, for "new_width" I set the value to null... and again passes validation, so I don't know what I'm doing wrong.
Upvotes: 1
Views: 2273
Reputation: 1495
If you put your condition as required:["url","temp_id","scale"]
, then, all three properties are required in the payload, but url
seems to be missing in your payload.
If you want url
to be optional, then, dont put it in the required constraint.
The validator also gives back an error message.It returns missing parameters/properties if that is the case.
Upvotes: 1
Reputation: 15016
It seems to be working fine. The console logs errors correctly. This is my index.js
var Validator = require('jsonschema').Validator;
var v = new Validator();
var instance = {
"original_image": {
"temp_id": "this is my id",
"scale": {
"new_width": null,
"new_height": 329
}
}
};
var schema = {
title: "Example Schema",
type: "object",
properties: {
original_image:{
type: "object",
properties: {
temp_id: {type: "string"},
url: {type: "string"},
scale:{
type: "object",
properties:{
new_width: {type: "number"},
new_height: {type: "number"}
},
required:["new_width","new_height"]
}
},
required:["url","temp_id","scale"]
}
},
required:["image"]
};
console.log(v.validate(instance, schema));
Upvotes: 2