validators 3.1.1 → 3.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +11 -1
  5. data/README.md +49 -29
  6. data/Rakefile +0 -1
  7. data/data/reserved_subdomains.txt +2830 -0
  8. data/lib/validators.rb +12 -4
  9. data/lib/validators/constants.rb +1 -1
  10. data/lib/validators/disposable_domains.rb +18 -0
  11. data/lib/validators/disposable_emails.rb +22 -0
  12. data/lib/validators/locale/en.yml +5 -2
  13. data/lib/validators/locale/pt-BR.yml +1 -0
  14. data/lib/validators/{reserved_hostnames.rb → reserved_subdomains.rb} +3 -5
  15. data/lib/validators/tld.rb +7 -5
  16. data/lib/validators/validates_cnpj_format_of.rb +1 -2
  17. data/lib/validators/validates_cpf_format_of.rb +1 -2
  18. data/lib/validators/validates_email_format_of.rb +19 -4
  19. data/lib/validators/validates_ssh_private_key.rb +1 -3
  20. data/lib/validators/validates_ssh_public_key.rb +1 -3
  21. data/lib/validators/validates_subdomain.rb +69 -0
  22. data/lib/validators/validates_username.rb +15 -0
  23. data/lib/validators/version.rb +1 -1
  24. data/test/test_helper.rb +17 -2
  25. data/test/validators/disposable_email_test.rb +18 -5
  26. data/test/validators/validates_cnpj_format_of_test.rb +3 -3
  27. data/test/validators/validates_cpf_format_of_test.rb +3 -3
  28. data/test/validators/validates_email_format_of_test.rb +27 -2
  29. data/test/validators/validates_ssh_private_key/common_test.rb +3 -3
  30. data/test/validators/validates_ssh_public_key_test.rb +3 -3
  31. data/test/validators/validates_subdomain_test.rb +75 -0
  32. data/test/validators/validates_url_format_of/with_tld_validation_test.rb +18 -0
  33. data/test/validators/validates_url_format_of/without_tld_validation_test.rb +18 -0
  34. data/test/validators/{validates_reserved_username_test.rb → validates_username_test.rb} +27 -4
  35. data/validators.gemspec +11 -1
  36. metadata +116 -22
  37. data/bin/sync-disposable-hostnames +0 -35
  38. data/bin/sync-tld +0 -17
  39. data/data/disposable.json +0 -60403
  40. data/data/reserved_hostnames.json +0 -2836
  41. data/data/tld.json +0 -1516
  42. data/lib/validators/disposable_hostnames.rb +0 -11
  43. data/lib/validators/validates_reserved_hostname.rb +0 -45
  44. data/lib/validators/validates_reserved_username.rb +0 -29
  45. data/test/validators/validates_reserved_hostname_test.rb +0 -40
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Validators
4
- class DisposableHostnames
5
- FILE_PATH = File.expand_path("../../data/disposable.json", __dir__)
6
-
7
- def self.all
8
- @all ||= JSON.parse(File.read(FILE_PATH))
9
- end
10
- end
11
- end
@@ -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