validators 3.1.0 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +1 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +10 -0
- data/README.md +23 -8
- data/Rakefile +0 -1
- data/data/reserved_subdomains.txt +2830 -0
- data/lib/validators.rb +5 -4
- data/lib/validators/constants.rb +1 -1
- data/lib/validators/disposable_domains.rb +19 -0
- data/lib/validators/disposable_emails.rb +23 -0
- data/lib/validators/locale/en.yml +5 -2
- data/lib/validators/locale/pt-BR.yml +1 -0
- data/lib/validators/{reserved_hostnames.rb → reserved_subdomains.rb} +3 -5
- data/lib/validators/tld.rb +9 -5
- data/lib/validators/validates_email_format_of.rb +19 -4
- data/lib/validators/validates_subdomain.rb +69 -0
- data/lib/validators/validates_username.rb +15 -0
- data/lib/validators/version.rb +1 -1
- data/test/test_helper.rb +17 -2
- data/test/validators/disposable_email_test.rb +18 -5
- data/test/validators/validates_email_format_of_test.rb +4 -2
- data/test/validators/validates_subdomain_test.rb +75 -0
- data/test/validators/validates_url_format_of/with_tld_validation_test.rb +18 -0
- data/test/validators/validates_url_format_of/without_tld_validation_test.rb +18 -0
- data/test/validators/{validates_reserved_username_test.rb → validates_username_test.rb} +27 -4
- data/validators.gemspec +11 -1
- metadata +116 -22
- data/bin/sync-disposable-hostnames +0 -35
- data/bin/sync-tld +0 -17
- data/data/disposable.json +0 -57281
- data/data/reserved_hostnames.json +0 -1399
- data/data/tld.json +0 -1516
- data/lib/validators/disposable_hostnames.rb +0 -11
- data/lib/validators/validates_reserved_hostname.rb +0 -45
- data/lib/validators/validates_reserved_username.rb +0 -29
- data/test/validators/validates_reserved_hostname_test.rb +0 -40
@@ -1,45 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
module Validations
|
5
|
-
class ReservedHostnameValidator < EachValidator
|
6
|
-
def validate_each(record, attribute, value)
|
7
|
-
return if value.blank? && options[:allow_blank]
|
8
|
-
return if value.nil? && options[:allow_nil]
|
9
|
-
return unless reserved?(value.to_s)
|
10
|
-
|
11
|
-
record.errors.add(
|
12
|
-
attribute,
|
13
|
-
:"reserved_#{options[:error_name]}",
|
14
|
-
message: options[:message],
|
15
|
-
value: value
|
16
|
-
)
|
17
|
-
end
|
18
|
-
|
19
|
-
def reserved?(subdomain)
|
20
|
-
::Validators::ReservedHostnames.reserved?(subdomain, options[:in])
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
module ClassMethods
|
25
|
-
# Validates whether or not the specified hostname is valid.
|
26
|
-
# The `in: array` can have strings and patterns. A pattern is everything
|
27
|
-
# that starts with `/` and will be parsed as a regular expression.
|
28
|
-
#
|
29
|
-
# Notice that subdomains will be normalized; it'll be downcased and have
|
30
|
-
# its underscores and hyphens stripped.
|
31
|
-
#
|
32
|
-
# class User < ActiveRecord::Base
|
33
|
-
# validates_reserved_hostname :site
|
34
|
-
#
|
35
|
-
# # Validates against a custom list.
|
36
|
-
# validates_reserved_hostname :site, in: %w[www]
|
37
|
-
# end
|
38
|
-
#
|
39
|
-
def validates_reserved_hostname(*attr_names)
|
40
|
-
options = _merge_attributes(attr_names).merge(error_name: :hostname)
|
41
|
-
validates_with ReservedHostnameValidator, options
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
module Validations
|
5
|
-
class ReservedUsernameValidator < ReservedHostnameValidator
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
# Validates whether or not the specified username is valid.
|
10
|
-
# The `in: array` can have strings and patterns. A pattern is everything
|
11
|
-
# that starts with `/` and will be parsed as a regular expression.
|
12
|
-
#
|
13
|
-
# Notice that subdomains will be normalized; it'll be downcased and have
|
14
|
-
# its underscores and hyphens stripped.
|
15
|
-
#
|
16
|
-
# class User < ActiveRecord::Base
|
17
|
-
# validates_reserved_hostname :site
|
18
|
-
#
|
19
|
-
# # Validates against a custom list.
|
20
|
-
# validates_reserved_hostname :site, in: %w[www]
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
def validates_reserved_username(*attr_names)
|
24
|
-
options = _merge_attributes(attr_names).merge(error_name: :username)
|
25
|
-
validates_with ReservedHostnameValidator, options
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "test_helper"
|
4
|
-
|
5
|
-
class ValidatesReservedHostnameTest < Minitest::Test
|
6
|
-
test "rejects reserved hostname" do
|
7
|
-
model = build_model do
|
8
|
-
attr_accessor :hostname
|
9
|
-
validates_reserved_hostname :hostname
|
10
|
-
end
|
11
|
-
|
12
|
-
instance = model.new(hostname: "www")
|
13
|
-
|
14
|
-
refute instance.valid?
|
15
|
-
assert_includes instance.errors[:hostname],
|
16
|
-
"www is a reserved hostname"
|
17
|
-
end
|
18
|
-
|
19
|
-
test "rejects reserved hostname with pattern" do
|
20
|
-
model = build_model do
|
21
|
-
attr_accessor :hostname
|
22
|
-
validates_reserved_hostname :hostname
|
23
|
-
end
|
24
|
-
|
25
|
-
instance = model.new(hostname: "www1234")
|
26
|
-
|
27
|
-
refute instance.valid?
|
28
|
-
end
|
29
|
-
|
30
|
-
test "uses custom list" do
|
31
|
-
model = build_model do
|
32
|
-
attr_accessor :hostname
|
33
|
-
validates_reserved_hostname :hostname, in: %w[nope]
|
34
|
-
end
|
35
|
-
|
36
|
-
instance = model.new(hostname: "nope")
|
37
|
-
|
38
|
-
refute instance.valid?
|
39
|
-
end
|
40
|
-
end
|