about summary refs log tree commit diff
path: root/app/validators
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2022-09-20 23:51:21 +0200
committerGitHub <noreply@github.com>2022-09-20 23:51:21 +0200
commit50948b46aabc0756d85bc6641f0bd3bcc09bf7d4 (patch)
tree19dd9e761aec2a33d6bc3c11b47673da40cd965b /app/validators
parent882e54c78678bd4247d70fe5b04571543769bcee (diff)
Add ability to filter followed accounts' posts by language (#19095)
Diffstat (limited to 'app/validators')
-rw-r--r--app/validators/language_validator.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/app/validators/language_validator.rb b/app/validators/language_validator.rb
new file mode 100644
index 000000000..b723e1a40
--- /dev/null
+++ b/app/validators/language_validator.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class LanguageValidator < ActiveModel::EachValidator
+  include LanguagesHelper
+
+  def validate_each(record, attribute, value)
+    record.errors.add(attribute, :invalid) unless valid?(value)
+  end
+
+  private
+
+  def valid?(str)
+    if str.nil?
+      true
+    elsif str.is_a?(Array)
+      str.all? { |x| valid_locale?(x) }
+    else
+      valid_locale?(str)
+    end
+  end
+end