Last active
September 7, 2021 03:33
-
-
Save thetutlage/e076f45245b6b27b3cc50b93c87bd954 to your computer and use it in GitHub Desktop.
AdonisJS custom validation rule for disallowing emojis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Save inside contracts/validator.ts | |
declare module '@ioc:Adonis/Core/Validator' { | |
interface Rules { | |
disallowEmoji(): Rule | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
) | |
} | |
} | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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