validatorJS

a basic validate utility to check and execute the corresponding callbacks.

by pharzan Tinati

JavaScript

var v = (function V() {
    if (!(this instanceof V)) {
        return new V();
    }
    var _results = [];
    var _errors = [];
    var _failed = false;

    this.validate = function(result, onFail, onPass) {
        _failed = false;
        var failCallback,
            passCallback;
        for (idx in arguments) {

            var firstArg = arguments[idx][0] || false,
                secondArg = arguments[idx][1] || null,
                thirdArg = arguments[idx][2] || null;


            if (_isExecArray(arguments[idx])) {
                failCallback = firstArg;
                passCallback = secondArg;

            } else if (Array.isArray(arguments[idx])) {
                _validate(firstArg, secondArg, thirdArg);

            } else
                _validate(false, 'format mismatch', null);
        };
        _runCallback(failCallback, passCallback);
        return _getErrorsThenReset();
    };

    _validate = function(result, onFail, onPass) {
        var r;

        if (result) {
            if (typeof onPass === 'function') {
                r = onPass.call(this, this);

                if (typeof r !== undefined)
                    _updateErrors(r);

            } else if (typeof onPass === 'string') {
                _updateErrors(onPass);
                return onFail;
            } else
                _updateErrors(true);

        } else if (!result) {

            _failed = true;
            if (typeof onFail === 'function') {
                r = onFail.call(this, this);
                if (typeof r !== undefined)
                    _updateErrors(r);
            } else if (typeof onFail === 'string') {
                _updateErrors(onFail);
                return onFail;
            } else
                _updateErrors(false);
        }

    };

    _runCallback = function(fail, pass) {

        if ((typeof fail == 'function') || (typeof pass == 'function')) {
            if (_failed)
                fail();
            else if (!_failed)
...