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
Source
# File lib/sequel/plugins/auto_validations.rb 177 def auto_validate_presence? 178 @auto_validate_presence 179 end
Whether to use a presence validation for not null columns
Source
# File lib/sequel/plugins/auto_validations.rb 182 def auto_validate_types? 183 @auto_validate_types 184 end
Whether to automatically validate schema types for all columns
Source
# File lib/sequel/plugins/auto_validations.rb 187 def freeze 188 @auto_validate_no_null_byte_columns.freeze 189 @auto_validate_not_null_columns.freeze 190 @auto_validate_explicit_not_null_columns.freeze 191 @auto_validate_max_length_columns.freeze 192 @auto_validate_max_value_columns.freeze 193 @auto_validate_min_value_columns.freeze 194 @auto_validate_unique_columns.freeze 195 196 super 197 end
Freeze auto_validation settings when freezing model class.
Source
# File lib/sequel/plugins/auto_validations.rb 205 def skip_auto_validations(type) 206 case type 207 when :all 208 [:not_null, :no_null_byte, :types, :unique, :max_length, :max_value, :min_value].each{|v| skip_auto_validations(v)} 209 when :not_null 210 auto_validate_not_null_columns.clear 211 auto_validate_explicit_not_null_columns.clear 212 when :types 213 @auto_validate_types = false 214 else 215 public_send("auto_validate_#{type}_columns").clear 216 end 217 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.
Private Instance Methods
Source
# File lib/sequel/plugins/auto_validations.rb 222 def setup_auto_validations 223 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}} 224 @auto_validate_not_null_columns = not_null_cols - Array(primary_key) 225 explicit_not_null_cols += Array(primary_key) 226 @auto_validate_explicit_not_null_columns = explicit_not_null_cols.uniq 227 @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]]} 228 @auto_validate_max_value_columns = db_schema.select{|col, sch| sch[:max_value]}.map{|col, sch| [col, sch[:max_value]]} 229 @auto_validate_min_value_columns = db_schema.select{|col, sch| sch[:min_value]}.map{|col, sch| [col, sch[:min_value]]} 230 @auto_validate_no_null_byte_columns = db_schema.select{|_, sch| sch[:type] == :string}.map{|col, _| col} 231 table = dataset.first_source_table 232 @auto_validate_unique_columns = if db.supports_index_parsing? && [Symbol, SQL::QualifiedIdentifier, SQL::Identifier, String].any?{|c| table.is_a?(c)} 233 db.indexes(table).select{|name, idx| idx[:unique] == true}.map{|name, idx| idx[:columns].length == 1 ? idx[:columns].first : idx[:columns]} 234 else 235 [] 236 end 237 end
Parse the database schema and indexes and record the columns to automatically validate.