Module | Sequel::Plugins::AutoValidations |
In: |
lib/sequel/plugins/auto_validations.rb
|
The auto_validations plugin automatically sets up the following types of validations for your model columns:
To determine the columns to use for the type/not_null/max_length 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(:not_null)
If you want to skip all auto validations (only useful if loading the plugin in a superclass):
Model.skip_auto_validations(:all)
By default, the plugin uses a not_null validation for NOT NULL columns, but that can be changed to a presence validation using an option:
Model.plugin :auto_validations, :not_null=>:presence
This is useful if you want to enforce that NOT NULL string columns do not allow empty values.
You can also supply hashes to pass options through to the underlying validators:
Model.plugin :auto_validations, unique_opts: {only_if_modified: true}
This works for unique_opts, max_length_opts, schema_types_opts, explicit_not_null_opts, and not_null_opts.
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
NOT_NULL_OPTIONS | = | {:from=>:values}.freeze |
EXPLICIT_NOT_NULL_OPTIONS | = | {:from=>:values, :allow_missing=>true}.freeze |
MAX_LENGTH_OPTIONS | = | {:from=>:values, :allow_nil=>true}.freeze |
SCHEMA_TYPES_OPTIONS | = | NOT_NULL_OPTIONS |
UNIQUE_OPTIONS | = | NOT_NULL_OPTIONS |
# File lib/sequel/plugins/auto_validations.rb, line 62 62: def self.apply(model, opts=OPTS) 63: model.instance_eval do 64: plugin :validation_helpers 65: @auto_validate_presence = false 66: @auto_validate_not_null_columns = [] 67: @auto_validate_explicit_not_null_columns = [] 68: @auto_validate_max_length_columns = [] 69: @auto_validate_unique_columns = [] 70: @auto_validate_types = true 71: 72: @auto_validate_options = { 73: :not_null=>NOT_NULL_OPTIONS, 74: :explicit_not_null=>EXPLICIT_NOT_NULL_OPTIONS, 75: :max_length=>MAX_LENGTH_OPTIONS, 76: :schema_types=>SCHEMA_TYPES_OPTIONS, 77: :unique=>UNIQUE_OPTIONS 78: }.freeze 79: end 80: end
Setup auto validations for the model if it has a dataset.
# File lib/sequel/plugins/auto_validations.rb, line 83 83: def self.configure(model, opts=OPTS) 84: model.instance_eval do 85: setup_auto_validations if @dataset 86: if opts[:not_null] == :presence 87: @auto_validate_presence = true 88: end 89: 90: h = @auto_validate_options.dup 91: [:not_null, :explicit_not_null, :max_length, :schema_types, :unique].each do |type| 92: if type_opts = opts["#{type}_opts""#{type}_opts"] 93: h[type] = h[type].merge(type_opts).freeze 94: end 95: end 96: @auto_validate_options = h.freeze 97: end 98: end