<commit_before>// Backbone.Validation v0.3.1
//
// Copyright (C)2011 <NAME>
// Distributed under MIT License
//
// Documentation and full license availabe at:
// http://github.com/thedersen/backbone.validation

Backbone.Validation = (function(Backbone, _, undefined) {
    var defaultSelector = 'name';
    var getValidatedAttrs = function(model){
        var validatedAttrs = {};
        for (var attr in model.validation) {
            if(model.validation.hasOwnProperty(attr)){
                validatedAttrs[attr] = undefined;
            }
        }
        return validatedAttrs;      
    };
    
    var getValidators = function(model, attr) {
        var validation = model.validation[attr],
            validators = [];

        if (_.isFunction(validation)) {
            return validation;
        } else if(_.isString(validation)) {
            return model[validation];
        } else {
            for (var validator in validation) {
                if (validator !== 'msg' && validation.hasOwnProperty(validator)) {
                    validators.push({
                        fn: Backbone.Validation.validators[validator],
                        val: validation[validator],
                        msg: validation.msg
                    });
                }
            }
            return validators;
        }
    };

    var validateAttr = function(model, attr, value) {
        var validators = getValidators(model, attr),
            error = '',
            validator, 
            result;

        if (_.isFunction(validators)) {
            return validators.call(model, value, attr);
        } else {
            for (var i = 0; i < validators.length; i++) {
                validator = validators[i];
                result = validator.fn(value, attr, validator.val, model);
                if(result === false) {
                    return;
                }
                else if (result) {
                    error += validator.msg || result;
                }
            };
            return error;
        }
    };
    
    return {
        version: '0.3.1',

        setDefaultSelector: function(selector){
            defaultSelector = selector;
        },
        
        bind: function(view, options) {
            options = options || {};
            var model = view.model,
                delayTrigger,
                selector = options.selector || defaultSelector,
                validFn = options.valid || Backbone.Validation.callbacks.valid,
                invalidFn = options.invalid || Backbone.Validation.callbacks.invalid,
                isValid = _.isUndefined(model.validation);
            
            model.validate = function(attrs) {
                if(!attrs){
                    return model.validate.call(model, _.extend(getValidatedAttrs(model), model.toJSON()));
                }
                
                var result = [], invalidAttrs = [];
                isValid = true;

                for (var changedAttr in attrs) {
                    var error = validateAttr(model, changedAttr, attrs[changedAttr]);
                    if (error) {
                        result.push(error);
                        invalidAttrs.push(changedAttr);
                        isValid = false;
                        invalidFn(view, changedAttr, error, selector);
                    } else {
                        validFn(view, changedAttr, selector);
                    }
                }

                if (isValid) {
                    for (var validatedAttr in model.validation) {
                        if (_.isUndefined(attrs[validatedAttr]) && validateAttr(model, validatedAttr, model.get(validatedAttr))) {
                            isValid = false;
                            break;
                        }
                    }
                }
                <commit_msg>Added support for arrays of validators and arrays of returned errors<commit_after>// Backbone.Validation v0.3.1
//
// Copyright (C)2011 <NAME>
// Distributed under MIT License
//
// Documentation and full license availabe at:
// http://github.com/thedersen/backbone.validation

Backbone.Validation = (function(Backbone, _, undefined) {
    var defaultSelector = 'name';
    var getValidatedAttrs = function(model){
        var validatedAttrs = {};
        for (var attr in model.validation) {
            if(model.validation.hasOwnProperty(attr)){
                validatedAttrs[attr] = undefined;
            }
        }
        return validatedAttrs;      
    };
    
    /*var getValidators = function(model, attr) {
        var validation = model.validation[attr],
            validators = [];

        if (_.isFunction(validation)) {
            return validation;
        } else if(_.isString(validation)) {
            return model[validation];
        } else {
            for (var validator in validation) {
                if (validator !== 'msg' && validation.hasOwnProperty(validator)) {
                    validators.push({
                        fn: Backbone.Validation.validators[validator],
                        val: validation[validator],
                        msg: validation.msg
                    });
                }
            }
            return validators;
        }
    }; */

    var getValidators = function(model, attr) {

        var validationElements = model.validation, validators = [], validationElement, validation;

        if( ! _.isArray(validationElements)) {
            validationElements = [validationElements];
        }

        for (var i = 0, l = validationElements.length; i < l; i++) {
            validationElement = validationElements[i];

            validation = validationElement[attr];

            if (_.isFunction(validation)) {
                validators.push( validation);
            } else if(_.isString(validation)) {
                validators.push( model[validation]);
            } else {
                for (var validator in validation) {
                    if (validator !== 'msg' && validation.hasOwnProperty(validator)) {
                        validators.push({
                            fn: Backbone.Validation.validators[validator],
                            val: validation[validator],
                            msg: validation.msg
                        });
                    }
                }
            }
        }

        return validators;
    };

    var validateAttr = function(model, attr, value) {
        var validatorsArray = getValidators(model, attr),
            error = '',
            errors = [],
            validator, 
            result;

        for( var index=0, count=validatorsArray.length; index<count; index++)
        {
            validator = validatorsArray[index];

            if (_.isFunction(validator)) {
                result = validator.call(model, value, attr);
                if (result) errors.push(result);
            } else {
                result = validator.fn(value, attr, validator.val, model);
                if (result) {
                    errors.push( validator.msg || result);
                }
            }
        }

        if (errors.length == 0) return false;

        return errors;
    };
    
    return {
        version: '0.3.1',

        setDefaultSelector: function(selector){
            defaultSelector = selector;
        },
        
        bind: function(view, options) {
            options = options || {};
            var model = view.model,
                delayTrigger,
                selector = options.selector || defaultSelector,
                validFn = options.valid || Backbone.Validation.callbacks.valid,
                invalidFn = options.invalid || Backbone.Validation.callbacks.invalid,
                isValid = _.isUndefined(model.validation);
            
            model.validate = function(attrs) {
                if(!attrs){
                    return model.validate.call(model, _.extend(getValidatedAttrs(model), model.toJSON()));
                }
                
                var result = [], invalidAttrs = [];
                isValid = true;

                for (var changedAttr in attrs) {
                    var errors = validateAttr(model, changedAttr, attrs[changedAttr]);
                    if (errors) {
                        for( var index=0, count=errors.length; index<count; index++)
                        {
                            result.push(errors[index]);
                            invalidAttrs.push(changedAttr);
                            isValid = false;

                            if( errors.length == 1) {
                                invalidFn(view, changedAttr, errors[0], selector);
                            } else {
                                invalidFn(view, changedAttr, errors, selector);
                            }
                        }
                    } else {
                        validFn(view, changedAttr, selector);
                    }
                }

                if (isValid) {
                    for (var validatedAttr in model.validation) {
                        if (_.isUndefined(attrs[validatedAttr]) && validateAttr(model, validatedAttr, model.get(validatedAttr))) {
                            isValid = false;
                            break;
                        }
                    }
                }
                
{'fraction_non_alphanumeric': 0.09864850484197084, 'fraction_numerical': 0.002873257422581675, 'mean_word_length': 1.2760959069992734, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3181160', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2138, 'n_words': 472}