<commit_before>
var moment = require('moment'),
    validator = require('mysql-validator');
var query = require('./query'),
    sql = require('./sql');


exports.value = function (column, value) {
    
    if (value === '') {
        if (column.defaultValue) {
            value = column.defaultValue;
        }
        if (!value) {
            return value;
        }
    }
    
    switch (true) {
        case column.control.date:
            // mysql.typeCast is disabled
            if (!value) return null;
            if (moment(value, 'YYYY-MM-DD').isValid()) {
                var formatted = moment(value).format('YYYY-MM-DD');
            }
            return formatted;

        case column.control.multiple:
            return setActiveMultiple(column, value);
        
        case column.control.select:
            return setActiveSingle(column, value);

        default:
            return value;
    }
}

function setActiveSingle (column, value) {
    if (!column.value) return value;
    var rows = column.value;
    for (var i=0; i < rows.length; i++) {
        if (parseInt(rows[i]['__pk']) === parseInt(value)) {
            rows[i].selected = true;
            return rows;
        }
    }
    return rows;
}

function setActiveMultiple (column, value) {
    if (!column.value) return value;
    var rows = column.value;
    for (var i=0; i < rows.length; i++) {
        var pk = parseInt(rows[i]['__pk']);
        value = (value instanceof Array) ? value : [value];
        for (var j=0; j < value.length; j++) {
            if (pk === parseInt(value[j])) {
                rows[i].selected = true;
                break;
            }
        }
    }
    return rows;
}

exports.validate = function (column, value) {
    
    if (value === '' || value === 'NULL' || value === null
        || (value instanceof Array && !value.length)) {
        // should check for column.defaultValue too
        if (!column.allowNull) {
            return new Error('Column '+column.name+' cannot be empty.');
        } else {
            return null;
        }
    }

    return validator.check(value, column.type);
}

exports.test = {
    setActiveSingle: setActiveSingle,
    setActiveMultiple: setActiveMultiple
};
<commit_msg>Temporary fix for date types when type casting is on<commit_after>
var moment = require('moment'),
    validator = require('mysql-validator');
var query = require('./query'),
    sql = require('./sql');


exports.value = function (column, value) {
    
    if (value === '') {
        if (column.defaultValue) {
            value = column.defaultValue;
        }
        if (!value) {
            return value;
        }
    }
    
    switch (true) {
        case column.control.date:
            // mysql.typeCast is disabled
            if (!value) return null;
            if (moment(value).isValid()) {
                var formatted = moment(value).format('YYYY-MM-DD');
            }
            return formatted;

        case column.control.multiple:
            return setActiveMultiple(column, value);
        
        case column.control.select:
            return setActiveSingle(column, value);

        default:
            return value;
    }
}

function setActiveSingle (column, value) {
    if (!column.value) return value;
    var rows = column.value;
    for (var i=0; i < rows.length; i++) {
        if (parseInt(rows[i]['__pk']) === parseInt(value)) {
            rows[i].selected = true;
            return rows;
        }
    }
    return rows;
}

function setActiveMultiple (column, value) {
    if (!column.value) return value;
    var rows = column.value;
    for (var i=0; i < rows.length; i++) {
        var pk = parseInt(rows[i]['__pk']);
        value = (value instanceof Array) ? value : [value];
        for (var j=0; j < value.length; j++) {
            if (pk === parseInt(value[j])) {
                rows[i].selected = true;
                break;
            }
        }
    }
    return rows;
}

exports.validate = function (column, value) {
    
    if (value === '' || value === 'NULL' || value === null
        || (value instanceof Array && !value.length)) {
        // should check for column.defaultValue too
        if (!column.allowNull) {
            return new Error('Column '+column.name+' cannot be empty.');
        } else {
            return null;
        }
    }
    if (column.control.date) {
        if (moment(value).isValid()) {
            value = moment(value).format('YYYY-MM-DD');
        }
    }

    return validator.check(value, column.type);
}

exports.test = {
    setActiveSingle: setActiveSingle,
    setActiveMultiple: setActiveMultiple
};

{'fraction_non_alphanumeric': 0.13781982369382928, 'fraction_numerical': 0.0012900451515803052, 'mean_word_length': 1.8574938574938575, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '2932919', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1314, 'n_words': 340}