23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/ree_lib/packages/ree_validator/package/ree_validator/functions/validate_url.rb', line 23
def call(url, error = nil, **opts)
begin
uri = URI.parse(url)
if opts[:schemes] && (opts[:schemes] & [uri.scheme]).size == 0
error ||= UrlErr.new(
t(
'validator.url.invalid_scheme',
{schemes: opts[:schemes]},
default_by_locale: :en
)
)
raise error
end
if opts[:ports] && (opts[:ports] & [uri.port]).size == 0
error ||= UrlErr.new(
t(
'validator.url.invalid_port',
{ports: opts[:ports]},
default_by_locale: :en
)
)
raise error
end
if opts[:domains] && (opts[:domains] & [uri.hostname]).size == 0
error ||= UrlErr.new(
t(
'validator.url.invalid_domain',
{domains: opts[:domains]},
default_by_locale: :en
)
)
raise error
end
rescue URI::InvalidURIError
error ||= UrlErr.new(
t(
'validator.url.invalid_url',
default_by_locale: :en
)
)
raise error
end
true
end
|