defra_ruby_validators 2.5.1 → 2.5.2

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: 3a8cefa4eee1eb9fcc363935f64760eb88aafd35d29ac09cfd3663105aafa3eb
4
- data.tar.gz: 0dbc80e1d0c36fc4452257e6ade5377d83f04b860f9ec0375975526aca43f961
3
+ metadata.gz: 54f780cf89fcc1888cd3b9af95707c6e3ac2a0d0dcd1e304ab6d9b2933b69ef9
4
+ data.tar.gz: 895551a30493fb79367289728c28e7e09873c1c6def93e685c70f7076b67f46c
5
5
  SHA512:
6
- metadata.gz: 0417b991688a935460cbd8e96d28dd8d1496838d7aa5d56420f9a0e5ac701d652b278657a1a733c905e1f3b8ae6880e659b58071e90673af351224c9a3445ed2
7
- data.tar.gz: 88fb21c5eb3ca1cc365ce4b52aeb8171540ec1cc5776b32cab5122fccb978215f46ea92ff13ceedde13fedd4b700d8f757a74db50f08e4e0e45e049c6ab2dfde
6
+ metadata.gz: '07863b0bde9327c8384a93b8e1c681b02ef7fedce040a36d4f17aa9435a4783e4db51fcf41a29469ea3b6d9599324b89f8ab5c3c830040b563894db33449d8e9'
7
+ data.tar.gz: 4e07b525aae83437d5ad450e4e3989f402caf04346efc130543440a41084e2a4ecc48168894f88e261fc66c269aa48389282dedd17b2031ce0a889924b99720c
data/README.md CHANGED
@@ -144,6 +144,16 @@ Add it to your model or form object using
144
144
  validates :phone_number, "defra_ruby/validators/phone_number": true
145
145
  ```
146
146
 
147
+ ### Mobile phone number
148
+
149
+ This validator checks the value is present, is not too long (15 is th maximum length for any phone number), is a mobile phone number, and is in the correct format as per [E.164](https://en.wikipedia.org/wiki/E.164) (we use [phonelib](https://github.com/daddyz/phonelib) to check the format).
150
+
151
+ Add it to your model or form object using
152
+
153
+ ```ruby
154
+ validates :mobile_phone_number, "defra_ruby/validators/mobile_phone_number": true
155
+ ```
156
+
147
157
  ### Position
148
158
 
149
159
  This validator checks the value provided for 'position' i.e. someones role or title within an organisation. This is an optional field so the validator has to handle the value being blank. If it's not then it can be no longer than 70 characters and only include letters, spaces, commas, full stops, hyphens and apostrophes else it will return an error.
@@ -0,0 +1,7 @@
1
+ en:
2
+ defra_ruby:
3
+ validators:
4
+ MobilePhoneNumberValidator:
5
+ blank: "Enter a mobile phone number"
6
+ invalid_format: "Enter a valid mobile phone number"
7
+ too_long: "Check the number you entered - it should have no more than than 15 characters"
@@ -9,9 +9,8 @@ module DefraRuby
9
9
  # SC534714, CE000958
10
10
  # IP00141R, IP27702R, SP02252R
11
11
  # https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/426891/uniformResourceIdentifiersCustomerGuide.pdf
12
- VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX = Regexp.new(
12
+ VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX =
13
13
  /\A(\d{8,8}$)|([a-zA-Z]{2}\d{6}$)|([a-zA-Z]{2}\d{5}[a-zA-Z]{1}$)\z/i
14
- ).freeze
15
14
 
16
15
  def validate_each(record, attribute, value)
17
16
  return false unless value_is_present?(record, attribute, value)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "phonelib"
4
+
5
+ module DefraRuby
6
+ module Validators
7
+ class MobilePhoneNumberValidator < BaseValidator
8
+ include CanValidatePresence
9
+ include CanValidateLength
10
+
11
+ MAX_LENGTH = 15
12
+
13
+ def validate_each(record, attribute, value)
14
+ return false unless value_is_present?(record, attribute, value)
15
+ return false unless value_is_not_too_long?(record, attribute, value, MAX_LENGTH)
16
+
17
+ valid_format?(record, attribute, value)
18
+ end
19
+
20
+ private
21
+
22
+ def valid_format?(record, attribute, value)
23
+ Phonelib.default_country = "GB"
24
+ return true if Phonelib.valid?(value) && Phonelib.parse(value).type == :mobile
25
+
26
+ add_validation_error(record, attribute, :invalid_format)
27
+ false
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module DefraRuby
4
4
  module Validators
5
- VERSION = "2.5.1"
5
+ VERSION = "2.5.2"
6
6
  end
7
7
  end
@@ -18,6 +18,7 @@ require_relative "validators/email_validator"
18
18
  require_relative "validators/grid_reference_validator"
19
19
  require_relative "validators/location_validator"
20
20
  require_relative "validators/phone_number_validator"
21
+ require_relative "validators/mobile_phone_number_validator"
21
22
  require_relative "validators/position_validator"
22
23
  require_relative "validators/token_validator"
23
24
  require_relative "validators/true_false_validator"
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.5.1
4
+ version: 2.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-05 00:00:00.000000000 Z
11
+ date: 2023-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -125,6 +125,7 @@ files:
125
125
  - config/locales/defra_ruby/validators/email_validator/en.yml
126
126
  - config/locales/defra_ruby/validators/grid_reference_validator/en.yml
127
127
  - config/locales/defra_ruby/validators/location_validator/en.yml
128
+ - config/locales/defra_ruby/validators/mobile_phone_number_validator/en.yml
128
129
  - config/locales/defra_ruby/validators/past_date_validator/en.yml
129
130
  - config/locales/defra_ruby/validators/phone_number_validator/en.yml
130
131
  - config/locales/defra_ruby/validators/position_validator/en.yml
@@ -144,6 +145,7 @@ files:
144
145
  - lib/defra_ruby/validators/engine.rb
145
146
  - lib/defra_ruby/validators/grid_reference_validator.rb
146
147
  - lib/defra_ruby/validators/location_validator.rb
148
+ - lib/defra_ruby/validators/mobile_phone_number_validator.rb
147
149
  - lib/defra_ruby/validators/past_date_validator.rb
148
150
  - lib/defra_ruby/validators/phone_number_validator.rb
149
151
  - lib/defra_ruby/validators/position_validator.rb