<commit_before>'use strict';

const Hoek = require('hoek');
const Magic = require('file-type');

const internals = {};

internals.validate = function (payload, options, next) {

    Object.keys(Object.assign({}, payload)).forEach((key) => {

        const parameter = payload[key];

        if (!(parameter instanceof Buffer)) {
            return;
        }

        const type = Magic(parameter);
        const list = options.whitelist || [];

        if (!type) {
            return next({ name: key, description: 'type is unknown' });
        }

        if (Hoek.contain(list, type.ext) || Hoek.contain(list, type.mime)) {
            return;
        }

        return next({ name: key, description: 'type is not allowed' });
    });

    next();
};

exports.validate = function (payload, options, next) {

    internals.validate(payload, options, (err) => {

        if (err) {
            const msg = `child \"${err.name}\" fails because [\"${err.name}\" ${err.description}]`;
            const error = new Error(msg);
            error.name = 'ValidationError';
            error.details = [{
                message: err.description,
                path: err.name,
                context: {
                    key: err.name
                }
            }];

            return next(error);
        }

        next(null, payload);
    });
};
<commit_msg>Fix logic to handle multiple invalid file types<commit_after>'use strict';

const Hoek = require('hoek');
const Magic = require('file-type');

const internals = {};

internals.validate = function (payload, options, next) {

    const keys = Object.keys(Object.assign({}, payload));

    for (let i = 0; i < keys.length; ++i) {
        const key = keys[i];
        const parameter = payload[key];

        if (!(parameter instanceof Buffer)) {
            continue;
        }

        const type = Magic(parameter);
        const list = options.whitelist || [];

        if (!type) {
            return next({ name: key, description: 'type is unknown' });
        }

        if (Hoek.contain(list, type.ext) || Hoek.contain(list, type.mime)) {
            continue;
        }

        return next({ name: key, description: 'type is not allowed' });
    };

    next();
};

exports.validate = function (payload, options, next) {

    internals.validate(payload, options, (err) => {

        if (err) {
            const msg = `child \"${err.name}\" fails because [\"${err.name}\" ${err.description}]`;
            const error = new Error(msg);
            error.name = 'ValidationError';
            error.details = [{
                message: err.description,
                path: err.name,
                context: {
                    key: err.name
                }
            }];

            return next(error);
        }

        next(null, payload);
    });
};

{'fraction_non_alphanumeric': 0.14673721340388007, 'fraction_numerical': 0.0003527336860670194, 'mean_word_length': 1.7996051332675223, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '6132060', 'n_tokens_mistral': 804, 'n_tokens_neox': 764, 'n_words': 205}