Fwendly function parameters validation library for JS

bower install ducker


Build Status

Ducker is a library created for purpose of function parameters object validation.

bower install ducker --save

<script src="bower_components/ducker/dist/ducker.min.js"></script>

You have a function(params) where params is an object that you would like to validate.

On validation ducker returns array of errors (no exceptions or nulls, these are for pussies) which is empty if everything is ok.

Errors consists paths to invalid param and the name of validator so you can handle it as you like and have pretty error message, or ugly if that's your choice.

Assume you want it to contain some set of params which some of which are required and some of which are supposed to have some type:

    var params = {
        'firstStringParam': 'someString',
        'secondIntParamThatIsRequired': 42,
        'someThirdParamThatWeDontWantValidate': 'anything',
        'someCustomParam': 'customString'

Inside function you describe types of params:

    function myFunction(params) {
        var paramTypes = {
            'firstStringParam': 'string', // built-in 'string' validator
            'secondIntParamThatIsRequired': 'int', // built-in 'int' validator
            // and we don't care about 'someThirdParamThatWeDontWantValidate' param
            'someCustomParam': 'customStringValidator' // 'customStringValidator' is a validator that we define on call or register in ducker with registerValidator(name, validator) function
        var customValidators = {
            'customStringValidator': function(s) { return s === 'customString'; }
        var errors = ducker.validate(params, paramTypes, customValidators);
        // return type [{path: ['path', 'to', 'invalid', 'property'], validation: 'validatorName'}, ...] // if no errors, empty array

It currently uses some non-compatible with old browsers functions so IE >= 9. Your soul shall suffer.