Skip to content

Instantly share code, notes, and snippets.

@ddunlop
Created May 4, 2016 23:24
Show Gist options
  • Save ddunlop/bea6f84c1b708aff89c53cb36a36371a to your computer and use it in GitHub Desktop.
Save ddunlop/bea6f84c1b708aff89c53cb36a36371a to your computer and use it in GitHub Desktop.
'use strict';
var swiz = require('swiz'),
Chain = swiz.Chain,
Valve = swiz.Valve,
o = swiz.struct.Obj,
f = swiz.struct.Field;
Valve.addChainValidator('isHigh', 'description', function(value, baton, callback) {
if(value > baton) {
callback(null, value);
return;
}
callback('value is not higher then baton');
});
var def = [o('Node', {
fields: [
f('low', {
val: new Chain().isInt().range(0, Infinity)
}),
f('high', {
val: new Chain().isInt().range(0, Infinity).custom('isHigh')
})
]})];
var validity = swiz.defToValve(def),
schema = validity.Node,
v = new Valve(schema),
payload = {
low: 1,
high: 10
};
v.baton = payload.low;
v.check(payload, function(err, cleaned) {
if(err) {
console.log('error:', err);
return;
}
console.log('cleaned:', cleaned);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment