No release in over 3 years
A Rails engine that provides validators to check if strings contain emojis using the unicode-emoji gem.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 7.0
 Project Readme

EmojiValidator

A Rails engine that provides validators for emoji strings using the unicode-emoji gem.

Installation

Add this line to your application's Gemfile:

gem "emoji_validator"

And then execute:

bundle install

Usage

Default: Single Emoji Only

By default, the field must contain exactly one emoji (no text, no multiple emojis):

class Reaction < ApplicationRecord
  validates :emoji, emoji: true
end
reaction = Reaction.new

reaction.emoji = "😀"      # ✓ Valid
reaction.emoji = "👋🏽"     # ✓ Valid (with skin tone)
reaction.emoji = "Hello 😀" # ✗ Invalid - has text
reaction.emoji = "😀😁"    # ✗ Invalid - multiple emojis

Options

allow_text: true

Allow text alongside the emoji (still only one emoji allowed):

class Message < ApplicationRecord
  validates :content, emoji: { allow_text: true }
end
message.content = "Hello 😀 World"  # ✓ Valid
message.content = "😀"              # ✓ Valid
message.content = "😀😁"            # ✗ Invalid - multiple emojis

allow_multiple: true

Allow multiple emojis (no text allowed):

class StickerSet < ApplicationRecord
  validates :emojis, emoji: { allow_multiple: true }
end
sticker.emojis = "😀😁🎉"     # ✓ Valid
sticker.emojis = "😀"          # ✓ Valid
sticker.emojis = "😀 Hello"    # ✗ Invalid - has text

Both Options

Allow multiple emojis with text:

class Post < ApplicationRecord
  validates :content, emoji: { allow_text: true, allow_multiple: true }
end
post.content = "🎉 Party time! 🎊"  # ✓ Valid
post.content = "Hello World"        # ✗ Invalid - no emoji

allow_blank / allow_nil

Allow empty strings or nil values:

class OptionalReaction < ApplicationRecord
  validates :emoji, emoji: { allow_blank: true, allow_nil: true }
end

Custom Error Message

class Reaction < ApplicationRecord
  validates :emoji, emoji: { message: "pick one emoji" }
end

Disallowing Emojis

In addition to requiring emojis, you can also validate that fields do not contain emojis.

no_emoji - Validate single attributes

class Person < ApplicationRecord
  validates :first_name, no_emoji: true
end
person.first_name = "John"      # ✓ Valid
person.first_name = "😃John"    # ✗ Invalid - contains emoji
person.first_name = "Jo😃hn"    # ✗ Invalid - contains emoji
Custom Error Message
class Person < ApplicationRecord
  validates :first_name, no_emoji: { message: "cannot contain emojis" }
end

NoEmojiAnywhereValidator - Validate all string/text columns

Automatically apply no-emoji validation to all string and text columns in your model:

class Person < ApplicationRecord
  include EmojiValidator::NoEmojiAnywhereValidator
end
person = Person.new(first_name: "😃", last_name: "😃")
person.valid? # false
person.errors.count # 2 (errors on both first_name and last_name)

Emoji Support

This gem uses the unicode-emoji gem which supports:

  • Basic Emoji (😀, 🎉, etc.)
  • Emoji with skin tones (👋🏽, etc.)
  • Emoji sequences (flags, keycaps, ZWJ sequences)
  • And more!

See the unicode-emoji documentation for more details.

License

Apache-2.0