form submission database censorship

Hello friends! I have some a custom form that submits comments in my site. I’m trying to figure out a way to block/autoremove comments taht contains some selected words. I research a lot but didn’t find the anyone trying to do exactly this. Found some indication that it should be possible through hooks in the database. I was trying to make my own using contain() under a export function databaseName_beforeInsert(item, context) { ). but I’m kinda lost. anybody can share a light over this? Are there some topics about it to indicate?

ps: The code I was trying to got up on my own is so poor and full of wrong “if’s and then{'s” that it isn’t worth to show here.

Thank you verry much for any help!

Hello. @gilaccessoverall . You can try something like this.

export function myCollection_beforeInsert(item, context) {
  const forbiddenWords = ['foo', 'bar', 'baz'];
  const forbiddenWordsAreIncluded = forbiddenWords.filter(word => item.someField.includes(word)).length;
  if (forbiddenWordsAreIncluded) {
    return Promise.reject('Has forbidden words');
  } else {
    return item;
  }
}

Andrii, thank you verry much! this works just fine! I was really with difficult understanding the disposition of things. You teached me a lot with this code, many thanks!