Z
Zod4mo ago
squibler.

squibler. - Hi folks. I'm looking to validate a...

Hi folks. I'm looking to validate that a user input is not in a blacklist - like for example swear words. Enum does this for allowed strings, but I'm struggling to find something that will work for disallowed strings. I have tried a regex - but my datasource has over 2000 disallowed words and regex would be too slow. Basically I'm looking for something like:
import { z } from 'zod'

const blacklisted = ["swearword", "cussword", "..."]

z.string().not(blacklisted, 'Naughty, naughty')
import { z } from 'zod'

const blacklisted = ["swearword", "cussword", "..."]

z.string().not(blacklisted, 'Naughty, naughty')
Solution:
Using this now: ```ts import { blacklist } from '~/blacklist'; export const notBlacklisted = (input: string): boolean => {...
Jump to solution
2 Replies
Solution
squibler.
squibler.4mo ago
Using this now:
import { blacklist } from '~/blacklist';
export const notBlacklisted = (input: string): boolean => {
return !blacklist.find((element) => element === input);
};
import { blacklist } from '~/blacklist';
export const notBlacklisted = (input: string): boolean => {
return !blacklist.find((element) => element === input);
};
with
import { z } from 'zod'
const schema = z.string().refine(notBlacklisted);
import { z } from 'zod'
const schema = z.string().refine(notBlacklisted);
Svish
Svish4mo ago
Yeah, refine is the way to go here