Commit 487a61ba authored by Jaap Karan Singh's avatar Jaap Karan Singh

Allow multiple validators in the `validate` property

parent 95fabe82
......@@ -4,6 +4,15 @@
exports.validate = function customValidate(instance, schema, opt, ctx) {
if (instance || schema.required) {
return schema.validate(instance, schema, opt, ctx);
var fn = schema.validate;
if (Array.isArray(fn)) {
for (var i = 0, len = fn.length; i < len; i += 1) {
var res = fn[i](instance, schema, opt, ctx);
if (res) { return res; }
}
} else {
return fn(instance, schema, opt, ctx);
}
}
};
\ No newline at end of file
{
"name": "jsonschema-extra",
"version": "1.0.2",
"version": "1.1.0",
"description": "Extends jsonschema with custom types and attributes",
"main": "index.js",
"directories": {
......
......@@ -45,6 +45,8 @@ Custom validator when `jsonschema` just isn't what you are after. This one is my
> One of the features of jsonschema is that you can share it across client and server which is exactly what I do! However, if your schema contains a validate attribute it will no longer be valid json. This is not a big issue if you are ok for client side code to not contain all your validation logic if schema is sent from the server.
**Single validator**
```js
var schema = {
validate: function(instance, schema, options) {
......@@ -55,6 +57,25 @@ var schema = {
};
```
**Multiple validators**
```js
var schema = {
validate: [
function(instance, schema, options) {
// do your crazy validation here
// return an error string if not valid
return 'is not a valid instance';
},
function(instance, schema, options) {
// do your crazy validation here
// return an error string if not valid
return 'is not a valid instance';
}
]
};
```
#### Types
......@@ -102,6 +123,9 @@ $ npm test
Changelog
---------
### v1.1.0 (18 Sep 2014)
- Allow multiple validators in the `validate` property
### v1.0.2 (30 Sep 2014)
- Fixed so it works on non-Windows systems
- Removed mongodb dev dependency
......
......@@ -116,6 +116,48 @@ describe('#jsonschema-extra', function() {
}
},
pass: true
},
{
instance: 3,
schema: {
validate: [
function() {
return 'is not valid';
},
function() {
return;
}
]
},
pass: false
},
{
instance: 4,
schema: {
validate: [
function() {
return;
},
function() {
return 'is not valid';
}
]
},
pass: false
},
{
instance: 5,
schema: {
validate: [
function() {
return;
},
function() {
return;
}
]
},
pass: true
}
]
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment