mirror of
https://github.com/mastodon/mastodon.git
synced 2025-01-08 19:35:11 +01:00
Added validator for extra profile field values with empty name
This commit is contained in:
parent
8770905186
commit
4da9244584
@ -118,6 +118,7 @@ class Account < ApplicationRecord
|
|||||||
validates :display_name, length: { maximum: DISPLAY_NAME_LENGTH_LIMIT }, if: -> { local? && will_save_change_to_display_name? }
|
validates :display_name, length: { maximum: DISPLAY_NAME_LENGTH_LIMIT }, if: -> { local? && will_save_change_to_display_name? }
|
||||||
validates :note, note_length: { maximum: NOTE_LENGTH_LIMIT }, if: -> { local? && will_save_change_to_note? }
|
validates :note, note_length: { maximum: NOTE_LENGTH_LIMIT }, if: -> { local? && will_save_change_to_note? }
|
||||||
validates :fields, length: { maximum: DEFAULT_FIELDS_SIZE }, if: -> { local? && will_save_change_to_fields? }
|
validates :fields, length: { maximum: DEFAULT_FIELDS_SIZE }, if: -> { local? && will_save_change_to_fields? }
|
||||||
|
validates_with EmptyProfileFieldNamesValidator, if: -> { local? && will_save_change_to_fields? }
|
||||||
with_options on: :create do
|
with_options on: :create do
|
||||||
validates :uri, absence: true, if: :local?
|
validates :uri, absence: true, if: :local?
|
||||||
validates :inbox_url, absence: true, if: :local?
|
validates :inbox_url, absence: true, if: :local?
|
||||||
@ -300,7 +301,7 @@ class Account < ApplicationRecord
|
|||||||
|
|
||||||
if attributes.is_a?(Hash)
|
if attributes.is_a?(Hash)
|
||||||
attributes.each_value do |attr|
|
attributes.each_value do |attr|
|
||||||
next if attr[:name].blank?
|
next if attr[:name].blank? && attr[:value].blank?
|
||||||
|
|
||||||
previous = old_fields.find { |item| item['value'] == attr[:value] }
|
previous = old_fields.find { |item| item['value'] == attr[:value] }
|
||||||
|
|
||||||
|
17
app/validators/empty_profile_field_names_validator.rb
Normal file
17
app/validators/empty_profile_field_names_validator.rb
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class EmptyProfileFieldNamesValidator < ActiveModel::Validator
|
||||||
|
def validate(account)
|
||||||
|
return if account.fields.empty?
|
||||||
|
field_names_valid = true
|
||||||
|
account.fields.each_with_index do |field, index|
|
||||||
|
if field.name.blank? && !field.value.blank?
|
||||||
|
field_names_valid = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return if field_names_valid
|
||||||
|
|
||||||
|
account.errors.add(:fields, 'Names of profile fields cannot be empty')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user