0
0
Fork 0

Add coverage for LanguageValidator (#25593)

This commit is contained in:
Matt Jankowski 2023-07-28 17:12:09 -04:00 committed by GitHub
parent ad81be6c8e
commit 6602edf064
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 6 deletions

View file

@ -4,18 +4,20 @@ class LanguageValidator < ActiveModel::EachValidator
include LanguagesHelper
def validate_each(record, attribute, value)
record.errors.add(attribute, :invalid) unless valid?(value)
@value = value
record.errors.add(attribute, :invalid) unless valid_locale_value?
end
private
def valid?(str)
if str.nil?
def valid_locale_value?
if @value.nil?
true
elsif str.is_a?(Array)
str.all? { |x| valid_locale?(x) }
elsif @value.is_a?(Array)
@value.all? { |x| valid_locale?(x) }
else
valid_locale?(str)
valid_locale?(@value)
end
end
end