defra_ruby_validators 1.0.0 → 1.1.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 +10 -0
- data/config/locales/defra_ruby/validators/true_false_validator/en.yml +5 -0
- data/lib/defra_ruby/validators.rb +3 -0
- data/lib/defra_ruby/validators/base_validator.rb +4 -2
- data/lib/defra_ruby/validators/companies_house_number_validator.rb +5 -5
- data/lib/defra_ruby/validators/concerns/can_validate_selection.rb +20 -0
- data/lib/defra_ruby/validators/true_false_validator.rb +15 -0
- data/lib/defra_ruby/validators/version.rb +1 -1
- data/spec/cassettes/company_no_inactive.yml +5 -5
- data/spec/cassettes/company_no_not_found.yml +4 -4
- data/spec/cassettes/company_no_valid.yml +5 -5
- data/spec/defra_ruby/validators/companies_house_number_validator_spec.rb +25 -5
- data/spec/defra_ruby/validators/true_false_validator_spec.rb +23 -0
- data/spec/examples.txt +44 -34
- data/spec/support/helpers/translator.rb +4 -2
- data/spec/support/shared_examples/validators/selection_validator.rb +32 -0
- metadata +23 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e898f7ee267a5ce4074c4f7c4c04284f3119732
|
4
|
+
data.tar.gz: fd9c073dc217d74a6468bc25261f6d9338bc24c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d97d8bd20be2314faa28da05224288eb90533113b419d6df3f4d1b0801a9a7f76ddd8e34a3df87b4f7dba7580317e55e315176c5aff38152007d502d49b69d2d
|
7
|
+
data.tar.gz: 29e0b534deb7e2801ec556c47488846e5be7edaa0f28b80e7433edf3ae69a858504d3f9158084bddd3946b97e5d28aa7b1630853c36f855565337f6f9b422195
|
data/README.md
CHANGED
@@ -48,6 +48,16 @@ Add it to your model or form object using
|
|
48
48
|
validates :company_no, "defra_ruby/validators/companies_house_number": true
|
49
49
|
```
|
50
50
|
|
51
|
+
### True or false
|
52
|
+
|
53
|
+
This validator checks the value provided is either `"true"` or `"false"`. If blank or not one of these it will return an error.
|
54
|
+
|
55
|
+
Add it to your model or form object using
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
validates :company_no, "defra_ruby/validators/true_false": true
|
59
|
+
```
|
60
|
+
|
51
61
|
## Contributing to this project
|
52
62
|
|
53
63
|
If you have an idea you'd like to contribute please log an issue.
|
@@ -6,8 +6,11 @@ require_relative "validators/version"
|
|
6
6
|
require_relative "validators/configuration"
|
7
7
|
require_relative "validators/companies_house_service"
|
8
8
|
|
9
|
+
require_relative "validators/concerns/can_validate_selection"
|
10
|
+
|
9
11
|
require_relative "validators/base_validator"
|
10
12
|
require_relative "validators/companies_house_number_validator"
|
13
|
+
require_relative "validators/true_false_validator"
|
11
14
|
|
12
15
|
module DefraRuby
|
13
16
|
module Validators
|
@@ -6,8 +6,10 @@ module DefraRuby
|
|
6
6
|
|
7
7
|
protected
|
8
8
|
|
9
|
-
def error_message(attribute, error)
|
10
|
-
I18n.t("defra_ruby.validators.#{class_name}.#{attribute}.#{error}")
|
9
|
+
def error_message(attribute: nil, error:)
|
10
|
+
return I18n.t("defra_ruby.validators.#{class_name}.#{attribute}.#{error}") if attribute
|
11
|
+
|
12
|
+
I18n.t("defra_ruby.validators.#{class_name}.#{error}")
|
11
13
|
end
|
12
14
|
|
13
15
|
private
|
@@ -25,14 +25,14 @@ module DefraRuby
|
|
25
25
|
def value_is_present?(record, attribute, value)
|
26
26
|
return true if value.present?
|
27
27
|
|
28
|
-
record.errors[attribute] << error_message(attribute, "blank")
|
28
|
+
record.errors[attribute] << error_message(attribute: attribute, error: "blank")
|
29
29
|
false
|
30
30
|
end
|
31
31
|
|
32
32
|
def format_is_valid?(record, attribute, value)
|
33
33
|
return true if value.match?(VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX)
|
34
34
|
|
35
|
-
record.errors[attribute] << error_message(attribute, "invalid")
|
35
|
+
record.errors[attribute] << error_message(attribute: attribute, error: "invalid")
|
36
36
|
false
|
37
37
|
end
|
38
38
|
|
@@ -41,12 +41,12 @@ module DefraRuby
|
|
41
41
|
when :active
|
42
42
|
true
|
43
43
|
when :inactive
|
44
|
-
record.errors[attribute] << error_message(attribute, "inactive")
|
44
|
+
record.errors[attribute] << error_message(attribute: attribute, error: "inactive")
|
45
45
|
when :not_found
|
46
|
-
record.errors[attribute] << error_message(attribute, "not_found")
|
46
|
+
record.errors[attribute] << error_message(attribute: attribute, error: "not_found")
|
47
47
|
end
|
48
48
|
rescue StandardError
|
49
|
-
record.errors[attribute] << error_message(attribute, "error")
|
49
|
+
record.errors[attribute] << error_message(attribute: attribute, error: "error")
|
50
50
|
end
|
51
51
|
|
52
52
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DefraRuby
|
4
|
+
module Validators
|
5
|
+
module CanValidateSelection
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
private
|
10
|
+
|
11
|
+
def value_is_included?(record, attribute, value, valid_options)
|
12
|
+
return true if value.present? && valid_options.include?(value)
|
13
|
+
|
14
|
+
record.errors[attribute] << error_message(error: "inclusion")
|
15
|
+
false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DefraRuby
|
4
|
+
module Validators
|
5
|
+
class TrueFalseValidator < BaseValidator
|
6
|
+
include CanValidateSelection
|
7
|
+
|
8
|
+
def validate_each(record, attribute, value)
|
9
|
+
valid_options = %w[true false]
|
10
|
+
|
11
|
+
value_is_included?(record, attribute, value, valid_options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -12,7 +12,7 @@ http_interactions:
|
|
12
12
|
Accept-Encoding:
|
13
13
|
- gzip, deflate
|
14
14
|
User-Agent:
|
15
|
-
- rest-client/2.0.2 (darwin18.
|
15
|
+
- rest-client/2.0.2 (darwin18.2.0 x86_64) ruby/2.4.2p198
|
16
16
|
Host:
|
17
17
|
- api.companieshouse.gov.uk
|
18
18
|
Authorization:
|
@@ -39,7 +39,7 @@ http_interactions:
|
|
39
39
|
Content-Type:
|
40
40
|
- application/json
|
41
41
|
Date:
|
42
|
-
-
|
42
|
+
- Tue, 04 Jun 2019 22:42:41 GMT
|
43
43
|
Pragma:
|
44
44
|
- no-cache
|
45
45
|
Server:
|
@@ -47,9 +47,9 @@ http_interactions:
|
|
47
47
|
X-Ratelimit-Limit:
|
48
48
|
- '600'
|
49
49
|
X-Ratelimit-Remain:
|
50
|
-
- '
|
50
|
+
- '599'
|
51
51
|
X-Ratelimit-Reset:
|
52
|
-
- '
|
52
|
+
- '1559688461'
|
53
53
|
X-Ratelimit-Window:
|
54
54
|
- 5m
|
55
55
|
Content-Length:
|
@@ -63,5 +63,5 @@ http_interactions:
|
|
63
63
|
Elizabeth House","address_line_2":"54-58 High Street"},"undeliverable_registered_office_address":false,"company_name":"DIRECT
|
64
64
|
SKIPS UK LTD","annual_return":{"last_made_up_to":"2014-06-11"},"jurisdiction":"england-wales","etag":"1cdef5bc2a020b3e9003b086033b64b78bcb28f6","company_status":"dissolved","has_insolvency_history":false,"has_charges":false,"links":{"self":"/company/07281919","filing_history":"/company/07281919/filing-history","officers":"/company/07281919/officers"},"date_of_cessation":"2016-01-05","can_file":false}'
|
65
65
|
http_version:
|
66
|
-
recorded_at:
|
66
|
+
recorded_at: Tue, 04 Jun 2019 22:42:41 GMT
|
67
67
|
recorded_with: VCR 4.0.0
|
@@ -12,7 +12,7 @@ http_interactions:
|
|
12
12
|
Accept-Encoding:
|
13
13
|
- gzip, deflate
|
14
14
|
User-Agent:
|
15
|
-
- rest-client/2.0.2 (darwin18.
|
15
|
+
- rest-client/2.0.2 (darwin18.2.0 x86_64) ruby/2.4.2p198
|
16
16
|
Host:
|
17
17
|
- api.companieshouse.gov.uk
|
18
18
|
Authorization:
|
@@ -39,7 +39,7 @@ http_interactions:
|
|
39
39
|
Content-Type:
|
40
40
|
- application/json
|
41
41
|
Date:
|
42
|
-
-
|
42
|
+
- Tue, 04 Jun 2019 22:42:41 GMT
|
43
43
|
Pragma:
|
44
44
|
- no-cache
|
45
45
|
Server:
|
@@ -49,7 +49,7 @@ http_interactions:
|
|
49
49
|
X-Ratelimit-Remain:
|
50
50
|
- '598'
|
51
51
|
X-Ratelimit-Reset:
|
52
|
-
- '
|
52
|
+
- '1559688461'
|
53
53
|
X-Ratelimit-Window:
|
54
54
|
- 5m
|
55
55
|
Content-Length:
|
@@ -60,5 +60,5 @@ http_interactions:
|
|
60
60
|
encoding: UTF-8
|
61
61
|
string: '{"errors":[{"type":"ch:service","error":"company-profile-not-found"}]}'
|
62
62
|
http_version:
|
63
|
-
recorded_at:
|
63
|
+
recorded_at: Tue, 04 Jun 2019 22:42:41 GMT
|
64
64
|
recorded_with: VCR 4.0.0
|
@@ -12,7 +12,7 @@ http_interactions:
|
|
12
12
|
Accept-Encoding:
|
13
13
|
- gzip, deflate
|
14
14
|
User-Agent:
|
15
|
-
- rest-client/2.0.2 (darwin18.
|
15
|
+
- rest-client/2.0.2 (darwin18.2.0 x86_64) ruby/2.4.2p198
|
16
16
|
Host:
|
17
17
|
- api.companieshouse.gov.uk
|
18
18
|
Authorization:
|
@@ -39,7 +39,7 @@ http_interactions:
|
|
39
39
|
Content-Type:
|
40
40
|
- application/json
|
41
41
|
Date:
|
42
|
-
-
|
42
|
+
- Tue, 04 Jun 2019 22:42:41 GMT
|
43
43
|
Pragma:
|
44
44
|
- no-cache
|
45
45
|
Server:
|
@@ -47,9 +47,9 @@ http_interactions:
|
|
47
47
|
X-Ratelimit-Limit:
|
48
48
|
- '600'
|
49
49
|
X-Ratelimit-Remain:
|
50
|
-
- '
|
50
|
+
- '597'
|
51
51
|
X-Ratelimit-Reset:
|
52
|
-
- '
|
52
|
+
- '1559688461'
|
53
53
|
X-Ratelimit-Window:
|
54
54
|
- 5m
|
55
55
|
Content-Length:
|
@@ -61,5 +61,5 @@ http_interactions:
|
|
61
61
|
string: '{"type":"ltd","company_name":"0800 WASTE LTD.","has_insolvency_history":false,"accounts":{"next_due":"2019-09-30","next_made_up_to":"2018-12-31","next_accounts":{"overdue":false,"period_start_on":"2018-01-01","due_on":"2019-09-30","period_end_on":"2018-12-31"},"accounting_reference_date":{"month":"12","day":"31"},"last_accounts":{"period_end_on":"2017-12-31","period_start_on":"2017-01-01","made_up_to":"2017-12-31"},"overdue":false},"undeliverable_registered_office_address":false,"etag":"0ec9d00cab0ffee2ef1f5d59f4f714fa5b1618f5","company_number":"09360070","registered_office_address":{"postal_code":"SM3
|
62
62
|
9ND","locality":"Sutton","region":"Surrey","address_line_1":"21 Haslam Avenue"},"jurisdiction":"england-wales","date_of_creation":"2014-12-18","company_status":"active","has_charges":false,"sic_codes":["38110"],"last_full_members_list_date":"2015-12-18","confirmation_statement":{"overdue":false,"next_made_up_to":"2020-01-23","last_made_up_to":"2019-01-23","next_due":"2020-02-06"},"links":{"self":"/company/09360070","filing_history":"/company/09360070/filing-history","officers":"/company/09360070/officers"},"registered_office_is_in_dispute":false,"can_file":true}'
|
63
63
|
http_version:
|
64
|
-
recorded_at:
|
64
|
+
recorded_at: Tue, 04 Jun 2019 22:42:41 GMT
|
65
65
|
recorded_with: VCR 4.0.0
|
@@ -37,14 +37,22 @@ module DefraRuby
|
|
37
37
|
context "when given an invalid company number" do
|
38
38
|
context "because it is blank" do
|
39
39
|
validatable = Test::CompaniesHouseNumberValidatable.new
|
40
|
-
error_message = Helpers::Translator.error_message(
|
40
|
+
error_message = Helpers::Translator.error_message(
|
41
|
+
klass: CompaniesHouseNumberValidator,
|
42
|
+
attribute: :company_no,
|
43
|
+
error: :blank
|
44
|
+
)
|
41
45
|
|
42
46
|
it_behaves_like "an invalid record", validatable, :company_no, error_message
|
43
47
|
end
|
44
48
|
|
45
49
|
context "because the format is wrong" do
|
46
50
|
validatable = Test::CompaniesHouseNumberValidatable.new(invalid_format_number)
|
47
|
-
error_message = Helpers::Translator.error_message(
|
51
|
+
error_message = Helpers::Translator.error_message(
|
52
|
+
klass: CompaniesHouseNumberValidator,
|
53
|
+
attribute: :company_no,
|
54
|
+
error: :invalid
|
55
|
+
)
|
48
56
|
|
49
57
|
it_behaves_like "an invalid record", validatable, :company_no, error_message
|
50
58
|
end
|
@@ -55,7 +63,11 @@ module DefraRuby
|
|
55
63
|
end
|
56
64
|
|
57
65
|
validatable = Test::CompaniesHouseNumberValidatable.new(unknown_number)
|
58
|
-
error_message = Helpers::Translator.error_message(
|
66
|
+
error_message = Helpers::Translator.error_message(
|
67
|
+
klass: CompaniesHouseNumberValidator,
|
68
|
+
attribute: :company_no,
|
69
|
+
error: :not_found
|
70
|
+
)
|
59
71
|
|
60
72
|
it_behaves_like "an invalid record", validatable, :company_no, error_message
|
61
73
|
end
|
@@ -66,7 +78,11 @@ module DefraRuby
|
|
66
78
|
end
|
67
79
|
|
68
80
|
validatable = Test::CompaniesHouseNumberValidatable.new(inactive_number)
|
69
|
-
error_message = Helpers::Translator.error_message(
|
81
|
+
error_message = Helpers::Translator.error_message(
|
82
|
+
klass: CompaniesHouseNumberValidator,
|
83
|
+
attribute: :company_no,
|
84
|
+
error: :inactive
|
85
|
+
)
|
70
86
|
|
71
87
|
it_behaves_like "an invalid record", validatable, :company_no, error_message
|
72
88
|
end
|
@@ -78,7 +94,11 @@ module DefraRuby
|
|
78
94
|
end
|
79
95
|
|
80
96
|
validatable = Test::CompaniesHouseNumberValidatable.new(valid_numbers.sample)
|
81
|
-
error_message = Helpers::Translator.error_message(
|
97
|
+
error_message = Helpers::Translator.error_message(
|
98
|
+
klass: CompaniesHouseNumberValidator,
|
99
|
+
attribute: :company_no,
|
100
|
+
error: :error
|
101
|
+
)
|
82
102
|
|
83
103
|
it_behaves_like "an invalid record", validatable, :company_no, error_message
|
84
104
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
module Test
|
6
|
+
TrueFalseValidatable = Struct.new(:response) do
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
validates :response, "defra_ruby/validators/true_false": true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module DefraRuby
|
14
|
+
module Validators
|
15
|
+
RSpec.describe TrueFalseValidator do
|
16
|
+
|
17
|
+
valid_value = %w[true false].sample
|
18
|
+
|
19
|
+
it_behaves_like "a validator"
|
20
|
+
it_behaves_like "a selection validator", TrueFalseValidator, Test::TrueFalseValidatable, :response, valid_value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/examples.txt
CHANGED
@@ -1,36 +1,46 @@
|
|
1
1
|
example_id | status | run_time |
|
2
2
|
---------------------------------------------------------------------------------- | ------ | --------------- |
|
3
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:1:1] | passed | 0.
|
4
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:1:1:1] | passed | 0.
|
5
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:1:1:2] | passed | 0.
|
6
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:2:1:1] | passed | 0.
|
7
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:2:1:2] | passed | 0.
|
8
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:3:1:1] | passed | 0.
|
9
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:3:1:2] | passed | 0.
|
10
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:4:1:1] | passed | 0.
|
11
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:4:1:2] | passed | 0.
|
12
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:1] | passed | 0.
|
13
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:2] | passed | 0.
|
14
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:3] | passed | 0.
|
15
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:1] | passed | 0.
|
16
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:2] | passed | 0.
|
17
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:3] | passed | 0.
|
18
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:1] | passed | 0.
|
19
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:2] | passed | 0.
|
20
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:3] | passed | 0.
|
21
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:1] | passed | 0.
|
22
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:2] | passed | 0.
|
23
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:3] | passed | 0.
|
24
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:1] | passed | 0.
|
25
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:2] | passed | 0.
|
26
|
-
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:3] | passed | 0.
|
27
|
-
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:1:1] | passed | 0.
|
28
|
-
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:2:1] | passed | 0.
|
29
|
-
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:3:1] | passed | 0.
|
30
|
-
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:4:1:1] | passed | 0.
|
31
|
-
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:4:2:1] | passed | 0.
|
32
|
-
./spec/defra_ruby/validators/configuration_spec.rb[1:1:1] | passed | 0.
|
33
|
-
./spec/defra_ruby/validators/configuration_spec.rb[1:2] | passed | 0.
|
34
|
-
./spec/defra_ruby/validators/configuration_spec.rb[1:3:1:1] | passed | 0.
|
35
|
-
./spec/defra_ruby/validators/configuration_spec.rb[1:3:2:1] | passed | 0.
|
36
|
-
./spec/defra_ruby/
|
3
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:1:1] | passed | 0.00016 seconds |
|
4
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:1:1:1] | passed | 0.00072 seconds |
|
5
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:1:1:2] | passed | 0.00079 seconds |
|
6
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:2:1:1] | passed | 0.00082 seconds |
|
7
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:2:1:2] | passed | 0.00081 seconds |
|
8
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:3:1:1] | passed | 0.00065 seconds |
|
9
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:3:1:2] | passed | 0.00062 seconds |
|
10
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:4:1:1] | passed | 0.00131 seconds |
|
11
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:4:1:2] | passed | 0.00086 seconds |
|
12
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:1] | passed | 0.00029 seconds |
|
13
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:2] | passed | 0.00023 seconds |
|
14
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:3] | passed | 0.0003 seconds |
|
15
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:1] | passed | 0.00026 seconds |
|
16
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:2] | passed | 0.00021 seconds |
|
17
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:3] | passed | 0.00028 seconds |
|
18
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:1] | passed | 0.00081 seconds |
|
19
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:2] | passed | 0.00102 seconds |
|
20
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:3] | passed | 0.00073 seconds |
|
21
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:1] | passed | 0.001 seconds |
|
22
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:2] | passed | 0.00071 seconds |
|
23
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:3] | passed | 0.00137 seconds |
|
24
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:1] | passed | 0.00503 seconds |
|
25
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:2] | passed | 0.00091 seconds |
|
26
|
+
./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:3] | passed | 0.07132 seconds |
|
27
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:1:1] | passed | 0.03655 seconds |
|
28
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:2:1] | passed | 0.02028 seconds |
|
29
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:3:1] | passed | 0.02097 seconds |
|
30
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:4:1:1] | passed | 0.03174 seconds |
|
31
|
+
./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:4:2:1] | passed | 0.01659 seconds |
|
32
|
+
./spec/defra_ruby/validators/configuration_spec.rb[1:1:1] | passed | 0.0146 seconds |
|
33
|
+
./spec/defra_ruby/validators/configuration_spec.rb[1:2] | passed | 0.006 seconds |
|
34
|
+
./spec/defra_ruby/validators/configuration_spec.rb[1:3:1:1] | passed | 0.0008 seconds |
|
35
|
+
./spec/defra_ruby/validators/configuration_spec.rb[1:3:2:1] | passed | 0.00518 seconds |
|
36
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:1:1] | passed | 0.00041 seconds |
|
37
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:1] | passed | 0.00031 seconds |
|
38
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:1:1:1] | passed | 0.00055 seconds |
|
39
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:1:1:2] | passed | 0.00029 seconds |
|
40
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:1:1:1] | passed | 0.00027 seconds |
|
41
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:1:1:2] | passed | 0.00027 seconds |
|
42
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:1:1:3] | passed | 0.00029 seconds |
|
43
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:2:1:1] | passed | 0.00026 seconds |
|
44
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:2:1:2] | passed | 0.00027 seconds |
|
45
|
+
./spec/defra_ruby/validators/true_false_validator_spec.rb[1:2:2:2:2:1:3] | passed | 0.00024 seconds |
|
46
|
+
./spec/defra_ruby/validators_spec.rb[1:1:1] | passed | 0.00956 seconds |
|
@@ -2,9 +2,11 @@
|
|
2
2
|
|
3
3
|
module Helpers
|
4
4
|
module Translator
|
5
|
-
def self.error_message(klass
|
5
|
+
def self.error_message(klass:, attribute: nil, error:)
|
6
6
|
class_name = klass_name(klass)
|
7
|
-
I18n.t("defra_ruby.validators.#{class_name}.#{attribute}.#{error}")
|
7
|
+
return I18n.t("defra_ruby.validators.#{class_name}.#{attribute}.#{error}") if attribute
|
8
|
+
|
9
|
+
I18n.t("defra_ruby.validators.#{class_name}.#{error}")
|
8
10
|
end
|
9
11
|
|
10
12
|
def self.klass_name(klass)
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.shared_examples "a selection validator" do |validator, validatable_class, property, valid_value|
|
4
|
+
it "includes CanValidateSelection" do
|
5
|
+
included_modules = described_class.ancestors.select { |ancestor| ancestor.instance_of?(Module) }
|
6
|
+
|
7
|
+
expect(included_modules)
|
8
|
+
.to include(DefraRuby::Validators::CanValidateSelection)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#validate_each" do
|
12
|
+
context "when the #{property} is valid" do
|
13
|
+
it_behaves_like "a valid record", validatable_class.new(valid_value)
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when the #{property} is not valid" do
|
17
|
+
context "because the #{property} is not present" do
|
18
|
+
validatable = validatable_class.new
|
19
|
+
error_message = Helpers::Translator.error_message(klass: validator, error: :inclusion)
|
20
|
+
|
21
|
+
it_behaves_like "an invalid record", validatable, property, error_message
|
22
|
+
end
|
23
|
+
|
24
|
+
context "because the #{property} is not from an approved list" do
|
25
|
+
validatable = validatable_class.new("unexpected_#{property}")
|
26
|
+
error_message = Helpers::Translator.error_message(klass: validator, error: :inclusion)
|
27
|
+
|
28
|
+
it_behaves_like "an invalid record", validatable, property, error_message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: defra_ruby_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Defra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05
|
11
|
+
date: 2019-06-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rest-client
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -205,12 +219,15 @@ files:
|
|
205
219
|
- bin/console
|
206
220
|
- bin/setup
|
207
221
|
- config/locales/defra_ruby/validators/companies_house_number_validator/en.yml
|
222
|
+
- config/locales/defra_ruby/validators/true_false_validator/en.yml
|
208
223
|
- lib/defra_ruby/validators.rb
|
209
224
|
- lib/defra_ruby/validators/base_validator.rb
|
210
225
|
- lib/defra_ruby/validators/companies_house_number_validator.rb
|
211
226
|
- lib/defra_ruby/validators/companies_house_service.rb
|
227
|
+
- lib/defra_ruby/validators/concerns/can_validate_selection.rb
|
212
228
|
- lib/defra_ruby/validators/configuration.rb
|
213
229
|
- lib/defra_ruby/validators/engine.rb
|
230
|
+
- lib/defra_ruby/validators/true_false_validator.rb
|
214
231
|
- lib/defra_ruby/validators/version.rb
|
215
232
|
- lib/defra_ruby_validators.rb
|
216
233
|
- spec/cassettes/company_no_inactive.yml
|
@@ -219,6 +236,7 @@ files:
|
|
219
236
|
- spec/defra_ruby/validators/companies_house_number_validator_spec.rb
|
220
237
|
- spec/defra_ruby/validators/companies_house_service_spec.rb
|
221
238
|
- spec/defra_ruby/validators/configuration_spec.rb
|
239
|
+
- spec/defra_ruby/validators/true_false_validator_spec.rb
|
222
240
|
- spec/defra_ruby/validators_spec.rb
|
223
241
|
- spec/examples.txt
|
224
242
|
- spec/spec_helper.rb
|
@@ -228,6 +246,7 @@ files:
|
|
228
246
|
- spec/support/i18n.rb
|
229
247
|
- spec/support/pry.rb
|
230
248
|
- spec/support/shared_examples/validators/invalid_record.rb
|
249
|
+
- spec/support/shared_examples/validators/selection_validator.rb
|
231
250
|
- spec/support/shared_examples/validators/valid_record.rb
|
232
251
|
- spec/support/shared_examples/validators/validator.rb
|
233
252
|
- spec/support/simplecov.rb
|
@@ -263,6 +282,7 @@ test_files:
|
|
263
282
|
- spec/defra_ruby/validators/companies_house_service_spec.rb
|
264
283
|
- spec/defra_ruby/validators/configuration_spec.rb
|
265
284
|
- spec/defra_ruby/validators/companies_house_number_validator_spec.rb
|
285
|
+
- spec/defra_ruby/validators/true_false_validator_spec.rb
|
266
286
|
- spec/examples.txt
|
267
287
|
- spec/cassettes/company_no_not_found.yml
|
268
288
|
- spec/cassettes/company_no_valid.yml
|
@@ -274,6 +294,7 @@ test_files:
|
|
274
294
|
- spec/support/dotenv.rb
|
275
295
|
- spec/support/defra_ruby_validators.rb
|
276
296
|
- spec/support/helpers/translator.rb
|
297
|
+
- spec/support/shared_examples/validators/selection_validator.rb
|
277
298
|
- spec/support/shared_examples/validators/invalid_record.rb
|
278
299
|
- spec/support/shared_examples/validators/valid_record.rb
|
279
300
|
- spec/support/shared_examples/validators/validator.rb
|