defra_ruby_validators 2.6.0 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e9394d56a860bc591e8858943843bae425845124bdca40f68f90fa864e3bbc6
4
- data.tar.gz: 259e137a79906fb23ca984322d0e7d87dfa117cd87f4b5f87df182a69fec8cd5
3
+ metadata.gz: fcc15d0837d498fd37fe5cfd1781d2b80b87165c0235b6e94aa50ad6463151a5
4
+ data.tar.gz: de144f02848b1b2c8de4ccb5bdb4ec9963697eed004ccf07106139333078936e
5
5
  SHA512:
6
- metadata.gz: 5336207fa6acbb53f0c36cb3759ea3d31e507de9392e2c0081bed9160bad765de799a88b6a8b4295daa80ec534f00ab158ae606e6592d624bbd8cfa48d34c69a
7
- data.tar.gz: c81e615d1b0337ff9e21380f80a91d50670ec5b95c950a35e346feda88822e34f0e9660f81b7e75d200b6b0fc83cff2d05af79e383b78123c7edab9fb46563e8
6
+ metadata.gz: bd502508cb5a172c6778bc20782dd799ea39f70fe0ada1291914c1ece812525cbe67c23c9de9687b76bba27fb3786cdab987720d798a1d478753527b5034e766
7
+ data.tar.gz: 0500a197f3405f89bd4501e642103b3692f57a4f40997c2d184897a5fad055b8fa90b34f79daea5e5eafeb1a5e4cd43e84d5d6b787a64dbd9de2bcbe6cd6088d
data/README.md CHANGED
@@ -184,6 +184,16 @@ Add it to your model or form object using
184
184
  validates :is_a_farmer, "defra_ruby/validators/true_false": true
185
185
  ```
186
186
 
187
+ ### Price
188
+
189
+ This validator checks the value is present and in the correct format, meaning it has no more than 2 decimal places. If blank or not in a valid format it will return an error.
190
+
191
+ Add it to your model or form object using
192
+
193
+ ```ruby
194
+ validates :price, "defra_ruby/validators/price": true
195
+ ```
196
+
187
197
  ## Adding custom error messages
188
198
 
189
199
  All the validators in this gem have built-in error messages. But if you want, you can provide your own custom message in the app, like this:
@@ -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.
@@ -0,0 +1,6 @@
1
+ en:
2
+ defra_ruby:
3
+ validators:
4
+ PriceValidator:
5
+ blank: "Enter a price"
6
+ invalid_format: "Enter a valid price - there’s a mistake in that one"
@@ -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
- company_type = options[:company_type]
20
- validate_with_companies_house(record, attribute, value, company_type)
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, company_type)
41
- case CompaniesHouseService.new(value, company_type).status
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(company_no, company_type = nil)
9
- @company_no = company_no
10
- @url = "#{DefraRuby::Validators.configuration.companies_house_host}#{@company_no}"
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 :not_found unless company_type_is_allowed?(json_response)
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 a company_type has not been defined in the validator,
43
- # we skip this check
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
- @company_type.to_s == json_response["type"]
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,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DefraRuby
4
+ module Validators
5
+ class PriceValidator < BaseValidator
6
+ include CanValidatePresence
7
+
8
+ def validate_each(record, attribute, value)
9
+ return false unless value_is_present?(record, attribute, value)
10
+
11
+ valid_format?(record, attribute, value)
12
+ end
13
+
14
+ private
15
+
16
+ def valid_format?(record, attribute, value)
17
+ return true if value.match?(/\A\d{1,10}(\.\d{1,2})?\z/)
18
+
19
+ add_validation_error(record, attribute, :invalid_format)
20
+ false
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module DefraRuby
4
4
  module Validators
5
- VERSION = "2.6.0"
5
+ VERSION = "2.7.1"
6
6
  end
7
7
  end
@@ -24,6 +24,7 @@ require_relative "validators/postcode_validator"
24
24
  require_relative "validators/token_validator"
25
25
  require_relative "validators/true_false_validator"
26
26
  require_relative "validators/past_date_validator"
27
+ require_relative "validators/price_validator"
27
28
 
28
29
  module DefraRuby
29
30
  module Validators
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.6.0
4
+ version: 2.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-21 00:00:00.000000000 Z
11
+ date: 2024-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -144,6 +144,7 @@ files:
144
144
  - config/locales/defra_ruby/validators/phone_number_validator/en.yml
145
145
  - config/locales/defra_ruby/validators/position_validator/en.yml
146
146
  - config/locales/defra_ruby/validators/postcode_validator/en.yml
147
+ - config/locales/defra_ruby/validators/price_validator/en.yml
147
148
  - config/locales/defra_ruby/validators/token_validator/en.yml
148
149
  - config/locales/defra_ruby/validators/true_false_validator/en.yml
149
150
  - lib/defra_ruby/validators.rb
@@ -165,6 +166,7 @@ files:
165
166
  - lib/defra_ruby/validators/phone_number_validator.rb
166
167
  - lib/defra_ruby/validators/position_validator.rb
167
168
  - lib/defra_ruby/validators/postcode_validator.rb
169
+ - lib/defra_ruby/validators/price_validator.rb
168
170
  - lib/defra_ruby/validators/token_validator.rb
169
171
  - lib/defra_ruby/validators/true_false_validator.rb
170
172
  - lib/defra_ruby/validators/version.rb
@@ -175,7 +177,7 @@ licenses:
175
177
  metadata:
176
178
  allowed_push_host: https://rubygems.org
177
179
  rubygems_mfa_required: 'true'
178
- post_install_message:
180
+ post_install_message:
179
181
  rdoc_options: []
180
182
  require_paths:
181
183
  - lib
@@ -191,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
193
  version: '0'
192
194
  requirements: []
193
195
  rubygems_version: 3.3.7
194
- signing_key:
196
+ signing_key:
195
197
  specification_version: 4
196
198
  summary: Defra ruby on rails validations
197
199
  test_files: []