Don't raise error when empty community emoji setting submitted (#15723)
* Don't raise NoMethodError when validating emoji settings Cargo culted the unique cross model slug validator setup (which is why value is called name here in the validatable). Don't raise an error when validating a nil emoji, and assert nil and empty string are permitted values. This expects no non-emoji characters (so an empty string would be permitted), and nil should be permitted. I believe this might have been introduced by the string settings cleaner (exchanging "" for nil in form inputs). * Update app/validators/emoji_only_validator.rb guard for value, and then validate value
This commit is contained in:
parent
7b5d90d252
commit
4b93684dab
2 changed files with 61 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ class EmojiOnlyValidator < ActiveModel::EachValidator
|
|||
DEFAULT_MESSAGE = "contains non-emoji characters or invalid emoji".freeze
|
||||
|
||||
def validate_each(record, attribute, value)
|
||||
return unless value
|
||||
return if value.gsub(EmojiRegex::RGIEmoji, "").blank?
|
||||
|
||||
record.errors.add(attribute, options[:message] || DEFAULT_MESSAGE)
|
||||
|
|
|
|||
60
spec/validators/emoji_only_validator_spec.rb
Normal file
60
spec/validators/emoji_only_validator_spec.rb
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe EmojiOnlyValidator do
|
||||
subject(:record) { validatable.new.tap { |m| m.name = name } }
|
||||
|
||||
let(:validatable) do
|
||||
Class.new do
|
||||
def self.name
|
||||
"👍"
|
||||
end
|
||||
include ActiveModel::Validations
|
||||
attr_accessor :name
|
||||
|
||||
def initialize
|
||||
@enforce_validation = true
|
||||
end
|
||||
|
||||
attr_accessor :enforce_validation
|
||||
alias_method :enforce_validation?, :enforce_validation
|
||||
|
||||
validates :name, emoji_only: true, if: :enforce_validation?
|
||||
end
|
||||
end
|
||||
|
||||
context "when if option is false" do
|
||||
let(:name) { "not-an-emoji" }
|
||||
|
||||
before { record.enforce_validation = false }
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
|
||||
context "when name includes non-emoji" do
|
||||
let(:name) { "not-an-emoji" }
|
||||
|
||||
it { is_expected.not_to be_valid }
|
||||
end
|
||||
|
||||
context "when name is an emoji" do
|
||||
let(:name) { "🍀" }
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
|
||||
context "when name is nil" do
|
||||
let(:name) { nil }
|
||||
|
||||
it "does not raise no method error" do
|
||||
expect { record.valid? }.not_to raise_error
|
||||
end
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
|
||||
context "when name is empty" do
|
||||
let(:name) { "" }
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue