Rails:验证链接(URL)的好方法是什么? [英] Rails: What's a good way to validate links (URLs)?

查看:50
本文介绍了Rails:验证链接(URL)的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何最好地验证 Rails 中的 URL.我正在考虑使用正则表达式,但不确定这是否是最佳做法.

I was wondering how I would best validate URLs in Rails. I was thinking of using a regular expression, but am not sure if this is the best practice.

而且,如果我要使用正则表达式,有人可以向我推荐一个吗?我对 Regex 还是个新手.

And, if I were to use a regex, could someone suggest one to me? I am still new to Regex.

推荐答案

验证 URL 是一项棘手的工作.这也是一个非常广泛的要求.

Validating an URL is a tricky job. It's also a very broad request.

你到底想做什么?您要验证 URL 的格式、存在性还是什么?有多种可能性,具体取决于您想要做什么.

What do you want to do, exactly? Do you want to validate the format of the URL, the existence, or what? There are several possibilities, depending on what you want to do.

正则表达式可以验证 URL 的格式.但即使是复杂的正则表达式也无法确保您处理的是有效的 URL.

A regular expression can validate the format of the URL. But even a complex regular expression cannot ensure you are dealing with a valid URL.

比如,如果你拿一个简单的正则表达式,它可能会拒绝下面的主机

For instance, if you take a simple regular expression, it will probably reject the following host

http://invalid##host.com

但它会允许

http://invalid-host.foo

这是一个有效的主机,但如果您考虑现有的 TLD,则它不是一个有效的域.实际上,如果您想验证主机名而不是域,该解决方案将起作用,因为以下是有效的主机名

that is a valid host, but not a valid domain if you consider the existing TLDs. Indeed, the solution would work if you want to validate the hostname, not the domain because the following one is a valid hostname

http://host.foo

还有下面的

http://localhost

现在,让我给你一些解决方案.

Now, let me give you some solutions.

如果你想验证一个域,那么你需要忘记正则表达式.目前可用的最佳解决方案是公共后缀列表,这是一个由 Mozilla 维护的列表.我创建了一个 Ruby 库来根据公共后缀列表解析和验证域,它被称为 PublicSuffix.

If you want to validate a domain, then you need to forget about regular expressions. The best solution available at the moment is the Public Suffix List, a list maintained by Mozilla. I created a Ruby library to parse and validate domains against the Public Suffix List, and it's called PublicSuffix.

如果您想验证 URI/URL 的格式,那么您可能需要使用正则表达式.使用内置的 Ruby URI.parse 方法,而不是搜索一个.

If you want to validate the format of an URI/URL, then you might want to use regular expressions. Instead of searching for one, use the built-in Ruby URI.parse method.

require 'uri'

def valid_url?(uri)
  uri = URI.parse(uri) && uri.host
rescue URI::InvalidURIError
  false
end

您甚至可以决定使其更具限制性.例如,如果您希望 URL 为 HTTP/HTTPS URL,则可以使验证更加准确.

You can even decide to make it more restrictive. For instance, if you want the URL to be an HTTP/HTTPS URL, then you can make the validation more accurate.

require 'uri'

def valid_url?(url)
  uri = URI.parse(url)
  uri.is_a?(URI::HTTP) && !uri.host.nil?
rescue URI::InvalidURIError
  false
end

当然,您可以对这种方法进行大量改进,包括检查路径或方案.

Of course, there are tons of improvements you can apply to this method, including checking for a path or a scheme.

最后但并非最不重要的一点,您还可以将此代码打包到验证器中:

Last but not least, you can also package this code into a validator:

class HttpUrlValidator < ActiveModel::EachValidator

  def self.compliant?(value)
    uri = URI.parse(value)
    uri.is_a?(URI::HTTP) && !uri.host.nil?
  rescue URI::InvalidURIError
    false
  end

  def validate_each(record, attribute, value)
    unless value.present? && self.class.compliant?(value)
      record.errors.add(attribute, "is not a valid HTTP URL")
    end
  end

end

# in the model
validates :example_attribute, http_url: true

这篇关于Rails:验证链接(URL)的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆