Class: ArrayPresenceValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/petstore_api_client/validators/array_presence_validator.rb

Overview

Custom validator for required array fields ActiveModel’s presence validator doesn’t handle empty arrays correctly

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/petstore_api_client/validators/array_presence_validator.rb', line 6

def validate_each(record, attribute, value)
  if value.nil?
    record.errors.add(attribute, "must be present")
  elsif !value.is_a?(Array)
    record.errors.add(attribute, "must be an array")
  elsif value.empty?
    record.errors.add(attribute, "cannot be empty")
  end
end