module Sequel::Plugins::AutoValidations::ClassMethods

Attributes

auto_validate_explicit_not_null_columns[R]

The columns with automatic not_null validations for columns present in the values.

auto_validate_max_length_columns[R]

The columns or sets of columns with automatic max_length validations, as an array of pairs, with the first entry being the column name and second entry being the maximum length.

auto_validate_max_value_columns[R]

The columns with automatch max value validations, as an array of pairs, with the first entry being the column name and second entry being the maximum value.

auto_validate_min_value_columns[R]

The columns with automatch min value validations, as an array of pairs, with the first entry being the column name and second entry being the minimum value.

auto_validate_no_null_byte_columns[R]

The columns with automatic no_null_byte validations

auto_validate_not_null_columns[R]

The columns with automatic not_null validations

auto_validate_options[R]

Inherited options

auto_validate_unique_columns[R]

The columns or sets of columns with automatic unique validations

Public Instance Methods

auto_validate_presence?() click to toggle source

Whether to use a presence validation for not null columns

    # File lib/sequel/plugins/auto_validations.rb
175 def auto_validate_presence?
176   @auto_validate_presence
177 end
auto_validate_types?() click to toggle source

Whether to automatically validate schema types for all columns

    # File lib/sequel/plugins/auto_validations.rb
180 def auto_validate_types?
181   @auto_validate_types
182 end
freeze() click to toggle source

Freeze auto_validation settings when freezing model class.

Calls superclass method
    # File lib/sequel/plugins/auto_validations.rb
185 def freeze
186   @auto_validate_no_null_byte_columns.freeze
187   @auto_validate_not_null_columns.freeze
188   @auto_validate_explicit_not_null_columns.freeze
189   @auto_validate_max_length_columns.freeze
190   @auto_validate_max_value_columns.freeze
191   @auto_validate_min_value_columns.freeze
192   @auto_validate_unique_columns.freeze
193 
194   super
195 end
skip_auto_validations(type) click to toggle source

Skip automatic validations for the given validation type (:not_null, :no_null_byte, :types, :unique, :max_length, :max_value, :min_value). If :all is given as the type, skip all auto validations.

Skipping types validation automatically skips max_value and min_value validations, since those validations require valid types.

    # File lib/sequel/plugins/auto_validations.rb
203 def skip_auto_validations(type)
204   case type
205   when :all
206     [:not_null, :no_null_byte, :types, :unique, :max_length, :max_value, :min_value].each{|v| skip_auto_validations(v)}
207   when :not_null
208     auto_validate_not_null_columns.clear
209     auto_validate_explicit_not_null_columns.clear
210   when :types
211     @auto_validate_types = false
212   else
213     public_send("auto_validate_#{type}_columns").clear
214   end
215 end

Private Instance Methods

setup_auto_validations() click to toggle source

Parse the database schema and indexes and record the columns to automatically validate.

    # File lib/sequel/plugins/auto_validations.rb
220 def setup_auto_validations
221   not_null_cols, explicit_not_null_cols = db_schema.select{|col, sch| sch[:allow_null] == false}.partition{|col, sch| sch[:default].nil?}.map{|cs| cs.map{|col, sch| col}}
222   @auto_validate_not_null_columns = not_null_cols - Array(primary_key)
223   explicit_not_null_cols += Array(primary_key)
224   @auto_validate_explicit_not_null_columns = explicit_not_null_cols.uniq
225   @auto_validate_max_length_columns = db_schema.select{|col, sch| sch[:type] == :string && sch[:max_length].is_a?(Integer)}.map{|col, sch| [col, sch[:max_length]]}
226   @auto_validate_max_value_columns = db_schema.select{|col, sch| sch[:max_value]}.map{|col, sch| [col, sch[:max_value]]}
227   @auto_validate_min_value_columns = db_schema.select{|col, sch| sch[:min_value]}.map{|col, sch| [col, sch[:min_value]]}
228   @auto_validate_no_null_byte_columns = db_schema.select{|_, sch| sch[:type] == :string}.map{|col, _| col}
229   table = dataset.first_source_table
230   @auto_validate_unique_columns = if db.supports_index_parsing? && [Symbol, SQL::QualifiedIdentifier, SQL::Identifier, String].any?{|c| table.is_a?(c)}
231     db.indexes(table).select{|name, idx| idx[:unique] == true}.map{|name, idx| idx[:columns].length == 1 ? idx[:columns].first : idx[:columns]}
232   else
233     []
234   end
235 end