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 +4 -4
- data/README.md +10 -0
- data/config/locales/defra_ruby/validators/price_validator/en.yml +6 -0
- data/lib/defra_ruby/validators/price_validator.rb +25 -0
- data/lib/defra_ruby/validators/version.rb +1 -1
- data/lib/defra_ruby/validators.rb +1 -0
- metadata +4 -4
- data/config/locales/defra_ruby/validators/future_date_validator/en.yml +0 -5
- data/lib/defra_ruby/validators/future_date_validator.rb_park +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6206b70eb2b95385d8ee3eaae2600048973784b8d0aec0d6ed8ad6b7160a09c1
|
4
|
+
data.tar.gz: 9882d021eef58fe6912b86df0a464e71efd2b3a366a16fe8a7b327a54aba3e72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,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
|
@@ -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.
|
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-
|
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,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
|