defra_ruby_validators 2.5.0 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -0
  3. data/config/locales/defra_ruby/validators/mobile_phone_number_validator/en.yml +7 -0
  4. data/lib/defra_ruby/validators/companies_house_number_validator.rb +3 -3
  5. data/lib/defra_ruby/validators/mobile_phone_number_validator.rb +32 -0
  6. data/lib/defra_ruby/validators/version.rb +1 -1
  7. data/lib/defra_ruby/validators.rb +1 -0
  8. metadata +24 -175
  9. data/spec/defra_ruby/validators/business_type_validator_spec.rb +0 -56
  10. data/spec/defra_ruby/validators/companies_house_number_validator_spec.rb +0 -124
  11. data/spec/defra_ruby/validators/companies_house_service_spec.rb +0 -98
  12. data/spec/defra_ruby/validators/configuration_spec.rb +0 -49
  13. data/spec/defra_ruby/validators/email_validator_spec.rb +0 -46
  14. data/spec/defra_ruby/validators/grid_reference_validator_spec.rb +0 -57
  15. data/spec/defra_ruby/validators/location_validator_spec.rb +0 -56
  16. data/spec/defra_ruby/validators/past_date_validator_spec.rb +0 -35
  17. data/spec/defra_ruby/validators/phone_number_validator_spec.rb +0 -59
  18. data/spec/defra_ruby/validators/position_validator_spec.rb +0 -47
  19. data/spec/defra_ruby/validators/token_validator_spec.rb +0 -45
  20. data/spec/defra_ruby/validators/true_false_validator_spec.rb +0 -37
  21. data/spec/defra_ruby/validators_spec.rb +0 -12
  22. data/spec/examples.txt +0 -189
  23. data/spec/spec_helper.rb +0 -87
  24. data/spec/support/defra_ruby_validators.rb +0 -16
  25. data/spec/support/helpers/text_generator.rb +0 -17
  26. data/spec/support/helpers/translator.rb +0 -15
  27. data/spec/support/i18n.rb +0 -8
  28. data/spec/support/pry.rb +0 -7
  29. data/spec/support/shared_examples/validators/characters_validator.rb +0 -29
  30. data/spec/support/shared_examples/validators/invalid_record.rb +0 -29
  31. data/spec/support/shared_examples/validators/length_validator.rb +0 -29
  32. data/spec/support/shared_examples/validators/presence_validator.rb +0 -29
  33. data/spec/support/shared_examples/validators/selection_validator.rb +0 -40
  34. data/spec/support/shared_examples/validators/valid_record.rb +0 -12
  35. data/spec/support/shared_examples/validators/validator.rb +0 -8
  36. data/spec/support/simplecov.rb +0 -17
@@ -1,98 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "webmock/rspec"
4
-
5
- RSpec.describe DefraRuby::Validators::CompaniesHouseService do
6
- let(:host) { "https://api.companieshouse.gov.uk/" }
7
-
8
- describe "#status" do
9
- let(:subject) { described_class.new("09360070") }
10
-
11
- context "when the company_no is for an active company" do
12
- before do
13
- expected_body = { "company_status": "active", "type": "ltd" }
14
-
15
- stub_request(:any, /.*#{host}.*/).to_return(
16
- status: 200,
17
- body: expected_body.to_json
18
- )
19
- end
20
-
21
- it "returns :active" do
22
- expect(subject.status).to eq(:active)
23
- end
24
- end
25
-
26
- context "when the company_no is not found" do
27
- before do
28
- stub_request(:any, /.*#{host}.*/).to_return(
29
- status: 404
30
- )
31
- end
32
-
33
- it "returns :not_found" do
34
- expect(subject.status).to eq(:not_found)
35
- end
36
- end
37
-
38
- context "when the company_no is inactive" do
39
- before do
40
- expected_body = { "company_status": "dissolved", "type": "ltd" }
41
-
42
- stub_request(:any, /.*#{host}.*/).to_return(
43
- status: 200,
44
- body: expected_body.to_json
45
- )
46
- end
47
-
48
- it "returns :inactive" do
49
- expect(subject.status).to eq(:inactive)
50
- end
51
- end
52
-
53
- context "checking the company_type" do
54
- let(:subject) { described_class.new("09360070", "llp") }
55
-
56
- context "when the company_no is for a LLP" do
57
- before do
58
- stub_request(:any, /.*#{host}.*/).to_return(
59
- status: 200,
60
- body: expected_body.to_json
61
- )
62
- end
63
-
64
- let(:expected_body) { { "company_status": "active", "type": "llp" } }
65
-
66
- it "returns :active" do
67
- expect(subject.status).to eq(:active)
68
- end
69
-
70
- context "but a ltd company is found" do
71
- let(:expected_body) { { "company_status": "active", "type": "ltd" } }
72
-
73
- it "returns :not_found" do
74
- expect(subject.status).to eq(:not_found)
75
- end
76
- end
77
- end
78
- end
79
-
80
- context "when there is a problem with the Companies House API" do
81
- context "and the request times out" do
82
- before { stub_request(:any, /.*#{host}.*/).to_timeout }
83
-
84
- it "raises an exception" do
85
- expect { subject.status }.to raise_error(StandardError)
86
- end
87
- end
88
-
89
- context "and request returns an error" do
90
- before { stub_request(:any, /.*#{host}.*/).to_raise(SocketError) }
91
-
92
- it "raises an exception" do
93
- expect { subject.status }.to raise_error(StandardError)
94
- end
95
- end
96
- end
97
- end
98
- end
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe DefraRuby::Validators::Configuration do
6
- describe "ATTRIBUTES" do
7
- it "represents the expected config settings and only those settings" do
8
- expected_attributes = %i[
9
- companies_house_host
10
- companies_house_api_key
11
- ]
12
-
13
- expect(described_class::ATTRIBUTES).to match_array(expected_attributes)
14
- end
15
- end
16
-
17
- it "sets the appropriate default config settings" do
18
- fresh_config = described_class.new
19
-
20
- expect(fresh_config.companies_house_host).to eq("https://api.companieshouse.gov.uk/company/")
21
- expect(fresh_config.companies_house_api_key).to eq(nil)
22
- end
23
-
24
- describe "#ensure_valid" do
25
- before(:each) do
26
- described_class::ATTRIBUTES.each do |attribute|
27
- subject.public_send("#{attribute}=", "foo")
28
- end
29
- end
30
-
31
- context "when all of the attributes are present" do
32
- it "does not raise an error" do
33
- expect { subject.ensure_valid }.to_not raise_error
34
- expect(subject.ensure_valid).to eq(true)
35
- end
36
- end
37
-
38
- context "when at least one of the attributes is missing" do
39
- before(:each) do
40
- subject.companies_house_api_key = nil
41
- end
42
-
43
- it "raises an error" do
44
- message = "The following DefraRuby::Validators configuration attributes are missing: [:companies_house_api_key]"
45
- expect { subject.ensure_valid }.to raise_error(message)
46
- end
47
- end
48
- end
49
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Test
6
- EmailValidatable = Struct.new(:email) do
7
- include ActiveModel::Validations
8
-
9
- validates :email, "defra_ruby/validators/email": true
10
- end
11
- end
12
-
13
- module DefraRuby
14
- module Validators
15
- RSpec.describe EmailValidator, type: :model do
16
-
17
- valid_email = "test@example.com"
18
- invalid_email = "foo@bar"
19
-
20
- it_behaves_like("a validator")
21
- it_behaves_like(
22
- "a presence validator",
23
- EmailValidator,
24
- Test::EmailValidatable,
25
- :email,
26
- valid: valid_email
27
- )
28
-
29
- describe "#validate_each" do
30
- context "when the email is not valid" do
31
- context "because the email is not correctly formatted" do
32
- validatable = Test::EmailValidatable.new(invalid_email)
33
-
34
- error_message = Helpers::Translator.error_message(EmailValidator, :invalid_format)
35
-
36
- it_behaves_like "an invalid record",
37
- validatable: validatable,
38
- attribute: :email,
39
- error: :invalid_format,
40
- error_message: error_message
41
- end
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Test
6
- GridReferenceValidatable = Struct.new(:grid_reference) do
7
- include ActiveModel::Validations
8
-
9
- validates :grid_reference, "defra_ruby/validators/grid_reference": true
10
- end
11
- end
12
-
13
- module DefraRuby
14
- module Validators
15
- RSpec.describe GridReferenceValidator, type: :model do
16
-
17
- valid_grid_reference = "ST 58337 72855" # Bristol, City of Bristol
18
- invalid_grid_reference = "58337 72855"
19
- non_coordinate_grid_reference = "AA 12345 67890"
20
-
21
- it_behaves_like("a validator")
22
- it_behaves_like(
23
- "a presence validator",
24
- GridReferenceValidator,
25
- Test::GridReferenceValidatable,
26
- :grid_reference,
27
- valid: valid_grid_reference
28
- )
29
-
30
- describe "#validate_each" do
31
- context "when the grid reference is not valid" do
32
- context "because the grid reference is not correctly formatted" do
33
- validatable = Test::GridReferenceValidatable.new(invalid_grid_reference)
34
- error_message = Helpers::Translator.error_message(GridReferenceValidator, :invalid_format)
35
-
36
- it_behaves_like "an invalid record",
37
- validatable: validatable,
38
- attribute: :grid_reference,
39
- error: :invalid_format,
40
- error_message: error_message
41
- end
42
-
43
- context "because the grid reference is not a coordinate" do
44
- validatable = Test::GridReferenceValidatable.new(non_coordinate_grid_reference)
45
- error_message = Helpers::Translator.error_message(GridReferenceValidator, :invalid)
46
-
47
- it_behaves_like "an invalid record",
48
- validatable: validatable,
49
- attribute: :grid_reference,
50
- error: :invalid,
51
- error_message: error_message
52
- end
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Test
6
- LocationValidatable = Struct.new(:location) do
7
- include ActiveModel::Validations
8
-
9
- validates :location, "defra_ruby/validators/location": true
10
- end
11
- end
12
-
13
- module DefraRuby
14
- module Validators
15
- RSpec.describe LocationValidator, type: :model do
16
-
17
- valid_location = %w[england northern_ireland scotland wales].sample
18
- invalid_location = "france"
19
-
20
- it_behaves_like("a validator")
21
- it_behaves_like(
22
- "a selection validator",
23
- LocationValidator,
24
- Test::LocationValidatable,
25
- :location,
26
- valid: valid_location, invalid: invalid_location
27
- )
28
-
29
- context "when the value is overseas" do
30
- validatable = Test::LocationValidatable.new("overseas")
31
-
32
- context "when overseas_allowed is enabled" do
33
- before do
34
- allow_any_instance_of(DefraRuby::Validators::BaseValidator).to receive(:options).and_return(allow_overseas: true)
35
- end
36
-
37
- it_behaves_like "a valid record", validatable
38
- end
39
-
40
- context "when overseas_allowed is not enabled" do
41
- before do
42
- allow_any_instance_of(DefraRuby::Validators::BaseValidator).to receive(:options).and_return({})
43
- end
44
-
45
- error_message = Helpers::Translator.error_message(LocationValidator, :inclusion)
46
-
47
- it_behaves_like "an invalid record",
48
- validatable: validatable,
49
- attribute: :location,
50
- error: :inclusion,
51
- error_message: error_message
52
- end
53
- end
54
- end
55
- end
56
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Test
6
- PastDateValidatable = Struct.new(:date) do
7
- include ActiveModel::Validations
8
-
9
- validates :date, "defra_ruby/validators/past_date": true
10
- end
11
- end
12
-
13
- module DefraRuby
14
- module Validators
15
- RSpec.describe PastDateValidator, type: :model do
16
-
17
- valid_date = Date.today
18
- future_date = Date.today + 1
19
- invalid_date = Date.new(20, 2, 2)
20
-
21
- it_behaves_like "a valid record", Test::PastDateValidatable.new(valid_date)
22
- it_behaves_like "an invalid record",
23
- validatable: Test::PastDateValidatable.new(future_date),
24
- attribute: :date,
25
- error: :past_date,
26
- error_message: Helpers::Translator.error_message(PastDateValidator, :past_date)
27
-
28
- it_behaves_like "an invalid record",
29
- validatable: Test::PastDateValidatable.new(invalid_date),
30
- attribute: :date,
31
- error: :invalid_date,
32
- error_message: Helpers::Translator.error_message(PastDateValidator, :invalid_date)
33
- end
34
- end
35
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Test
6
- PhoneNumberValidatable = Struct.new(:phone_number) do
7
- include ActiveModel::Validations
8
-
9
- validates :phone_number, "defra_ruby/validators/phone_number": true
10
- end
11
- end
12
-
13
- module DefraRuby
14
- module Validators
15
- RSpec.describe PhoneNumberValidator, type: :model do
16
-
17
- valid_number = [
18
- "+441234567890",
19
- "01234567890",
20
- "+441234-567-890",
21
- "01234.567.890",
22
- "+441234 567 890"
23
- ].sample
24
- too_long_number = Helpers::TextGenerator.random_number_string(16) # The max length is 15.
25
- invalid_number = "#123"
26
-
27
- it_behaves_like("a validator")
28
- it_behaves_like(
29
- "a presence validator",
30
- PhoneNumberValidator,
31
- Test::PhoneNumberValidatable,
32
- :phone_number,
33
- valid: valid_number
34
- )
35
- it_behaves_like(
36
- "a length validator",
37
- PhoneNumberValidator,
38
- Test::PhoneNumberValidatable,
39
- :phone_number,
40
- valid: valid_number, invalid: too_long_number
41
- )
42
-
43
- describe "#validate_each" do
44
- context "when the phone number is not valid" do
45
- context "because the phone number is not correctly formatted" do
46
- validatable = Test::PhoneNumberValidatable.new(invalid_number)
47
- error_message = Helpers::Translator.error_message(PhoneNumberValidator, :invalid_format)
48
-
49
- it_behaves_like "an invalid record",
50
- validatable: validatable,
51
- attribute: :phone_number,
52
- error: :invalid_format,
53
- error_message: error_message
54
- end
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Test
6
- PositionValidatable = Struct.new(:position) do
7
- include ActiveModel::Validations
8
-
9
- validates :position, "defra_ruby/validators/position": true
10
- end
11
- end
12
-
13
- module DefraRuby
14
- module Validators
15
- RSpec.describe PositionValidator, type: :model do
16
-
17
- valid_position = "Padawan"
18
- too_long_position = Helpers::TextGenerator.random_string(PositionValidator::MAX_LENGTH + 1)
19
- invalid_position = "**Invalid_@_Position**"
20
- empty_position = ""
21
-
22
- it_behaves_like("a validator")
23
- it_behaves_like(
24
- "a length validator",
25
- PositionValidator,
26
- Test::PositionValidatable,
27
- :position,
28
- valid: valid_position, invalid: too_long_position
29
- )
30
- it_behaves_like(
31
- "a characters validator",
32
- PositionValidator,
33
- Test::PositionValidatable,
34
- :position,
35
- valid: valid_position, invalid: invalid_position
36
- )
37
-
38
- describe "#validate_each" do
39
- context "when the position is valid" do
40
- context "despite being blank (because position is optional)" do
41
- it_behaves_like "a valid record", Test::PositionValidatable.new(empty_position)
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Test
6
- TokenValidatable = Struct.new(:token) do
7
- include ActiveModel::Validations
8
-
9
- validates :token, "defra_ruby/validators/token": true
10
- end
11
- end
12
-
13
- module DefraRuby
14
- module Validators
15
- RSpec.describe TokenValidator, type: :model do
16
-
17
- valid_token = Helpers::TextGenerator.random_string(24)
18
- invalid_token = "123456"
19
-
20
- it_behaves_like("a validator")
21
- it_behaves_like(
22
- "a presence validator",
23
- TokenValidator,
24
- Test::TokenValidatable,
25
- :token,
26
- valid: valid_token
27
- )
28
-
29
- describe "#validate_each" do
30
- context "when the token is not valid" do
31
- context "because the token is not correctly formatted" do
32
- validatable = Test::TokenValidatable.new(invalid_token)
33
- error_message = Helpers::Translator.error_message(TokenValidator, :invalid_format)
34
-
35
- it_behaves_like "an invalid record",
36
- validatable: validatable,
37
- attribute: :token,
38
- error: :invalid_format,
39
- error_message: error_message
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Test
6
- TrueFalseValidatable = Struct.new(:attribute) do
7
- include ActiveModel::Validations
8
-
9
- validates :attribute, "defra_ruby/validators/true_false": true
10
- end
11
- end
12
-
13
- module DefraRuby
14
- module Validators
15
- RSpec.describe TrueFalseValidator do
16
- invalid_value = "unsure"
17
-
18
- it_behaves_like("a validator")
19
-
20
- it_behaves_like(
21
- "a selection validator",
22
- TrueFalseValidator,
23
- Test::TrueFalseValidatable,
24
- :attribute,
25
- valid: true, invalid: invalid_value
26
- )
27
-
28
- it_behaves_like(
29
- "a selection validator",
30
- TrueFalseValidator,
31
- Test::TrueFalseValidatable,
32
- :attribute,
33
- valid: false, invalid: invalid_value
34
- )
35
- end
36
- end
37
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe DefraRuby::Validators do
6
- describe "VERSION" do
7
- it "is a version string in the correct format" do
8
- expect(DefraRuby::Validators::VERSION).to be_a(String)
9
- expect(DefraRuby::Validators::VERSION).to match(/\d+\.\d+\.\d+/)
10
- end
11
- end
12
- end