Class: Coradoc::Validation::Rules::Count
- Inherits:
-
Coradoc::Validation::Rule
- Object
- Coradoc::Validation::Rule
- Coradoc::Validation::Rules::Count
- Defined in:
- lib/coradoc/validation.rb
Overview
Count validation for arrays/collections
Instance Attribute Summary
Attributes inherited from Coradoc::Validation::Rule
Instance Method Summary collapse
Methods inherited from Coradoc::Validation::Rule
Constructor Details
This class inherits a constructor from Coradoc::Validation::Rule
Instance Method Details
#validate(element, _context = {}) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/coradoc/validation.rb', line 256 def validate(element, _context = {}) field = [:field] value = element.send(field) if element.respond_to?(field) return [] if value.nil? errors = [] count = value.respond_to?(:count) ? value.count : 0 errors << "#{field} must have at least #{[:min]} items" if [:min] && count < [:min] errors << "#{field} must have at most #{[:max]} items" if [:max] && count > [:max] errors end |