Skip to content

Instantly share code, notes, and snippets.

@thetutlage
Last active September 7, 2021 03:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thetutlage/e076f45245b6b27b3cc50b93c87bd954 to your computer and use it in GitHub Desktop.
Save thetutlage/e076f45245b6b27b3cc50b93c87bd954 to your computer and use it in GitHub Desktop.
AdonisJS custom validation rule for disallowing emojis
// Save inside contracts/validator.ts
declare module '@ioc:Adonis/Core/Validator' {
interface Rules {
disallowEmoji(): Rule
}
}
// Save inside start/validator.ts
import { validator } from '@ioc:Adonis/Core/Validator'
validator.rule(
'disallowEmoji',
(value, {}, { pointer, arrayExpressionPointer, errorReporter }) => {
if (typeof value !== 'string') {
return
}
if (/<% RGI_Emoji %>|\p{Emoji_Presentation}|\p{Emoji}\uFE0F|\p{Emoji_Modifier_Base}/gu.test(value)) {
errorReporter.report(
pointer,
'disallowEmoji',
'Emojis are not allowed',
arrayExpressionPointer
)
}
}
)
const { password } = await request.validate({
schema: schema.create({
password: schema.string({ trim: true }, [
rules.minLength(8),
rules.maxLength(28),
rules.confirmed(),
rules.disallowEmoji(),
]),
}),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment