MongoMapper自定义验证

发布于 2024-10-31 04:03:12 字数 732 浏览 2 评论 0原文

我有一个带有一系列链接的 ruby​​ 类。现在,即使数组包含无效 URL 的链接,我也可以保存 Paper 对象。我有一个方法可以遍历数组并验证 url,如果 url 无效则返回 false。但当我尝试调用 Paper.save 时,我想收到一条错误消息。这可能吗?

class Paper
  include MongoMapper::Document

  key :links,       Array

  validates_presence_of :links

  def validate_urls
    reg = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
    status = []
    links.each do |link|
      if link.match(reg)
        status.push('true')
      else
        if "http://#{link}".match(reg)
          status.push('true')
        else
          status.push('false')
        end
      end
    end
    if status.include?('false')
      return false
    else
      return true
    end
  end

end

I have this ruby class with an array of links. As it is now I'm able to save a Paper object even if the array contains links that are not valid urls. I have a method that runs through the array and validates the urls and returns false if a url is invalid. But I want to get an error message when I try to call Paper.save. Is that possible?

class Paper
  include MongoMapper::Document

  key :links,       Array

  validates_presence_of :links

  def validate_urls
    reg = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
    status = []
    links.each do |link|
      if link.match(reg)
        status.push('true')
      else
        if "http://#{link}".match(reg)
          status.push('true')
        else
          status.push('false')
        end
      end
    end
    if status.include?('false')
      return false
    else
      return true
    end
  end

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2024-11-07 04:03:12

如果您使用 GitHub 上的 MongoMapper(支持 ActiveModel),请参阅 http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validate

class Paper
  include MongoMapper::Document

  key :links,       Array

  validates_presence_of :links
  validate :validate_urls

  def validate_urls
    reg = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
    status = []
    links.each do |link|
      if link.match(reg)
        status.push('true')
      else
        if "http://#{link}".match(reg)
          status.push('true')
        else
          status.push('false')
        end
      end
    end
    if status.include?('false')

      # add errors to make the save fail
      errors.add :links, 'must all be valid urls'

    end
  end

end

不确定该代码是否适用于 0.8.6 gem,但可能会。

另外,它不适用于这种情况,但如果它不是一个数组,您可以将其全部分解为一行:

key :link, String, :format => /your regex here/

If you're using MongoMapper from GitHub (which supports ActiveModel), see http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validate

class Paper
  include MongoMapper::Document

  key :links,       Array

  validates_presence_of :links
  validate :validate_urls

  def validate_urls
    reg = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
    status = []
    links.each do |link|
      if link.match(reg)
        status.push('true')
      else
        if "http://#{link}".match(reg)
          status.push('true')
        else
          status.push('false')
        end
      end
    end
    if status.include?('false')

      # add errors to make the save fail
      errors.add :links, 'must all be valid urls'

    end
  end

end

Not sure if that code works with the 0.8.6 gem but it might.

Also, it doesn't apply in this case but if it weren't an array you could smash it all into a single line:

key :link, String, :format => /your regex here/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文