defra_ruby_validators 2.7.0 → 2.7.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cb145644c2a51c737b951282707e436d9b740ab756c04f93d761cf05cbc91a6
4
- data.tar.gz: 45ce24942e9e95030e8d051f06db0f20facff88f2320445b96b7c695a35b17f8
3
+ metadata.gz: fcc15d0837d498fd37fe5cfd1781d2b80b87165c0235b6e94aa50ad6463151a5
4
+ data.tar.gz: de144f02848b1b2c8de4ccb5bdb4ec9963697eed004ccf07106139333078936e
5
5
  SHA512:
6
- metadata.gz: 7c6c390badabbd364ed4174746857062d0f85d92405981c2b83e72498e493c6440e5663bc8ade16bc0d87534da467db4ea1a9dcb1871f9b1cc7f810dede007fb
7
- data.tar.gz: 31404af40d4a8da9da8c726bb9b081d6d9838eb0843ede3042b270d9dd7036c5810f00becde67f8fcdb0e723ea8f79883b970a8eec38a9eea581b31d9b93c376
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:
@@ -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"
@@ -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.7.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.7.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: 2024-10-25 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
@@ -137,7 +137,6 @@ 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
141
140
  - config/locales/defra_ruby/validators/grid_reference_validator/en.yml
142
141
  - config/locales/defra_ruby/validators/location_validator/en.yml
143
142
  - config/locales/defra_ruby/validators/mobile_phone_number_validator/en.yml
@@ -145,6 +144,7 @@ files:
145
144
  - config/locales/defra_ruby/validators/phone_number_validator/en.yml
146
145
  - config/locales/defra_ruby/validators/position_validator/en.yml
147
146
  - config/locales/defra_ruby/validators/postcode_validator/en.yml
147
+ - config/locales/defra_ruby/validators/price_validator/en.yml
148
148
  - config/locales/defra_ruby/validators/token_validator/en.yml
149
149
  - config/locales/defra_ruby/validators/true_false_validator/en.yml
150
150
  - lib/defra_ruby/validators.rb
@@ -159,7 +159,6 @@ files:
159
159
  - lib/defra_ruby/validators/configuration.rb
160
160
  - lib/defra_ruby/validators/email_validator.rb
161
161
  - lib/defra_ruby/validators/engine.rb
162
- - lib/defra_ruby/validators/future_date_validator.rb_park
163
162
  - lib/defra_ruby/validators/grid_reference_validator.rb
164
163
  - lib/defra_ruby/validators/location_validator.rb
165
164
  - lib/defra_ruby/validators/mobile_phone_number_validator.rb
@@ -167,6 +166,7 @@ files:
167
166
  - lib/defra_ruby/validators/phone_number_validator.rb
168
167
  - lib/defra_ruby/validators/position_validator.rb
169
168
  - lib/defra_ruby/validators/postcode_validator.rb
169
+ - lib/defra_ruby/validators/price_validator.rb
170
170
  - lib/defra_ruby/validators/token_validator.rb
171
171
  - lib/defra_ruby/validators/true_false_validator.rb
172
172
  - lib/defra_ruby/validators/version.rb
@@ -177,7 +177,7 @@ licenses:
177
177
  metadata:
178
178
  allowed_push_host: https://rubygems.org
179
179
  rubygems_mfa_required: 'true'
180
- post_install_message:
180
+ post_install_message:
181
181
  rdoc_options: []
182
182
  require_paths:
183
183
  - lib
@@ -193,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
193
  version: '0'
194
194
  requirements: []
195
195
  rubygems_version: 3.3.7
196
- signing_key:
196
+ signing_key:
197
197
  specification_version: 4
198
198
  summary: Defra ruby on rails validations
199
199
  test_files: []
@@ -1,5 +0,0 @@
1
- en:
2
- defra_ruby:
3
- validators:
4
- FutureDateValidator:
5
- future_date: "Date cannot be in the past"
@@ -1,21 +0,0 @@
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