I've been tasked to add a filtering function for forbidden usernames. I've created a huge list of these names and split everything into two parts, one containing a list of exact matches, the other with a phrase/word match list.
The exact match works as intended (e.g. "admin", "adminello" - is allowed), but the phrase/word match only works in a way if that word is found within a sentence, e.g. "sht hello". I should also make it work dashes, "sht-hello" and even combined string like "shthshthellosh*t".
Should I also divide the list, because atm it's one JSON file like:
{
"admin": 1,
"sh*t": 2
}
Helper function (badNamesList - JSON file)
module.exports = (options = {}) => {
return async context => {
const argumentsList = _.get(context, 'arguments', []);
const username = _.size(argumentsList) > 1 ? _.get(argumentsList[1], 'username') : null;
if (username) {
const exactMatchList = _.map(badNamesList, (name, key) => {
if (name === 1) return key;
});
const phraseMatchList = _.map(badNamesList, (name, key) => {
if (name === 2) return key;
});
if (_.includes(exactMatchList, _.toLower(username))) {
throw new errors.BadRequest({ fieldErrors: { username: 'forbiddenUsername' } });
}
if (_.some(phraseMatchList, name => _.includes(_.map(_.split(username, /-| /), _.toLower), name))) {
throw new errors.BadRequest({ fieldErrors: { username: 'forbiddenUsername' } });
}
}
return context;
};
};