Ensures the object is valid for the context provided, and otherwise throws :halt and returns false.
# File lib/couchrest/validation.rb, line 95 def check_validations(context = :default) throw(:halt, false) unless context.nil? || valid?(context) end
Return the ValidationErrors
# File lib/couchrest/validation.rb, line 101 def errors @errors ||= ValidationErrors.new end
Do recursive validity checking
# File lib/couchrest/validation.rb, line 141 def recursive_valid?(target, context, state) valid = state target.each do |key, prop| if prop.is_a?(Array) prop.each do |item| if item.validatable? valid = recursive_valid?(item, context, valid) && valid end end elsif prop.validatable? valid = recursive_valid?(prop, context, valid) && valid end end target._run_validate_callbacks do target.class.validators.execute(context, target) && valid end end
Check if a resource is valid in a given context
# File lib/couchrest/validation.rb, line 121 def valid?(context = :default) recursive_valid?(self, context, true) end
Alias for valid?(:default)
# File lib/couchrest/validation.rb, line 115 def valid_for_default? valid?(:default) end
Mark this resource as validatable. When we validate associations of a resource we can check if they respond to validatable? before trying to recursivly validate them
# File lib/couchrest/validation.rb, line 109 def validatable? true end
checking on casted objects
# File lib/couchrest/validation.rb, line 126 def validate_casted_arrays result = true array_casted_properties = self.class.properties.select { |property| property.casted && property.type.instance_of?(Array) } array_casted_properties.each do |property| casted_values = self.send(property.name) next unless casted_values.is_a?(Array) && casted_values.first.respond_to?(:valid?) casted_values.each do |value| result = (result && value.valid?) if value.respond_to?(:valid?) end end result end
Get the corresponding Object property, if it exists.
# File lib/couchrest/validation.rb, line 165 def validation_property(field_name) properties.find{|p| p.name == field_name} end
# File lib/couchrest/validation.rb, line 160 def validation_property_value(name) self.respond_to?(name, true) ? self.send(name) : nil end
# File lib/couchrest/validation.rb, line 50 def self.included(base) base.class_eval " extend CouchRest::InheritableAttributes couchrest_inheritable_accessor(:auto_validation) # Callbacks define_callbacks :validate # Turn off auto validation by default self.auto_validation ||= false # Force the auto validation for the class properties # This feature is still not fully ported over, # test are lacking, so please use with caution def self.auto_validate! self.auto_validation = true end # share the validations with subclasses def self.inherited(subklass) self.validators.contexts.each do |k, v| subklass.validators.contexts[k] = v.dup end super end ", __FILE__, __LINE__ + 1 base.extend(ClassMethods) base.class_eval " define_callbacks :validate if method_defined?(:_run_save_callbacks) set_callback :save, :before, :check_validations end ", __FILE__, __LINE__ + 1 base.class_eval " def self.define_property(name, options={}, &block) property = super auto_generate_validations(property) unless property.nil? end ", __FILE__, __LINE__ + 1 end