Module: Respect::HasConstraints
- Included in:
- BooleanSchema, NumericSchema, StringSchema
- Defined in:
- lib/respect/has_constraints.rb
Overview
Module supporting execution of validators referred in options.
Classes including this module must fulfill the following requirements:
-
Respond to options and returned a hash of options where keys refers to validator name (i.e. greater_than for GreaterThanValidator).
-
Respond to validate_type(object) which must returns the sanitized object.
Instance Method Summary (collapse)
-
- (Object) validate(object)
Call validate_type with the given object, apply the constraints and assign the sanitized object.
-
- (Object) validate_constraints(value)
Validate all the constraints listed in options to the given value.
Instance Method Details
- (Object) validate(object)
Call validate_type with the given object, apply the constraints and assign the sanitized object.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/respect/has_constraints.rb', line 22 def validate(object) sanitized_object = validate_type(object) validate_constraints(sanitized_object) unless sanitized_object.nil? && allow_nil? self.sanitized_object = sanitized_object true rescue ValidationError => e # Reset sanitized object. self.sanitized_object = nil raise e end |
- (Object) validate_constraints(value)
Validate all the constraints listed in options to the given value.
12 13 14 15 16 17 18 |
# File 'lib/respect/has_constraints.rb', line 12 def validate_constraints(value) .each do |option, arg| if validator_class = Respect.validator_for(option) validator_class.new(arg).validate(value) end end end |