Class: Toolchain::Validations::Validators::Inclusion

Inherits:
Base
  • Object
show all
Defined in:
lib/toolchain/validations/validators/inclusion.rb

Overview

Validates the inclusion of values in an attribute. In the example, only ‘pending` and `dispatched` are allowed to be set.

Examples:

class Shipment::Creator
  validates :status, inclusion: {
    in: ["pending", "dispatched"],
    message: "available options are: pending, dispatched"
  }
end

Instance Attribute Summary

Attributes inherited from Base

#data, #errors, #key_path, #message, #object

Instance Method Summary collapse

Methods inherited from Base

#initialize, #value

Constructor Details

This class inherits a constructor from Toolchain::Validations::Validators::Base

Instance Method Details

#validateObject



17
18
19
20
21
# File 'lib/toolchain/validations/validators/inclusion.rb', line 17

def validate
  if not_included?
    errors.add(key_path, message || "is invalid")
  end
end