Module: CouchRest::Validation

Defined in:
lib/couchrest/validation.rb,
lib/couchrest/validation/auto_validate.rb,
lib/couchrest/validation/validation_errors.rb,
lib/couchrest/validation/contextual_validators.rb,
lib/couchrest/validation/validators/formats/url.rb,
lib/couchrest/validation/validators/formats/email.rb,
lib/couchrest/validation/validators/format_validator.rb,
lib/couchrest/validation/validators/method_validator.rb,
lib/couchrest/validation/validators/length_validator.rb,
lib/couchrest/validation/validators/generic_validator.rb,
lib/couchrest/validation/validators/numeric_validator.rb,
lib/couchrest/validation/validators/absent_field_validator.rb,
lib/couchrest/validation/validators/confirmation_validator.rb,
lib/couchrest/validation/validators/required_field_validator.rb

Defined Under Namespace

Modules: AutoValidate, ClassMethods, Format, ValidatesAbsent, ValidatesFormat, ValidatesIsConfirmed, ValidatesIsNumber, ValidatesLength, ValidatesPresent, ValidatesWithMethod Classes: AbsentFieldValidator, ConfirmationValidator, ContextualValidators, FormatValidator, GenericValidator, LengthValidator, MethodValidator, NumericValidator, RequiredFieldValidator, ValidationErrors

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/couchrest/validation.rb', line 50

def self.included(base)
  base.class_eval <<-EOS, __FILE__, __LINE__ + 1
      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
  EOS
  
  base.extend(ClassMethods)
  base.class_eval <<-EOS, __FILE__, __LINE__ + 1
    define_callbacks :validate
    if method_defined?(:_run_save_callbacks)
      set_callback :save, :before, :check_validations
    end
  EOS
  base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
    def self.define_property(name, options={}, &block)
      property = super
      auto_generate_validations(property) unless property.nil?
    end
  RUBY_EVAL
end

Instance Method Details

- (Object) check_validations(context = :default)

Ensures the object is valid for the context provided, and otherwise throws :halt and returns false.



95
96
97
# File 'lib/couchrest/validation.rb', line 95

def check_validations(context = :default)
  throw(:halt, false) unless context.nil? || valid?(context)
end

- (Object) errors

Return the ValidationErrors



101
102
103
# File 'lib/couchrest/validation.rb', line 101

def errors
  @errors ||= ValidationErrors.new
end

- (Boolean) recursive_valid?(target, context, state)

Do recursive validity checking



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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

- (Boolean) valid?(context = :default)

Check if a resource is valid in a given context



121
122
123
# File 'lib/couchrest/validation.rb', line 121

def valid?(context = :default)
  recursive_valid?(self, context, true)
end

- (Boolean) valid_for_default?

Alias for valid?(:default)



115
116
117
# File 'lib/couchrest/validation.rb', line 115

def valid_for_default?
  valid?(:default)
end

- (Boolean) validatable?

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



109
110
111
# File 'lib/couchrest/validation.rb', line 109

def validatable?
  true
end

- (Object) validate_casted_arrays

checking on casted objects



126
127
128
129
130
131
132
133
134
135
136
137
# 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

- (Object) validation_property(field_name)

Get the corresponding Object property, if it exists.



165
166
167
# File 'lib/couchrest/validation.rb', line 165

def validation_property(field_name)
  properties.find{|p| p.name == field_name}
end

- (Object) validation_property_value(name)



160
161
162
# File 'lib/couchrest/validation.rb', line 160

def validation_property_value(name)
  self.respond_to?(name, true) ? self.send(name) : nil
end