如何验证数组字段的成员?

时间:2011-04-14 20:50:10

标签: ruby-on-rails ruby ruby-on-rails-3 mongodb mongoid

我有这个型号:

class Campaign

  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, :type => String
  field :subdomain, :type => String
  field :intro, :type => String
  field :body, :type => String
  field :emails, :type => Array
end

现在我要验证emails数组中的每封电子邮件是否格式正确。我阅读了Mongoid和ActiveModel :: Validations文档,但我没有找到如何做到这一点。

你能告诉我指针吗?

5 个答案:

答案 0 :(得分:24)

您可以定义自定义ArrayValidator。将以下内容放入app/validators/array_validator.rb

class ArrayValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, values)
    Array(values).each do |value|
      options.each do |key, args|
        validator_options = { attributes: attribute }
        validator_options.merge!(args) if args.is_a?(Hash)

        next if value.nil? && validator_options[:allow_nil]
        next if value.blank? && validator_options[:allow_blank]

        validator_class_name = "#{key.to_s.camelize}Validator"
        validator_class = begin
          validator_class_name.constantize
        rescue NameError
          "ActiveModel::Validations::#{validator_class_name}".constantize
        end

        validator = validator_class.new(validator_options)
        validator.validate_each(record, attribute, value)
      end
    end
  end
end

您可以在模特中使用它:

class User
  include Mongoid::Document
  field :tags, Array

  validates :tags, array: { presence: true, inclusion: { in: %w{ ruby rails } }
end

它将针对array哈希中指定的每个验证程序验证数组中的每个元素。

答案 1 :(得分:13)

Milovan的回答得到了我的支持,但实施有一些问题:

  1. 展平嵌套数组会更改行为并隐藏无效值。

  2. nil字段值被视为[nil],这似乎不正确。

  3. 所提供的示例presence: true会生成NotImplementedError错误,因为PresenceValidator未实现validate_each

  4. 在每次验证时为数组中的每个值实例化一个新的验证器实例效率很低。

  5. 生成的错误消息未显示数组元素无效的原因,从而导致用户体验不佳。

  6. 这是一个更新的enumerable and array validator,解决了所有这些问题。为方便起见,下面包含代码。

    # Validates the values of an Enumerable with other validators.
    # Generates error messages that include the index and value of
    # invalid elements.
    #
    # Example:
    #
    #   validates :values, enum: { presence: true, inclusion: { in: %w{ big small } } }
    #
    class EnumValidator < ActiveModel::EachValidator
    
      def initialize(options)
        super
        @validators = options.map do |(key, args)|
          create_validator(key, args)
        end
      end
    
      def validate_each(record, attribute, values)
        helper = Helper.new(@validators, record, attribute)
        Array.wrap(values).each do |value|
          helper.validate(value)
        end
      end
    
      private
    
      class Helper
    
        def initialize(validators, record, attribute)
          @validators = validators
          @record = record
          @attribute = attribute
          @count = -1
        end
    
        def validate(value)
          @count += 1
          @validators.each do |validator|
            next if value.nil? && validator.options[:allow_nil]
            next if value.blank? && validator.options[:allow_blank]
            validate_with(validator, value)
          end
        end
    
        def validate_with(validator, value)
          before_errors = error_count
          run_validator(validator, value)
          if error_count > before_errors
            prefix = "element #{@count} (#{value}) "
            (before_errors...error_count).each do |pos|
              error_messages[pos] = prefix + error_messages[pos]
            end
          end
        end
    
        def run_validator(validator, value)
          validator.validate_each(@record, @attribute, value)
        rescue NotImplementedError
          validator.validate(@record)
        end
    
        def error_messages
          @record.errors.messages[@attribute]
        end
    
        def error_count
          error_messages ? error_messages.length : 0
        end
      end
    
      def create_validator(key, args)
        opts = {attributes: attributes}
        opts.merge!(args) if args.kind_of?(Hash)
        validator_class(key).new(opts).tap do |validator|
          validator.check_validity!
        end
      end
    
      def validator_class(key)
        validator_class_name = "#{key.to_s.camelize}Validator"
        validator_class_name.constantize
      rescue NameError
        "ActiveModel::Validations::#{validator_class_name}".constantize
      end
    end
    

答案 2 :(得分:5)

您可能希望为电子邮件字段定义自己的自定义验证程序。

因此,您将在课程定义后添加

validate :validate_emails

def validate_emails
  invalid_emails = self.emails.map{ |email| email.match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) }.select{ |e| e != nil }
  errors.add(:emails, 'invalid email address') unless invalid_emails.empty?
end

正则表达式本身可能并不完美,但这是基本的想法。您可以按如下方式查看导轨指南:

http://guides.rubyonrails.org/v2.3.8/activerecord_validations_callbacks.html#creating-custom-validation-methods

答案 3 :(得分:2)

发现自己刚刚尝试解决这个问题。我稍微修改了Tim O的答案以提出以下内容,它为错误对象提供更清晰的输出和更多信息,然后您可以在视图中向用户显示。

validate :validate_emails

def validate_emails
  emails.each do |email|
    unless email.match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
      errors.add(:emails, "#{email} is not a valid email address.")
    end
  end
end

答案 4 :(得分:-4)

以下是一个可能有助于修复rails api文档的示例:http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates

  

验证方法的强大功能来自于在给定属性的一次调用中使用自定义验证器和默认验证器,例如。

class EmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors[attribute] << (options[:message] || "is not an email") unless
      value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
  end
end

class Person
  include ActiveModel::Validations
  attr_accessor :name, :email

  validates :name, :presence => true, :uniqueness => true, :length => { :maximum => 100 }
  validates :email, :presence => true, :email => true
end