defra_ruby_validators 2.7.0 → 2.7.2

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: 6206b70eb2b95385d8ee3eaae2600048973784b8d0aec0d6ed8ad6b7160a09c1
4
+ data.tar.gz: 9882d021eef58fe6912b86df0a464e71efd2b3a366a16fe8a7b327a54aba3e72
5
5
  SHA512:
6
- metadata.gz: 7c6c390badabbd364ed4174746857062d0f85d92405981c2b83e72498e493c6440e5663bc8ade16bc0d87534da467db4ea1a9dcb1871f9b1cc7f810dede007fb
7
- data.tar.gz: 31404af40d4a8da9da8c726bb9b081d6d9838eb0843ede3042b270d9dd7036c5810f00becde67f8fcdb0e723ea8f79883b970a8eec38a9eea581b31d9b93c376
6
+ metadata.gz: e8030181c4ca547e313e626b90eea3955935d186ee9703011e319de10fe4bd4bec133ae75a84e9509ae04199448c72086e1e64de28078e006fe4d174b53eb863
7
+ data.tar.gz: 0f77ecfb97c54d08817461737c07b5d926995e2c37c5d968495e8b52e83b3b7ed482b2725645a5aca2316ddada502972c310256b475a023993963925ef7fea24
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.to_s.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.2"
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-25 00:00:00.000000000 Z
11
+ date: 2024-11-14 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
@@ -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