<commit_before>var tv4 = require("./tv4");

exports.validate = function (data) {
    var self = this;

    // check data
    if (!data || !data.data || !data.schema) {
        return console.error("Data provided not valid");
    }

    // check if the required schema was configured
    if (!self._config || !self._config.schema || !self._config.schema[data.schema]) {
        return console.error("Schema not configured");
    }

    // look for a callback
    var callback = data.callback || function () {};

    // validate data
    var schema = self._config.schema[data.schema];
    var valid = tv4.validate(data.data, schema);

    if (valid) {
        self.flow("schema_valid").write(null, {
            data: data.data,
            schema: data.schema
        });

        // send response back via callback
        callback(true);
    } else {
        var error = tv4.error;
        self.flow("schema_invalid").write(null, {
            schema: data.schema,
            error: error
        });

        // send response back via callback
        callback(false, error);
    }<commit_msg>Write response to stream<commit_after>var tv4 = require("./tv4");

function validate (data, callback) {
    var self = this;

    // check data
    if (!data || !data.data || !data.schema) {
        return callback("Data provided not valid");
    }

    // check if the required schema was configured
    if (!self._config || !self._config.schema || !self._config.schema[data.schema]) {
        return callback("Schema not configured");
    }

    // validate data
    var schema = self._config.schema[data.schema];
    var valid = tv4.validate(data.data, schema);

    if (valid) {
        self.flow("schema_valid").write(null, {
            data: data.data,
            schema: data.schema
        });

        // send response back
        callback(null, {
            valid: true
        });
    } else {
        var error = tv4.error;
        self.flow("schema_invalid").write(null, {
            schema: data.schema,
            error: error
        });

        // send response back
        callback(null, {
            valid: false,
            error: error
        });
    }
}

exports.validate = function (data, stream) {
    var self = this;

    validate.call(self, data, function (err, response) {

        // look for a callback if provided
        var callback = data.callback || function () {};
        callback(err, response);

        // write response to stream
        if (stream) {
            stream.write(err, response);
        }
    });
{'fraction_non_alphanumeric': 0.13017982799061767, 'fraction_numerical': 0.003127443315089914, 'mean_word_length': 1.9245714285714286, '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': '3324913', 'n_tokens_mistral': 708, 'n_tokens_neox': 690, 'n_words': 191}