module Sequel::Plugins::AutoValidations::ClassMethods
Attributes
The columns with automatic not_null validations for columns present in the values.
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.
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.
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.
The columns with automatic no_null_byte validations
The columns with automatic not_null validations
Inherited options
The columns or sets of columns with automatic unique validations
Public Instance Methods
Whether to use a presence validation for not null columns
# File lib/sequel/plugins/auto_validations.rb, line 174 def auto_validate_presence? @auto_validate_presence end
Whether to automatically validate schema types for all columns
# File lib/sequel/plugins/auto_validations.rb, line 179 def auto_validate_types? @auto_validate_types end
Freeze auto_validation settings when freezing model class.
# File lib/sequel/plugins/auto_validations.rb, line 184 def freeze @auto_validate_no_null_byte_columns.freeze @auto_validate_not_null_columns.freeze @auto_validate_explicit_not_null_columns.freeze @auto_validate_max_length_columns.freeze @auto_validate_max_value_columns.freeze @auto_validate_min_value_columns.freeze @auto_validate_unique_columns.freeze super end
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, line 202 def skip_auto_validations(type) case type when :all [:not_null, :no_null_byte, :types, :unique, :max_length, :max_value, :min_value].each{|v| skip_auto_validations(v)} when :not_null auto_validate_not_null_columns.clear auto_validate_explicit_not_null_columns.clear when :types @auto_validate_types = false else public_send("auto_validate_#{type}_columns").clear end end
Private Instance Methods
Parse the database schema and indexes and record the columns to automatically validate.
# File lib/sequel/plugins/auto_validations.rb, line 219 def setup_auto_validations 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}} @auto_validate_not_null_columns = not_null_cols - Array(primary_key) explicit_not_null_cols += Array(primary_key) @auto_validate_explicit_not_null_columns = explicit_not_null_cols.uniq @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]]} @auto_validate_max_value_columns = db_schema.select{|col, sch| sch[:max_value]}.map{|col, sch| [col, sch[:max_value]]} @auto_validate_min_value_columns = db_schema.select{|col, sch| sch[:min_value]}.map{|col, sch| [col, sch[:min_value]]} @auto_validate_no_null_byte_columns = db_schema.select{|_, sch| sch[:type] == :string}.map{|col, _| col} table = dataset.first_source_table @auto_validate_unique_columns = if db.supports_index_parsing? && [Symbol, SQL::QualifiedIdentifier, SQL::Identifier, String].any?{|c| table.is_a?(c)} db.indexes(table).select{|name, idx| idx[:unique] == true}.map{|name, idx| idx[:columns].length == 1 ? idx[:columns].first : idx[:columns]} else [] end end