Ant*_*rdo 14 ruby ruby-on-rails mongodb mongoid ruby-on-rails-3
我有这个型号:
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
Run Code Online (Sandbox Code Playgroud)
现在我想验证emails数组中的每个电子邮件是否格式正确.我读了Mongoid和ActiveModel :: Validations文档,但我没有找到如何做到这一点.
你能告诉我指针吗?
Mil*_*vic 26
您可以定义自定义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
Run Code Online (Sandbox Code Playgroud)
您可以在模型中使用它:
class User
include Mongoid::Document
field :tags, Array
validates :tags, array: { presence: true, inclusion: { in: %w{ ruby rails } }
end
Run Code Online (Sandbox Code Playgroud)
它将针对散列中指定的每个验证器验证数组中的每个元素.array
Sim*_*Sim 13
Milovan的回答得到了我的支持,但实施有一些问题:
展平嵌套数组会更改行为并隐藏无效值.
nil字段值被视为[nil],这似乎不正确.
提供的示例presence: true将生成NotImplementedError错误,因为PresenceValidator未实现validate_each.
在每次验证时为数组中的每个值实例化一个新的验证器实例效率很低.
生成的错误消息未显示数组元素无效的原因,从而导致用户体验不佳.
这是一个更新的可枚举和数组验证器,可以解决所有这些问题.为方便起见,下面包含代码.
# 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
Run Code Online (Sandbox Code Playgroud)
您可能希望为电子邮件字段定义自己的自定义验证程序.
因此,您将在课程定义后添加,
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
Run Code Online (Sandbox Code Playgroud)
正则表达式本身可能并不完美,但这是基本的想法.您可以按如下方式查看导轨指南: