Module: Modelish::Validations::ClassMethods
- Defined in:
- lib/modelish/validations.rb
Instance Method Summary collapse
-
#add_validator(property_name, &validator) ⇒ Object
Sets up a block containing validation logic for a given property.
-
#validate_length(name, value, max_length) ⇒ Object
Validates the length of a value, returning an error when validation fails.
-
#validate_length!(name, value, max_length) ⇒ Object
Validates the length of a value, raising an error to indicate validation failure.
-
#validate_length?(name, value, max_length) ⇒ true, false
Validates the length of a value, returning a boolean to indicate validation success.
-
#validate_required(args) ⇒ Array<ArgumentError>
Validates the required values, returning a list of errors when validation fails.
-
#validate_required!(args) ⇒ Object
Validates the required values, raising an error when validation fails.
-
#validate_required?(args) ⇒ true, false
Validates the required values, returning a boolean indicating validation success.
-
#validate_type(name, value, type) ⇒ ArgumentError
Validates the type of the value, returning an error when the value cannot be converted to the appropriate type.
-
#validate_type!(name, value, type) ⇒ Object
Validates the type of the value, raising an error when the value is not of the correct type.
-
#validate_type?(name, value, type) ⇒ true, false
Validates the type of the value, returning a boolean indicating validation outcome.
-
#validators ⇒ Object
A map of registered validator blocks, keyed on property_name.
Instance Method Details
#add_validator(property_name, &validator) ⇒ Object
Sets up a block containing validation logic for a given property. Each property may have 0 or more validators.
67 68 69 70 71 72 73 74 |
# File 'lib/modelish/validations.rb', line 67 def add_validator(property_name, &validator) validators[property_name.to_sym] ||= [] validators[property_name.to_sym] << validator class_eval do attr_accessor property_name.to_sym unless method_defined?(property_name.to_sym) end end |
#validate_length(name, value, max_length) ⇒ Object
Validates the length of a value, returning an error when validation fails.
136 137 138 139 140 |
# File 'lib/modelish/validations.rb', line 136 def validate_length(name, value, max_length) if max_length.to_i > 0 && value.to_s.length > max_length.to_i ArgumentError.new("#{name} must be less than #{max_length} characters") end end |
#validate_length!(name, value, max_length) ⇒ Object
Validates the length of a value, raising an error to indicate validation failure.
116 117 118 119 |
# File 'lib/modelish/validations.rb', line 116 def validate_length!(name, value, max_length) error = validate_length(name, value, max_length) raise error if error end |
#validate_length?(name, value, max_length) ⇒ true, false
Validates the length of a value, returning a boolean to indicate validation success.
126 127 128 |
# File 'lib/modelish/validations.rb', line 126 def validate_length?(name, value, max_length) validate_length(name, value, max_length).nil? end |
#validate_required(args) ⇒ Array<ArgumentError>
Validates the required values, returning a list of errors when validation fails.
86 87 88 89 90 91 92 |
# File 'lib/modelish/validations.rb', line 86 def validate_required(args) errors = [] args.each do |name, value| errors << ArgumentError.new("#{name} must not be nil or blank") if value.nil? || value.to_s.strip.empty? end errors end |
#validate_required!(args) ⇒ Object
Validates the required values, raising an error when validation fails.
99 100 101 102 |
# File 'lib/modelish/validations.rb', line 99 def validate_required!(args) errors = validate_required(args) raise errors.first unless errors.empty? end |
#validate_required?(args) ⇒ true, false
Validates the required values, returning a boolean indicating validation success.
108 109 110 |
# File 'lib/modelish/validations.rb', line 108 def validate_required?(args) validate_required(args).empty? end |
#validate_type(name, value, type) ⇒ ArgumentError
Validates the type of the value, returning an error when the value cannot be converted to the appropriate type.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/modelish/validations.rb', line 176 def validate_type(name, value, type) error = nil begin if value && type # Can't use a case statement because of the way === is implemented on some classes if type == Integer is_valid = (value.is_a?(Integer) || value.to_s =~ /^\-?\d+$/) elsif type == Float is_valid = (value.is_a?(Float) || value.to_s =~ /^\-?\d+\.?\d*$/) elsif [Date, DateTime].include?(type) is_valid = value.is_a?(type) || (type.parse(value.to_s) rescue false) elsif type == Symbol is_valid = value.respond_to?(:to_sym) else is_valid = value.is_a?(type) end unless is_valid error = ArgumentError.new("#{name} must be of type #{type}, but got #{value.inspect}") end end rescue StandardError => e error = ArgumentError.new("An error occurred validating #{name} with value #{value.inspect}: #{e.}") end error end |
#validate_type!(name, value, type) ⇒ Object
Validates the type of the value, raising an error when the value is not of the correct type.
158 159 160 161 |
# File 'lib/modelish/validations.rb', line 158 def validate_type!(name, value, type) error = validate_type(name, value, type) raise error if error end |
#validate_type?(name, value, type) ⇒ true, false
Validates the type of the value, returning a boolean indicating validation outcome.
148 149 150 |
# File 'lib/modelish/validations.rb', line 148 def validate_type?(name, value, type) validate_type(name, value, type).nil? end |
#validators ⇒ Object
A map of registered validator blocks, keyed on property_name.
77 78 79 |
# File 'lib/modelish/validations.rb', line 77 def validators @validators ||= {} end |