defra_ruby_validators 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +88 -1
- data/config/locales/defra_ruby/validators/business_type_validator/en.yml +6 -0
- data/config/locales/defra_ruby/validators/email_validator/en.yml +7 -0
- data/config/locales/defra_ruby/validators/grid_reference_validator/en.yml +8 -0
- data/config/locales/defra_ruby/validators/location_validator/en.yml +6 -0
- data/config/locales/defra_ruby/validators/phone_number_validator/en.yml +8 -0
- data/config/locales/defra_ruby/validators/position_validator/en.yml +7 -0
- data/config/locales/defra_ruby/validators/token_validator/en.yml +7 -0
- data/config/locales/defra_ruby/validators/true_false_validator/en.yml +2 -1
- data/lib/defra_ruby/validators.rb +10 -0
- data/lib/defra_ruby/validators/base_validator.rb +2 -4
- data/lib/defra_ruby/validators/business_type_validator.rb +15 -0
- data/lib/defra_ruby/validators/companies_house_number_validator.rb +5 -5
- data/lib/defra_ruby/validators/concerns/can_validate_characters.rb +19 -0
- data/lib/defra_ruby/validators/concerns/can_validate_length.rb +18 -0
- data/lib/defra_ruby/validators/concerns/can_validate_presence.rb +18 -0
- data/lib/defra_ruby/validators/concerns/can_validate_selection.rb +6 -8
- data/lib/defra_ruby/validators/email_validator.rb +28 -0
- data/lib/defra_ruby/validators/grid_reference_validator.rb +55 -0
- data/lib/defra_ruby/validators/location_validator.rb +15 -0
- data/lib/defra_ruby/validators/phone_number_validator.rb +32 -0
- data/lib/defra_ruby/validators/position_validator.rb +22 -0
- data/lib/defra_ruby/validators/token_validator.rb +27 -0
- data/lib/defra_ruby/validators/true_false_validator.rb +2 -2
- data/lib/defra_ruby/validators/version.rb +1 -1
- data/spec/cassettes/company_no_inactive.yml +14 -14
- data/spec/cassettes/company_no_not_found.yml +15 -15
- data/spec/cassettes/company_no_valid.yml +14 -14
- data/spec/defra_ruby/validators/business_type_validator_spec.rb +30 -0
- data/spec/defra_ruby/validators/companies_house_number_validator_spec.rb +15 -15
- data/spec/defra_ruby/validators/email_validator_spec.rb +42 -0
- data/spec/defra_ruby/validators/grid_reference_validator_spec.rb +49 -0
- data/spec/defra_ruby/validators/location_validator_spec.rb +30 -0
- data/spec/defra_ruby/validators/phone_number_validator_spec.rb +55 -0
- data/spec/defra_ruby/validators/position_validator_spec.rb +47 -0
- data/spec/defra_ruby/validators/token_validator_spec.rb +41 -0
- data/spec/defra_ruby/validators/true_false_validator_spec.rb +11 -4
- data/spec/examples.txt +79 -44
- data/spec/spec_helper.rb +4 -0
- data/spec/support/helpers/text_generator.rb +17 -0
- data/spec/support/helpers/translator.rb +2 -3
- data/spec/support/shared_examples/validators/characters_validator.rb +25 -0
- data/spec/support/shared_examples/validators/invalid_record.rb +13 -3
- data/spec/support/shared_examples/validators/length_validator.rb +25 -0
- data/spec/support/shared_examples/validators/presence_validator.rb +25 -0
- data/spec/support/shared_examples/validators/selection_validator.rb +11 -11
- metadata +70 -19
- data/spec/support/dotenv.rb +0 -4
@@ -0,0 +1,30 @@
|
|
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
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,55 @@
|
|
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, :phone_number, :invalid_format)
|
48
|
+
|
49
|
+
it_behaves_like "an invalid record", validatable, :phone_number, error_message
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,47 @@
|
|
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
|
@@ -0,0 +1,41 @@
|
|
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, :token, :invalid_format)
|
34
|
+
|
35
|
+
it_behaves_like "an invalid record", validatable, :token, error_message
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -3,10 +3,10 @@
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
5
|
module Test
|
6
|
-
TrueFalseValidatable = Struct.new(:
|
6
|
+
TrueFalseValidatable = Struct.new(:attribute) do
|
7
7
|
include ActiveModel::Validations
|
8
8
|
|
9
|
-
validates :
|
9
|
+
validates :attribute, "defra_ruby/validators/true_false": true
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
@@ -15,9 +15,16 @@ module DefraRuby
|
|
15
15
|
RSpec.describe TrueFalseValidator do
|
16
16
|
|
17
17
|
valid_value = %w[true false].sample
|
18
|
+
invalid_value = "unsure"
|
18
19
|
|
19
|
-
it_behaves_like
|
20
|
-
it_behaves_like
|
20
|
+
it_behaves_like("a validator")
|
21
|
+
it_behaves_like(
|
22
|
+
"a selection validator",
|
23
|
+
TrueFalseValidator,
|
24
|
+
Test::TrueFalseValidatable,
|
25
|
+
:attribute,
|
26
|
+
valid: valid_value, invalid: invalid_value
|
27
|
+
)
|
21
28
|
end
|
22
29
|
end
|
23
30
|
end
|
data/spec/examples.txt
CHANGED
@@ -1,46 +1,81 @@
|
|
1
1
|
example_id | status | run_time |
|
2
2
|
---------------------------------------------------------------------------------- | ------ | --------------- |
|
3
|
-
./spec/defra_ruby/validators/
|
4
|
-
./spec/defra_ruby/validators/
|
5
|
-
./spec/defra_ruby/validators/
|
6
|
-
./spec/defra_ruby/validators/
|
7
|
-
./spec/defra_ruby/validators/
|
8
|
-
./spec/defra_ruby/validators/
|
9
|
-
./spec/defra_ruby/validators/
|
10
|
-
./spec/defra_ruby/validators/
|
11
|
-
./spec/defra_ruby/validators/
|
12
|
-
./spec/defra_ruby/validators/
|
13
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:
|
14
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
15
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
16
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
17
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
18
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
19
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
20
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
21
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
22
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:
|
23
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:
|
24
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
25
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
26
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:
|
27
|
-
./spec/defra_ruby/validators/
|
28
|
-
./spec/defra_ruby/validators/
|
29
|
-
./spec/defra_ruby/validators/
|
30
|
-
./spec/defra_ruby/validators/
|
31
|
-
./spec/defra_ruby/validators/
|
32
|
-
./spec/defra_ruby/validators/
|
33
|
-
./spec/defra_ruby/validators/
|
34
|
-
./spec/defra_ruby/validators/
|
35
|
-
./spec/defra_ruby/validators/
|
36
|
-
./spec/defra_ruby/validators/
|
37
|
-
./spec/defra_ruby/validators/
|
38
|
-
./spec/defra_ruby/validators/
|
39
|
-
./spec/defra_ruby/validators/
|
40
|
-
./spec/defra_ruby/validators/
|
41
|
-
./spec/defra_ruby/validators/
|
42
|
-
./spec/defra_ruby/validators/
|
43
|
-
./spec/defra_ruby/validators/
|
44
|
-
./spec/defra_ruby/validators/
|
45
|
-
./spec/defra_ruby/validators/
|
46
|
-
./spec/defra_ruby/
|
3
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:1:1] | passed | 0.00145 seconds |
|
4
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:2:1] | passed | 0.00011 seconds |
|
5
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:2:2:1:1:1] | passed | 0.00017 seconds |
|
6
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:2:2:1:1:2] | passed | 0.00445 seconds |
|
7
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:2:2:2:1:1:1] | passed | 0.00018 seconds |
|
8
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:2:2:2:1:1:2] | passed | 0.00016 seconds |
|
9
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:2:2:2:1:1:3] | passed | 0.00059 seconds |
|
10
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:2:2:2:2:1:1] | passed | 0.00016 seconds |
|
11
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:2:2:2:2:1:2] | passed | 0.0002 seconds |
|
12
|
+
./spec/defra_ruby/validators/business_type_validator_spec.rb[1:2:2:2:2:1:3] | passed | 0.00018 seconds |
|
13
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:1:1] | passed | 0.00009 seconds |
|
14
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:1:1:1] | passed | 0.00043 seconds |
|
15
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:1:1:2] | passed | 0.00044 seconds |
|
16
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:2:1:1] | passed | 0.00043 seconds |
|
17
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:2:1:2] | passed | 0.00041 seconds |
|
18
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:3:1:1] | passed | 0.00045 seconds |
|
19
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:3:1:2] | passed | 0.00646 seconds |
|
20
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:4:1:1] | passed | 0.00042 seconds |
|
21
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:4:1:2] | passed | 0.00038 seconds |
|
22
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:1] | passed | 0.00015 seconds |
|
23
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:2] | passed | 0.00016 seconds |
|
24
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:3] | passed | 0.00017 seconds |
|
25
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:1] | passed | 0.00017 seconds |
|
26
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:2] | passed | 0.00015 seconds |
|
27
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:3] | passed | 0.0002 seconds |
|
28
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:1] | passed | 0.00046 seconds |
|
29
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:2] | passed | 0.00042 seconds |
|
30
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:3] | passed | 0.00043 seconds |
|
31
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:1] | passed | 0.00046 seconds |
|
32
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:2] | passed | 0.00046 seconds |
|
33
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:3] | passed | 0.00044 seconds |
|
34
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:1] | passed | 0.00045 seconds |
|
35
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:2] | passed | 0.00042 seconds |
|
36
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:3] | passed | 0.00046 seconds |
|
37
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:1:1] | passed | 0.10949 seconds |
|
38
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:2:1] | passed | 0.22067 seconds |
|
39
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:3:1] | passed | 0.11422 seconds |
|
40
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:4:1:1] | passed | 0.01368 seconds |
|
41
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:4:2:1] | passed | 0.01557 seconds |
|
42
|
+
./spec/defra_ruby/validators/configuration_spec.rb[1:1:1] | passed | 0.0014 seconds |
|
43
|
+
./spec/defra_ruby/validators/configuration_spec.rb[1:2] | passed | 0.00045 seconds |
|
44
|
+
./spec/defra_ruby/validators/configuration_spec.rb[1:3:1:1] | passed | 0.00113 seconds |
|
45
|
+
./spec/defra_ruby/validators/configuration_spec.rb[1:3:2:1] | passed | 0.00015 seconds |
|
46
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:1:1] | passed | 0.0001 seconds |
|
47
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:2:1] | passed | 0.0001 seconds |
|
48
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:2:2:1:1:1] | passed | 0.00014 seconds |
|
49
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:2:2:1:1:2] | passed | 0.00013 seconds |
|
50
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:2:2:2:1:1:1] | passed | 0.00018 seconds |
|
51
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:2:2:2:1:1:2] | passed | 0.00019 seconds |
|
52
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:2:2:2:1:1:3] | passed | 0.00023 seconds |
|
53
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:3:1] | passed | 0.00013 seconds |
|
54
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:3:2:1:1:1] | passed | 0.00017 seconds |
|
55
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:3:2:1:1:2] | passed | 0.00015 seconds |
|
56
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:3:2:2:1:1:1] | passed | 0.00021 seconds |
|
57
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:3:2:2:1:1:2] | passed | 0.00019 seconds |
|
58
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:3:2:2:1:1:3] | passed | 0.0003 seconds |
|
59
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:4:1:1:1:1] | passed | 0.00014 seconds |
|
60
|
+
./spec/defra_ruby/validators/position_validator_spec.rb[1:4:1:1:1:2] | passed | 0.00018 seconds |
|
61
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:1:1] | passed | 0.00009 seconds |
|
62
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:2:1] | passed | 0.00012 seconds |
|
63
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:2:2:1:1:1] | passed | 0.0002 seconds |
|
64
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:2:2:1:1:2] | passed | 0.00012 seconds |
|
65
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:2:2:2:1:1:1] | passed | 0.00018 seconds |
|
66
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:2:2:2:1:1:2] | passed | 0.00014 seconds |
|
67
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:2:2:2:1:1:3] | passed | 0.00017 seconds |
|
68
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:3:1:1:1:1] | passed | 0.00018 seconds |
|
69
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:3:1:1:1:2] | passed | 0.00016 seconds |
|
70
|
+
./spec/defra_ruby/validators/token_validator_spec.rb[1:3:1:1:1:3] | passed | 0.00015 seconds |
|
71
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:1:1] | passed | 0.0001 seconds |
|
72
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:1] | passed | 0.0001 seconds |
|
73
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:1:1:1] | passed | 0.00014 seconds |
|
74
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:1:1:2] | passed | 0.00015 seconds |
|
75
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:1:1:1] | passed | 0.00017 seconds |
|
76
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:1:1:2] | passed | 0.00016 seconds |
|
77
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:1:1:3] | passed | 0.00024 seconds |
|
78
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:2:1:1] | passed | 0.00019 seconds |
|
79
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:2:1:2] | passed | 0.00018 seconds |
|
80
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:2:1:3] | passed | 0.00019 seconds |
|
81
|
+
./spec/defra_ruby/validators_spec.rb[1:1:1] | passed | 0.001 seconds |
|
data/spec/spec_helper.rb
CHANGED
@@ -6,6 +6,10 @@ require "bundler/setup"
|
|
6
6
|
# This is as per its docs https://github.com/colszowka/simplecov#getting-started
|
7
7
|
require "./spec/support/simplecov"
|
8
8
|
|
9
|
+
# Load env vars from a text file. This must be done before we load the other
|
10
|
+
# support files as some of them rely on the env vars being set.
|
11
|
+
require "dotenv/load"
|
12
|
+
|
9
13
|
# Requires supporting ruby files with custom matchers and macros, etc, in
|
10
14
|
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
|
11
15
|
# run as spec files by default. This means that files in spec/support that end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Helpers
|
4
|
+
module TextGenerator
|
5
|
+
def self.random_string(length)
|
6
|
+
random_string_from_sequence([("a".."z"), ("A".."Z")].map(&:to_a).flatten, length)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.random_number_string(length)
|
10
|
+
random_string_from_sequence(("0".."9").to_a, length)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.random_string_from_sequence(sequence, length)
|
14
|
+
(0...length).map { sequence[rand(sequence.length)] }.join
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -2,11 +2,10 @@
|
|
2
2
|
|
3
3
|
module Helpers
|
4
4
|
module Translator
|
5
|
-
def self.error_message(klass
|
5
|
+
def self.error_message(klass, attribute, error)
|
6
6
|
class_name = klass_name(klass)
|
7
|
-
return I18n.t("defra_ruby.validators.#{class_name}.#{attribute}.#{error}") if attribute
|
8
7
|
|
9
|
-
I18n.t("defra_ruby.validators.#{class_name}.#{error}")
|
8
|
+
I18n.t("defra_ruby.validators.#{class_name}.#{attribute}.#{error}")
|
10
9
|
end
|
11
10
|
|
12
11
|
def self.klass_name(klass)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.shared_examples "a characters validator" do |validator, validatable_class, attribute, values|
|
4
|
+
it "includes CanValidateCharacters" do
|
5
|
+
included_modules = described_class.ancestors.select { |ancestor| ancestor.instance_of?(Module) }
|
6
|
+
|
7
|
+
expect(included_modules)
|
8
|
+
.to include(DefraRuby::Validators::CanValidateCharacters)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#validate_each" do
|
12
|
+
context "when the #{attribute} is valid" do
|
13
|
+
it_behaves_like "a valid record", validatable_class.new(values[:valid])
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when the #{attribute} is not valid" do
|
17
|
+
context "because the #{attribute} is not correctly formatted" do
|
18
|
+
validatable = validatable_class.new(values[:invalid])
|
19
|
+
error_message = Helpers::Translator.error_message(validator, attribute, :invalid)
|
20
|
+
|
21
|
+
it_behaves_like "an invalid record", validatable, attribute, error_message
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,18 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
RSpec.shared_examples "an invalid record" do |validatable,
|
3
|
+
RSpec.shared_examples "an invalid record" do |validatable, attribute, error_message|
|
4
4
|
it "confirms the object is invalid" do
|
5
5
|
expect(validatable).to_not be_valid
|
6
6
|
end
|
7
7
|
|
8
8
|
it "adds a single validation error to the record" do
|
9
9
|
validatable.valid?
|
10
|
-
expect(validatable.errors[
|
10
|
+
expect(validatable.errors[attribute].count).to eq(1)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "adds an appropriate validation error" do
|
14
14
|
validatable.valid?
|
15
15
|
expect(error_message).to_not include("translation missing:")
|
16
|
-
expect(validatable.errors[
|
16
|
+
expect(validatable.errors[attribute]).to eq([error_message])
|
17
|
+
end
|
18
|
+
|
19
|
+
context "when there is a custom error message" do
|
20
|
+
let(:custom_message) { "something is wrong (in a customised way)" }
|
21
|
+
before { allow_any_instance_of(DefraRuby::Validators::BaseValidator).to receive(:options).and_return(message: custom_message) }
|
22
|
+
|
23
|
+
it "uses the custom message instead of the default" do
|
24
|
+
validatable.valid?
|
25
|
+
expect(validatable.errors[attribute]).to eq([custom_message])
|
26
|
+
end
|
17
27
|
end
|
18
28
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.shared_examples "a length validator" do |validator, validatable_class, attribute, values|
|
4
|
+
it "includes CanValidateLength" do
|
5
|
+
included_modules = described_class.ancestors.select { |ancestor| ancestor.instance_of?(Module) }
|
6
|
+
|
7
|
+
expect(included_modules)
|
8
|
+
.to include(DefraRuby::Validators::CanValidateLength)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#validate_each" do
|
12
|
+
context "when the #{attribute} is valid" do
|
13
|
+
it_behaves_like "a valid record", validatable_class.new(values[:valid])
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when the #{attribute} is not valid" do
|
17
|
+
context "because the #{attribute} is too long" do
|
18
|
+
validatable = validatable_class.new(values[:invalid])
|
19
|
+
error_message = Helpers::Translator.error_message(validator, attribute, :too_long)
|
20
|
+
|
21
|
+
it_behaves_like "an invalid record", validatable, attribute, error_message
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|