Created
May 4, 2016 23:24
-
-
Save ddunlop/bea6f84c1b708aff89c53cb36a36371a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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