Module: Sequel::Plugins::AutoValidations
- Defined in:
- lib/sequel/plugins/auto_validations.rb
Overview
The auto_validations plugin automatically sets up the following types of validations for your model columns:
-
type validations for all columns
-
not_null validations on NOT NULL columns (optionally, presence validations)
-
unique validations on columns or sets of columns with unique indexes
-
max length validations on string 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. Additionally, unique validations are only added for models that select from a simple table, they are not added for models that select from a subquery or joined dataset.
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)
It is possible to skip auto validations on a per-model-instance basis via:
instance.skip_auto_validations(:unique, :not_null) do
puts instance.valid?
end
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.
If you only want auto_validations to add validations to columns that do not already have an error associated with them, you can use the skip_invalid option:
Model.plugin :auto_validations, skip_invalid: true
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
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Constant Summary collapse
- 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
- NO_NULL_BYTE_OPTIONS =
MAX_LENGTH_OPTIONS
- EMPTY_ARRAY =
[].freeze
Class Method Summary collapse
- .apply(model, opts = OPTS) ⇒ Object
-
.configure(model, opts = OPTS) ⇒ Object
Setup auto validations for the model if it has a dataset.
Class Method Details
.apply(model, opts = OPTS) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sequel/plugins/auto_validations.rb', line 77 def self.apply(model, opts=OPTS) model.instance_exec do plugin :validation_helpers @auto_validate_presence = false @auto_validate_no_null_byte_columns = [] @auto_validate_not_null_columns = [] @auto_validate_explicit_not_null_columns = [] @auto_validate_max_length_columns = [] @auto_validate_unique_columns = [] @auto_validate_types = true = { :no_null_byte=>NO_NULL_BYTE_OPTIONS, :not_null=>NOT_NULL_OPTIONS, :explicit_not_null=>EXPLICIT_NOT_NULL_OPTIONS, :max_length=>MAX_LENGTH_OPTIONS, :schema_types=>SCHEMA_TYPES_OPTIONS, :unique=>UNIQUE_OPTIONS }.freeze end end |
.configure(model, opts = OPTS) ⇒ Object
Setup auto validations for the model if it has a dataset.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sequel/plugins/auto_validations.rb', line 100 def self.configure(model, opts=OPTS) model.instance_exec do setup_auto_validations if @dataset if opts[:not_null] == :presence @auto_validate_presence = true end h = .dup [:not_null, :explicit_not_null, :max_length, :no_null_byte, :schema_types, :unique].each do |type| if type_opts = opts[:"#{type}_opts"] h[type] = h[type].merge(type_opts).freeze end end if opts[:skip_invalid] [:not_null, :explicit_not_null, :no_null_byte, :max_length, :schema_types].each do |type| h[type] = h[type].merge(:skip_invalid=>true).freeze end end = h.freeze end end |