Class: EnumValidator

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

Overview

Custom enum validator - ActiveModel’s inclusion validator doesn’t quite work the way we want for status enums, so rolling our own

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



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

def validate_each(record, attribute, value)
  return if value.nil? && options[:allow_nil]

  allowed_values = options[:in] || options[:within]
  return if allowed_values.include?(value)

  record.errors.add(
    attribute,
    "must be one of: #{allowed_values.join(", ")}, but got '#{value}'"
  )
end