The auto_validations plugin automatically sets up three types of validations for your model columns:
type validations for all columns
presence validations on NOT NULL columns
unique validations on columns or sets of columns with unique indexes
To determine the columns to use for the presence validations and the types for the type validations, the plugin looks at the database schema for the model's table. To determine the unique validations, Sequel looks at the indexes on the table. In order for this plugin to be fully functional, the underlying database adapter needs to support both schema and index parsing.
This plugin uses the validation_helpers plugin underneath to implement the validations. It does not allow for any per-column validation message customization, but you can alter the messages for the given type of validation on a per-model basis (see the validation_helpers documentation).
You can skip certain types of validations from being automatically added via:
Model.skip_auto_validations(:presence)
If you want to skip all auto validations (only useful if loading the plugin in a superclass):
Model.skip_auto_validations(:all)
Usage:
# Make all model subclass use auto validations (called before loading subclasses) Sequel::Model.plugin :auto_validations # Make the Album class use auto validations Album.plugin :auto_validations
Load the validation_helpers plugin and setup data structures.
# File lib/sequel/plugins/auto_validations.rb, line 39 def self.apply(model) model.instance_eval do plugin :validation_helpers @auto_validate_presence_columns = [] @auto_validate_unique_columns = [] @auto_validate_types = true end end
Setup auto validations for the model if it has a dataset.
# File lib/sequel/plugins/auto_validations.rb, line 49 def self.configure(model) model.instance_eval do setup_auto_validations if @dataset end end