module Sequel::Plugins::PgAutoValidateEnums

The pg_auto_validate_enums plugin implements automatic validations for enum columns, ensuring that enum columns have a valid value. With this plugin, trying to save with an invalid enum value results in Sequel::ValidationFailed before saving, instead of Sequel::DatabaseError (wrapping PG::InvalidTextRepresentation or similar exception) during saving.

class Person < Sequel::Model
  # assume state enum column with allowed values active and inactive
  plugin :pg_auto_validate_enums
end
p = Person.new(state: "active").valid? # => true
p = Person.new(state: "inactive").valid? # => true
p = Person.new(state: "other").valid? # => false

While you can load this into individual model classes, typical use would be to load it into Sequel::Model or the appropriate model base class, and have all models that inherit from that class automatically pick it up.

This plugin depends on the validation_helpers plugin.