Sha256: 2f8c3237596b33c4f23109f43e847d0271d0f513abbff57e0084fe2430286415

Contents?: true

Size: 757 Bytes

Versions: 1

Compression:

Stored size: 757 Bytes

Contents

# rubocop:disable Style/AsciiComments
module Emoji
  module Validator
    # Validate an attribute against emojis
    #
    #   class Person < ApplicationRecord
    #     validates :first_name, no_emoji: true
    #   end
    #
    #   person = Person.new(first_name: "John", last_name: "😃")
    #   person.valid? #true
    #   person.first_name = "😃"
    #   person.valid? #false
    #
    class NoEmojiValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        return if value.match(Unicode::Emoji::REGEX_VALID).nil?
        record.errors.add(attribute, :has_emojis)
      end
    end
  end
end

ActiveModel::Validations::NoEmojiValidator = Emoji::Validator::NoEmojiValidator
# rubocop:enable Style/AsciiComments

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
emoji-validator-0.1.3 lib/emoji/validator/no_emoji_validator.rb