Class: Volt::FormatValidator
- 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
Class Method Summary collapse
-
.validate(model, field_name, options) ⇒ Hash
Creates a new instance with the provided options and returns it’s errors.
Instance Method Summary collapse
-
#apply(options) ⇒ self
Applies criteria to the validator in a variety of forms.
-
#error_messages ⇒ Array<String>
Returns an array of validation error messages.
-
#errors ⇒ Hash
Returns the first of the validation errors or an empty hash.
-
#initialize(model, field_name) ⇒ FormatValidator
constructor
A new instance of FormatValidator.
-
#valid? ⇒ Boolean
Returns true or false depending on if the model passes all its criteria.
-
#with(criterion, message) ⇒ self
Adds a criterion and error message.
Constructor Details
#initialize(model, field_name) ⇒ FormatValidator
Returns a new instance of FormatValidator.
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
30 31 32 |
# File 'lib/volt/models/validators/format_validator.rb', line 30 def self.validate(model, field_name, ) new(model, field_name).apply().errors end |
Instance Method Details
#apply(options) ⇒ self
Applies criteria to the validator in a variety of forms
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() return apply_list if .is_a? Array = case when true when Hash if .is_a? Hash .merge else end end with [:with], [:message] self end |
#error_messages ⇒ Array<String>
Returns an array of validation error messages
86 87 88 89 90 |
# File 'lib/volt/models/validators/format_validator.rb', line 86 def @criteria.reduce([]) do |e, c| test(c[:criterion]) ? e : e << c[:message] end end |
#errors ⇒ Hash
Returns the first of the validation errors or an empty hash
80 81 82 |
# File 'lib/volt/models/validators/format_validator.rb', line 80 def errors valid? ? {} : { @name => } end |
#valid? ⇒ Boolean
Returns true or false depending on if the model passes all its criteria
94 95 96 |
# File 'lib/volt/models/validators/format_validator.rb', line 94 def valid? .empty? end |
#with(criterion, message) ⇒ self
Adds a criterion and error message
109 110 111 112 |
# File 'lib/volt/models/validators/format_validator.rb', line 109 def with(criterion, ) @criteria << { criterion: criterion, message: } self end |