class Evnt::Validator
Validator
is a class used to validates params and attributes automatically.
Constants
- TYPES
Attributes
value[R]
Public Class Methods
new(value, options)
click to toggle source
The constructor is used to initialize a new validator.
Attributes¶ ↑
-
value
- The value that should be validated. -
options
- The list of options for the validation.
Options¶ ↑
-
presence
- Boolean value used to not accept nil values. -
type
- Symbol or String value used to set the type of the value. -
custom_options
- Other options that can change for every type.
# File lib/evnt/validator.rb, line 29 def initialize(value, options) @value = value @_options = options @_result = true _validates_presence if @_result _validates_type if @_result _validates_custom if @_result end
Public Instance Methods
passed?()
click to toggle source
This function tells if the validation is passed or not.
# File lib/evnt/validator.rb, line 43 def passed? @_result end
Private Instance Methods
_validates_custom()
click to toggle source
# File lib/evnt/validator.rb, line 66 def _validates_custom _validates_global_equal if @_result _validates_global_in if @_result _validates_global_out if @_result case @_options[:type] when :string _validates_string_blank if @_result _validates_string_length if @_result _validates_string_regex if @_result _validates_string_min_length if @_result _validates_string_max_length if @_result when :integer _validates_number_min if @_result _validates_number_max if @_result when :float _validates_number_min if @_result _validates_number_max if @_result when :time _validates_time_min if @_result _validates_time_max if @_result when :date _validates_time_min if @_result _validates_time_max if @_result when :datetime _validates_time_min if @_result _validates_time_max if @_result end end
_validates_global_equal()
click to toggle source
Global validations:
# File lib/evnt/validator.rb, line 169 def _validates_global_equal return if @value.nil? || @_options[:equal].nil? @_result = @_options[:equal] == @value end
_validates_global_in()
click to toggle source
# File lib/evnt/validator.rb, line 174 def _validates_global_in return if @value.nil? || @_options[:in].nil? @_result = @_options[:in].include?(@value) end
_validates_global_out()
click to toggle source
# File lib/evnt/validator.rb, line 179 def _validates_global_out return if @value.nil? || @_options[:out].nil? @_result = !@_options[:out].include?(@value) end
_validates_number_max()
click to toggle source
# File lib/evnt/validator.rb, line 220 def _validates_number_max return if @value.nil? || @_options[:max].nil? @_result = @value <= @_options[:max] end
_validates_number_min()
click to toggle source
Number validations:
# File lib/evnt/validator.rb, line 215 def _validates_number_min return if @value.nil? || @_options[:min].nil? @_result = @value >= @_options[:min] end
_validates_presence()
click to toggle source
# File lib/evnt/validator.rb, line 49 def _validates_presence return if @_options[:presence].nil? @_result = @_options[:presence] ? !@value.nil? : @value.nil? end
_validates_string_blank()
click to toggle source
String validations:
# File lib/evnt/validator.rb, line 187 def _validates_string_blank return if @value.nil? || @_options[:blank].nil? @_result = @_options[:blank] ? @value.empty? : !@value.empty? end
_validates_string_length()
click to toggle source
# File lib/evnt/validator.rb, line 192 def _validates_string_length return if @value.nil? || @_options[:length].nil? @_result = @value.length == @_options[:length] end
_validates_string_max_length()
click to toggle source
# File lib/evnt/validator.rb, line 202 def _validates_string_max_length return if @value.nil? || @_options[:max_length].nil? @_result = @value.length <= @_options[:max_length] end
_validates_string_min_length()
click to toggle source
# File lib/evnt/validator.rb, line 197 def _validates_string_min_length return if @value.nil? || @_options[:min_length].nil? @_result = @value.length >= @_options[:min_length] end
_validates_string_regex()
click to toggle source
# File lib/evnt/validator.rb, line 207 def _validates_string_regex return if @value.nil? || @_options[:regex].nil? @_result = @value.match?(@_options[:regex]) end
_validates_time_max()
click to toggle source
# File lib/evnt/validator.rb, line 233 def _validates_time_max return if @value.nil? || @_options[:max].nil? @_result = @value <= @_options[:max] end
_validates_time_min()
click to toggle source
Time validations:
# File lib/evnt/validator.rb, line 228 def _validates_time_min return if @value.nil? || @_options[:min].nil? @_result = @value >= @_options[:min] end
_validates_type()
click to toggle source
# File lib/evnt/validator.rb, line 54 def _validates_type return if @value.nil? || @_options[:type].nil? if @_options[:type].instance_of?(Symbol) && TYPES.include?(@_options[:type]) send("_validates_type_#{@_options[:type]}") elsif @_options[:type].instance_of?(String) _validates_type_custom else raise 'Validator type option not accepted' end end
_validates_type_array()
click to toggle source
# File lib/evnt/validator.rb, line 111 def _validates_type_array @_result = @value.instance_of?(Array) end
_validates_type_boolean()
click to toggle source
# File lib/evnt/validator.rb, line 115 def _validates_type_boolean return if @value.instance_of?(TrueClass) || @value.instance_of?(FalseClass) @value = true && return if [1, 'true'].include?(@value) @value = false && return if [0, 'false'].include?(@value) @_result = false end
_validates_type_custom()
click to toggle source
Types validations:
# File lib/evnt/validator.rb, line 99 def _validates_type_custom @_result = @value.instance_of?(Object.const_get(@_options[:type])) end
_validates_type_date()
click to toggle source
# File lib/evnt/validator.rb, line 145 def _validates_type_date return if @value.instance_of?(Date) @value = Date.parse(@value) rescue StandardError @_result = false end
_validates_type_datetime()
click to toggle source
# File lib/evnt/validator.rb, line 152 def _validates_type_datetime return if @value.instance_of?(DateTime) @value = DateTime.parse(@value) rescue StandardError @_result = false end
_validates_type_float()
click to toggle source
# File lib/evnt/validator.rb, line 131 def _validates_type_float return if @value.instance_of?(Float) @value = @value.to_f rescue StandardError @_result = false end
_validates_type_hash()
click to toggle source
# File lib/evnt/validator.rb, line 107 def _validates_type_hash @_result = @value.instance_of?(Hash) || @value.instance_of?(ActiveSupport::HashWithIndifferentAccess) end
_validates_type_integer()
click to toggle source
# File lib/evnt/validator.rb, line 124 def _validates_type_integer return if @value.instance_of?(Integer) @value = @value.to_i rescue StandardError @_result = false end
_validates_type_string()
click to toggle source
# File lib/evnt/validator.rb, line 138 def _validates_type_string return if @value.instance_of?(String) @value = @value.to_s rescue StandardError @_result = false end
_validates_type_symbol()
click to toggle source
# File lib/evnt/validator.rb, line 103 def _validates_type_symbol @_result = @value.instance_of?(Symbol) end
_validates_type_time()
click to toggle source
# File lib/evnt/validator.rb, line 159 def _validates_type_time return if @value.instance_of?(Time) @value = Time.parse(@value) rescue StandardError @_result = false end