defra_ruby_validators 2.6.0 → 2.7.0
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.
- checksums.yaml +4 -4
- data/config/locales/defra_ruby/validators/companies_house_number_validator/en.yml +2 -0
- data/config/locales/defra_ruby/validators/future_date_validator/en.yml +5 -0
- data/lib/defra_ruby/validators/companies_house_number_validator.rb +8 -5
- data/lib/defra_ruby/validators/companies_house_service.rb +26 -9
- data/lib/defra_ruby/validators/future_date_validator.rb_park +21 -0
- data/lib/defra_ruby/validators/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8cb145644c2a51c737b951282707e436d9b740ab756c04f93d761cf05cbc91a6
|
4
|
+
data.tar.gz: 45ce24942e9e95030e8d051f06db0f20facff88f2320445b96b7c695a35b17f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c6c390badabbd364ed4174746857062d0f85d92405981c2b83e72498e493c6440e5663bc8ade16bc0d87534da467db4ea1a9dcb1871f9b1cc7f810dede007fb
|
7
|
+
data.tar.gz: 31404af40d4a8da9da8c726bb9b081d6d9838eb0843ede3042b270d9dd7036c5810f00becde67f8fcdb0e723ea8f79883b970a8eec38a9eea581b31d9b93c376
|
@@ -6,4 +6,6 @@ en:
|
|
6
6
|
invalid_format: "Enter a valid number - it should have 8 digits, or 2 letters followed by 6 digits, or 2 letters followed by 5 digits and another letter. If your number has only 7 digits, enter it with a zero at the start."
|
7
7
|
not_found: Companies House couldn't find a company with this number
|
8
8
|
inactive: Your company must be registered as an active company
|
9
|
+
unsupported_company_type: Your company must be registered as a limited company or a limited liability partnership
|
10
|
+
argument_error: Invalid permitted_types value. It must be nil, a string or an array of strings.
|
9
11
|
error: There was an error connecting with Companies House. Hopefully this is a one off and will work if you try again.
|
@@ -16,8 +16,8 @@ module DefraRuby
|
|
16
16
|
return false unless value_is_present?(record, attribute, value)
|
17
17
|
return false unless format_is_valid?(record, attribute, value)
|
18
18
|
|
19
|
-
|
20
|
-
validate_with_companies_house(record, attribute, value,
|
19
|
+
permitted_types = options[:permitted_types]
|
20
|
+
validate_with_companies_house(record, attribute, value, permitted_types)
|
21
21
|
end
|
22
22
|
|
23
23
|
private
|
@@ -37,18 +37,21 @@ module DefraRuby
|
|
37
37
|
false
|
38
38
|
end
|
39
39
|
|
40
|
-
def validate_with_companies_house(record, attribute, value,
|
41
|
-
case CompaniesHouseService.new(value,
|
40
|
+
def validate_with_companies_house(record, attribute, value, permitted_types)
|
41
|
+
case CompaniesHouseService.new(company_number: value, permitted_types:).status
|
42
42
|
when :active
|
43
43
|
true
|
44
44
|
when :inactive
|
45
45
|
add_validation_error(record, attribute, :inactive)
|
46
46
|
when :not_found
|
47
47
|
add_validation_error(record, attribute, :not_found)
|
48
|
+
when :unsupported_company_type
|
49
|
+
add_validation_error(record, attribute, :unsupported_company_type)
|
48
50
|
else
|
49
|
-
# Sonarcloud suggested that not having an `else` is a code smell
|
50
51
|
add_validation_error(record, attribute, :error)
|
51
52
|
end
|
53
|
+
rescue ArgumentError
|
54
|
+
add_validation_error(record, attribute, :argument_error)
|
52
55
|
rescue StandardError
|
53
56
|
add_validation_error(record, attribute, :error)
|
54
57
|
end
|
@@ -5,15 +5,18 @@ require "rest-client"
|
|
5
5
|
module DefraRuby
|
6
6
|
module Validators
|
7
7
|
class CompaniesHouseService
|
8
|
-
def initialize(
|
9
|
-
@
|
10
|
-
@
|
8
|
+
def initialize(company_number:, permitted_types: nil)
|
9
|
+
@company_number = company_number
|
10
|
+
@permitted_types = permitted_types
|
11
|
+
|
12
|
+
validate_permitted_types
|
13
|
+
|
14
|
+
@url = "#{DefraRuby::Validators.configuration.companies_house_host}#{@company_number}"
|
11
15
|
@api_key = DefraRuby::Validators.configuration.companies_house_api_key
|
12
|
-
@company_type = company_type
|
13
16
|
end
|
14
17
|
|
15
18
|
def status
|
16
|
-
return :
|
19
|
+
return :unsupported_company_type unless company_type_is_allowed?(json_response)
|
17
20
|
|
18
21
|
status_is_allowed?(json_response) ? :active : :inactive
|
19
22
|
rescue RestClient::ResourceNotFound
|
@@ -34,16 +37,30 @@ module DefraRuby
|
|
34
37
|
|
35
38
|
private
|
36
39
|
|
40
|
+
def validate_permitted_types
|
41
|
+
return if @permitted_types.nil?
|
42
|
+
|
43
|
+
return if @permitted_types.is_a?(String) || @permitted_types.is_a?(Array)
|
44
|
+
|
45
|
+
raise ArgumentError, I18n.t("defra_ruby.validators.CompaniesHouseNumberValidator.argument_error")
|
46
|
+
end
|
47
|
+
|
37
48
|
def status_is_allowed?(json_response)
|
38
49
|
%w[active voluntary-arrangement].include?(json_response["company_status"])
|
39
50
|
end
|
40
51
|
|
41
52
|
def company_type_is_allowed?(json_response)
|
42
|
-
# if
|
43
|
-
|
44
|
-
return true if @company_type.blank?
|
53
|
+
# if permitted_types has not been defined in the validator, we skip this check
|
54
|
+
return true if @permitted_types.blank?
|
45
55
|
|
46
|
-
@
|
56
|
+
case @permitted_types
|
57
|
+
when String
|
58
|
+
@permitted_types.to_s == json_response["type"]
|
59
|
+
when Array
|
60
|
+
@permitted_types.include?(json_response["type"])
|
61
|
+
else
|
62
|
+
raise ArgumentError, I18n.t("defra_ruby.validators.CompaniesHouseNumberValidator.argument_error")
|
63
|
+
end
|
47
64
|
end
|
48
65
|
end
|
49
66
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DefraRuby
|
4
|
+
module Validators
|
5
|
+
class FutureDateValidator < BaseValidator
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
return false if value.blank?
|
8
|
+
|
9
|
+
date = value.to_date
|
10
|
+
|
11
|
+
if date <= Date.today
|
12
|
+
add_validation_error(record, attribute, :future_date)
|
13
|
+
|
14
|
+
return false
|
15
|
+
end
|
16
|
+
|
17
|
+
true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
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: 2.
|
4
|
+
version: 2.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Defra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -137,6 +137,7 @@ files:
|
|
137
137
|
- config/locales/defra_ruby/validators/business_type_validator/en.yml
|
138
138
|
- config/locales/defra_ruby/validators/companies_house_number_validator/en.yml
|
139
139
|
- config/locales/defra_ruby/validators/email_validator/en.yml
|
140
|
+
- config/locales/defra_ruby/validators/future_date_validator/en.yml
|
140
141
|
- config/locales/defra_ruby/validators/grid_reference_validator/en.yml
|
141
142
|
- config/locales/defra_ruby/validators/location_validator/en.yml
|
142
143
|
- config/locales/defra_ruby/validators/mobile_phone_number_validator/en.yml
|
@@ -158,6 +159,7 @@ files:
|
|
158
159
|
- lib/defra_ruby/validators/configuration.rb
|
159
160
|
- lib/defra_ruby/validators/email_validator.rb
|
160
161
|
- lib/defra_ruby/validators/engine.rb
|
162
|
+
- lib/defra_ruby/validators/future_date_validator.rb_park
|
161
163
|
- lib/defra_ruby/validators/grid_reference_validator.rb
|
162
164
|
- lib/defra_ruby/validators/location_validator.rb
|
163
165
|
- lib/defra_ruby/validators/mobile_phone_number_validator.rb
|