Module: Callcredit::Validations
- Defined in:
- lib/callcredit/validations.rb
Constant Summary collapse
- VALIDATIONS =
{ date_of_birth: ->(value) { clean_date_of_birth(value) }, title: ->(value) { value || "Unknown" }, first_name: ->(value) { clean_name(value, :first_name) }, last_name: ->(value) { clean_name(value, :last_name) }, middle_names: ->(value) { clean_name(value, :middle_names) }, postcode: ->(value) { clean_postcode(value, :postcode) }, previous_postcode: ->(value) { clean_postcode(value, :previous_postcode) }, delivery_postcode: ->(value) { clean_postcode(value, :delivery_postcode) } }
Class Method Summary collapse
- .clean_date_of_birth(date_of_birth) ⇒ Object
- .clean_name(name, param) ⇒ Object
- .clean_param(key, value) ⇒ Object
- .clean_postcode(postcode, param) ⇒ Object
- .input_error(param, value = nil) ⇒ Object
Class Method Details
.clean_date_of_birth(date_of_birth) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/callcredit/validations.rb', line 20 def self.clean_date_of_birth(date_of_birth) return unless date_of_birth date_of_birth = Date.parse(date_of_birth) if date_of_birth.is_a? String date_of_birth.strftime("%d/%m/%Y") rescue input_error(:date_of_birth, date_of_birth) end |
.clean_name(name, param) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/callcredit/validations.rb', line 28 def self.clean_name(name, param) return unless name # Convert name to ASCII characters only name = UnicodeUtils.nfkd(name).gsub(/(\p{Letter})\p{Mark}+/,'\\1') input_error(param, name) unless name =~ /\A[a-z A-Z'-]{1,30}\z/ name end |
.clean_param(key, value) ⇒ Object
15 16 17 18 |
# File 'lib/callcredit/validations.rb', line 15 def self.clean_param(key, value) validator = VALIDATIONS.fetch(key, ->(value){ value }) validator.call(value) end |
.clean_postcode(postcode, param) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/callcredit/validations.rb', line 36 def self.clean_postcode(postcode, param) return unless postcode postcode = postcode.upcase.strip input_error(param, postcode) unless postcode =~ /\A[A-Z 0-9]{5,8}\z/ postcode end |
.input_error(param, value = nil) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/callcredit/validations.rb', line 43 def self.input_error(param, value=nil) msg = if value.nil? "#{param} is a required param" else %{Invalid value "#{value}" for #{param}} end raise InvalidRequestError.new(msg, param) end |