<commit_before>
var $U = require('underscore');
var Class = require('better-js-class');


module.exports = function() {
    var simpleError = function(code) {
        return {
            'status': 'error',
            'error': {
                'code': code
            }
        };
    };

    var ok = function() {
        return {
            'status': 'ok'
        };
    };

    var _validate = function(cfg) {
        if (cfg['guard']) {
            return ok();
        } else {
            return validatorError(cfg['message']);
        }
    };

    var validatorError = function(validatorError) {
        return {
            'status': 'error',
            'error': {
                'code': cls.EXCEPTION.VALIDATOR_ERROR,
                'validator': validatorError
            }
        };
    };

    var objectFieldError = function(fieldErrors) {
        return {
            'status': 'error',
            'error': {
                'code': cls.EXCEPTION.OBJECT_FIELD_ERROR,
                'fields': fieldErrors
            }
        }
    };

    var unionVariantError = function(variantError) {
        return {
            'status': 'error',
            'error': {
                'code': cls.EXCEPTION.UNION_VARIANT_ERROR,
                'variants': variantError
            }
        }
    };

    var arrayElementError = function(elementError) {
        return {
            'status': 'error',
            'error': {
                'code': cls.EXCEPTION.ARRAY_ELEMENT_ERROR,
                'element': elementError
            }
        }
    };

    var cls = Class({
        _init: function(cfg) {
            this._typeDefs = cfg['typeDefs'];
        },

        typeCheck: function(value, type) {
            var me = this;

            if (!type) {
                return ok();
            }

            var nullable = type['nullable'];

            if (nullable) {
                if (value == null) {
                    return ok();
                } else {
                    return me._typeCheck(value, type);
                }
            } else {
                if (value == null) {
                    return simpleError(cls.EXCEPTION.MANDATORY_INPUT_IS_NULL_ERROR);
                } else {
                    return me._typeCheck(value, type);
                }
            }
        },

        _lookupType: function(alias) {
            return this._typeDefs[alias];
        },

        _typeCheck: function(value, type) {
            var me = this;

            var typeTag = type['type'];
            var validator = type['validator'];

            var validate = function(validator, value) {
                if (validator) {
                    return validator(value);
                } else {
                    return ok();
                }
            };

            switch(typeTag) {
                case 'String':
                    if (!$U.isString(value)) {
                        return simpleError(cls.EXCEPTION.TYPE_ERROR_NOT_A_STRING);
                    } else {
                        return validate(validator, value);
                    }
                case 'Number':
                    value = parseFloat(value);
                    if (!$U.isNumber(value)) {
                        return simpleError(cls.EXCEPTION.TYPE_ERROR_NOT_A_NUMBER);
                    } else {
                        return validate(validator, value);
                    }
                case 'Boolean':
                    if (!$U.isBoolean(value)) {
                        return simpleError(cls.EXCEPTION.TYPE_ERROR_NOT_A_BOOLEAN);
                    } else {
                        return validate(validator, value);<commit_msg>* Enhance type checking of 'Number'<commit_after>
var $U = require('underscore');
var Class = require('better-js-class');


module.exports = function() {
    var simpleError = function(code) {
        return {
            'status': 'error',
            'error': {
                'code': code
            }
        };
    };

    var ok = function() {
        return {
            'status': 'ok'
        };
    };

    var _validate = function(cfg) {
        if (cfg['guard']) {
            return ok();
        } else {
            return validatorError(cfg['message']);
        }
    };

    var validatorError = function(validatorError) {
        return {
            'status': 'error',
            'error': {
                'code': cls.EXCEPTION.VALIDATOR_ERROR,
                'validator': validatorError
            }
        };
    };

    var objectFieldError = function(fieldErrors) {
        return {
            'status': 'error',
            'error': {
                'code': cls.EXCEPTION.OBJECT_FIELD_ERROR,
                'fields': fieldErrors
            }
        }
    };

    var unionVariantError = function(variantError) {
        return {
            'status': 'error',
            'error': {
                'code': cls.EXCEPTION.UNION_VARIANT_ERROR,
                'variants': variantError
            }
        }
    };

    var arrayElementError = function(elementError) {
        return {
            'status': 'error',
            'error': {
                'code': cls.EXCEPTION.ARRAY_ELEMENT_ERROR,
                'element': elementError
            }
        }
    };

    var cls = Class({
        _init: function(cfg) {
            this._typeDefs = cfg['typeDefs'];
        },

        typeCheck: function(value, type) {
            var me = this;

            if (!type) {
                return ok();
            }

            var nullable = type['nullable'];

            if (nullable) {
                if (value == null) {
                    return ok();
                } else {
                    return me._typeCheck(value, type);
                }
            } else {
                if (value == null) {
                    return simpleError(cls.EXCEPTION.MANDATORY_INPUT_IS_NULL_ERROR);
                } else {
                    return me._typeCheck(value, type);
                }
            }
        },

        _lookupType: function(alias) {
            return this._typeDefs[alias];
        },

        _typeCheck: function(value, type) {
            var me = this;

            var typeTag = type['type'];
            var validator = type['validator'];

            var validate = function(validator, value) {
                if (validator) {
                    return validator(value);
                } else {
                    return ok();
                }
            };

            switch(typeTag) {
                case 'String':
                    if (!$U.isString(value)) {
                        return simpleError(cls.EXCEPTION.TYPE_ERROR_NOT_A_STRING);
                    } else {
                        return validate(validator, value);
                    }
                case 'Number':
                    value = parseFloat(value);
                    if (($U.isNaN(value)) || (!$U.isNumber(value))) {
                        return simpleError(cls.EXCEPTION.TYPE_ERROR_NOT_A_NUMBER);
                    } else {
                        return validate(validator, value);
                    }
                case 'Boolean':
                    if (!$U.isBoolean(value)) {
                        return simpleError(cls.EXCEPTION.TYPE_ERROR_NOT_A_BOOLEAN);
                    } else {
                        return validate(validator, value);
{'fraction_non_alphanumeric': 0.10967568294980487, 'fraction_numerical': 0.0, 'mean_word_length': 1.1167758473369411, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5475835', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1844, 'n_words': 363}