Module: CouchRest::Validation
- Defined in:
- lib/couchrest/validation.rb,
lib/couchrest/mixins/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/length_validator.rb,
lib/couchrest/validation/validators/method_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
-
#check_validations(context = :default) ⇒ Object
Ensures the object is valid for the context provided, and otherwise throws :halt and returns false.
-
#errors ⇒ Object
Return the ValidationErrors.
-
#recursive_valid?(target, context, state) ⇒ Boolean
Do recursive validity checking.
-
#valid?(context = :default) ⇒ Boolean
Check if a resource is valid in a given context.
-
#valid_for_default? ⇒ Boolean
Alias for valid?(:default).
-
#validatable? ⇒ Boolean
Mark this resource as validatable.
-
#validate_casted_arrays ⇒ Object
checking on casted objects.
-
#validation_property(field_name) ⇒ Object
Get the corresponding Object property, if it exists.
- #validation_property_value(name) ⇒ Object
Class Method Details
.included(base) ⇒ Object
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 |
# File 'lib/couchrest/validation.rb', line 50 def self.included(base) base.extlib_inheritable_accessor(:auto_validation) base.class_eval <<-EOS, __FILE__, __LINE__ + 1 # 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={}) super auto_generate_validations(properties.last) if properties && properties.size > 0 autovalidation_check = true end RUBY_EVAL end |
Instance Method Details
#check_validations(context = :default) ⇒ Object
Ensures the object is valid for the context provided, and otherwise throws :halt and returns false.
94 95 96 |
# File 'lib/couchrest/validation.rb', line 94 def check_validations(context = :default) throw(:halt, false) unless context.nil? || valid?(context) end |
#errors ⇒ Object
Return the ValidationErrors
100 101 102 |
# File 'lib/couchrest/validation.rb', line 100 def errors @errors ||= ValidationErrors.new end |
#recursive_valid?(target, context, state) ⇒ Boolean
Do recursive validity checking
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/couchrest/validation.rb', line 140 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 |
#valid?(context = :default) ⇒ Boolean
Check if a resource is valid in a given context
120 121 122 |
# File 'lib/couchrest/validation.rb', line 120 def valid?(context = :default) recursive_valid?(self, context, true) end |
#valid_for_default? ⇒ Boolean
Alias for valid?(:default)
114 115 116 |
# File 'lib/couchrest/validation.rb', line 114 def valid_for_default? valid?(:default) end |
#validatable? ⇒ Boolean
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
108 109 110 |
# File 'lib/couchrest/validation.rb', line 108 def validatable? true end |
#validate_casted_arrays ⇒ Object
checking on casted objects
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/couchrest/validation.rb', line 125 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 |
#validation_property(field_name) ⇒ Object
Get the corresponding Object property, if it exists.
164 165 166 |
# File 'lib/couchrest/validation.rb', line 164 def validation_property(field_name) properties.find{|p| p.name == field_name} end |
#validation_property_value(name) ⇒ Object
159 160 161 |
# File 'lib/couchrest/validation.rb', line 159 def validation_property_value(name) self.respond_to?(name, true) ? self.send(name) : nil end |