Module: Sequel::Plugins::ValidationContexts

Defined in:


The validation_contexts plugin adds support for a validation_context method inside a validate method. You pass in the validation context to use via the :validation_context option to Sequel::Model#save && Sequel::Model#valid?:

class Album < Sequel::Model
  plugin :validation_contexts
  def validate
    errors.add(:status_id, 'not 1') if status_id != 1 && validation_context == :initial
    errors.add(:status_id, 'not 2') if status_id != 2 && validation_context == :approve
end 1).valid?(validation_context: :initial) # => true 2).valid?(validation_context: :initial) # => false 1).valid?(validation_context: :approve) # => false 2).valid?(validation_context: :approve) # => true

There is no validation context used by default, so validation_context will be nil if one is not specified. If you want to differentiate between creating new objects and updating existing objects, just use new?.

Once this plugin is loaded into a model, after you freeze an instance of that model, you can no longer specify a validation context when validating the instance.

Defined Under Namespace

Modules: InstanceMethods