Class: Volt::FormatValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/volt/models/validators/format_validator.rb

Overview

Validates the format of a field against any number of block or regex criteria

Direct Known Subclasses

EmailValidator, PhoneNumberValidator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, field_name) ⇒ FormatValidator

Returns a new instance of FormatValidator.

Parameters:

  • model (Volt::Model)

    the model being validated

  • field_name (String)

    the name of the field being validated



36
37
38
39
40
41
# File 'lib/volt/models/validators/format_validator.rb', line 36

def initialize(model, field_name)
  @name = field_name
  @value = model.get field_name

  @criteria = []
end

Class Method Details

.validate(model, field_name, options) ⇒ Hash

Creates a new instance with the provided options and returns it’s errors

Examples:

options = { with: /.+@.+/, message: 'must include an @ symobl' }

FormatValidator.validate(user, 'email', options)
numbers_only = /^\d+$/
sum_equals_ten = ->(s) { s.chars.map(&:to_i).reduce(:+) == 10 }

options = [
  { with: numbers_only, message: 'must include only numbers' },
  { with: sum_equals_ten, message: 'must add up to 10' }
]

FormatValidator.validate(user, 'email', options)

Parameters:

  • model (Volt::Model)

    the model being validated

  • field_name (String)

    the name of the field being validated

  • options (Hash, Array<Hash>)

    criteria and related error messages

Options Hash (options):

  • :with (Regexp, Proc)

    criterion for validation

  • :message (String)

    to display if criterion not met

    • will be appended to the field name

    • should start with something like:

      • “must include…”

      • “should end with…”

      • “is invalid because…”

Returns:

  • (Hash)

    hash of validation errors for the field

    • {} if there are no errors

    • { field_name: [messages] } if there are errors



30
31
32
# File 'lib/volt/models/validators/format_validator.rb', line 30

def self.validate(model, field_name, options)
  new(model, field_name).apply(options).errors
end

Instance Method Details

#apply(options) ⇒ self

Applies criteria to the validator in a variety of forms

Parameters:

  • options (Hash, Array<Hash>)

    criteria and related error messages

Options Hash (options):

  • :with (Regexp, Proc)

    criterion for validation

  • :message (String)

    to display if criterion not met

    • will be appended to the field name

    • should start with something like:

      • “must include…”

      • “should end with…”

      • “is invalid because…”

Returns:

  • (self)

    returns itself for chaining

See Also:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/volt/models/validators/format_validator.rb', line 57

def apply(options)
  return apply_list options if options.is_a? Array

  options = case options
            when true
              default_options
            when Hash
              if default_options.is_a? Hash
                default_options.merge options
              else
                options
              end
            end

  with options[:with], options[:message]
  self
end

#error_messagesArray<String>

Returns an array of validation error messages

Returns:



86
87
88
89
90
# File 'lib/volt/models/validators/format_validator.rb', line 86

def error_messages
  @criteria.reduce([]) do |e, c|
    test(c[:criterion]) ? e : e << c[:message]
  end
end

#errorsHash

Returns the first of the validation errors or an empty hash

Returns:

  • (Hash)

    hash of validation errors for the field

    • {} if there are no errors

    • { field_name: [messages] } if there are errors



80
81
82
# File 'lib/volt/models/validators/format_validator.rb', line 80

def errors
  valid? ? {} : { @name => error_messages }
end

#valid?Boolean

Returns true or false depending on if the model passes all its criteria

Returns:



94
95
96
# File 'lib/volt/models/validators/format_validator.rb', line 94

def valid?
  error_messages.empty?
end

#with(criterion, message) ⇒ self

Adds a criterion and error message

Parameters:

  • criterion (Regexp, Proc)

    criterion for validation

  • message (String)

    to display if criterion not met

    • will be appended to the field name

    • should start with something like:

      • “must include…”

      • “should end with…”

      • “is invalid because…”

Returns:

  • (self)

    returns itself for chaining



109
110
111
112
# File 'lib/volt/models/validators/format_validator.rb', line 109

def with(criterion, message)
  @criteria << { criterion: criterion, message: message }
  self
end