Module: Modelish::Validations::ClassMethods
- Defined in:
- lib/modelish/validations.rb
Overview
Class methods for managing validated properties
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.
73 74 75 76 77 78 79 80 81 |
# File 'lib/modelish/validations.rb', line 73 def add_validator(property_name, &validator) property = property_name.to_sym validators[property] ||= [] validators[property] << validator class_eval do attr_accessor property unless method_defined?(property_name) end end |
#validate_length(name, value, max_length) ⇒ Object
Validates the length of a value, returning an error when validation fails.
148 149 150 151 152 153 |
# File 'lib/modelish/validations.rb', line 148 def validate_length(name, value, max_length) if max_length.to_i > 0 && value.to_s.length > max_length.to_i = "#{name} must be less than #{max_length} characters" ArgumentError.new() end end |
#validate_length!(name, value, max_length) ⇒ Object
Validates the length of a value, raising an error to indicate validation failure.
126 127 128 129 |
# File 'lib/modelish/validations.rb', line 126 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.
137 138 139 |
# File 'lib/modelish/validations.rb', line 137 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.
93 94 95 96 97 98 |
# File 'lib/modelish/validations.rb', line 93 def validate_required(args) blanks = args.select { |_k, v| v.nil? || v.to_s.strip.empty? } blanks.keys.map do |name| ArgumentError.new("#{name} must not be nil or blank") end end |
#validate_required!(args) ⇒ Object
Validates the required values, raising an error when validation fails.
106 107 108 109 |
# File 'lib/modelish/validations.rb', line 106 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.
117 118 119 |
# File 'lib/modelish/validations.rb', line 117 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.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/modelish/validations.rb', line 191 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 = "#{name} must be of type #{type}, " \ "but got #{value.inspect}" error = ArgumentError.new() end end rescue StandardError => e = "An error occurred validating #{name} with " \ "value #{value.inspect}: #{e.}" error = ArgumentError.new() 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.
172 173 174 175 |
# File 'lib/modelish/validations.rb', line 172 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.
162 163 164 |
# File 'lib/modelish/validations.rb', line 162 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.
84 85 86 |
# File 'lib/modelish/validations.rb', line 84 def validators @validators ||= {} end |