silfverorg / duns.js

Javascript schema validator
5 stars 2 forks source link

Add conditional validations #34

Open silfverstrom opened 9 years ago

silfverstrom commented 9 years ago

Would be nice if duns schemas could have conditional validations, like this

var schema = Duns.object.keys({
    age : Duns.number().min(0).condition(
        'above18', 
        Duns.number().min(18)
    )
})

if (schema.validate() === false) return; 
var str = schema.validate('above18') ? 'Above 18' : 'No more than 18';

With this functionality you can easily remove a lot of nested conditional validation, by creating more complex schemas.