Class: CouchRest::Validation::RequiredFieldValidator

Inherits:
GenericValidator show all
Defined in:
lib/couchrest/validation/validators/required_field_validator.rb

Overview

Author:

Since:

Instance Attribute Summary

Attributes inherited from GenericValidator

#field_name, #if_clause, #unless_clause

Instance Method Summary (collapse)

Methods inherited from GenericValidator

#==, #add_error, #execute?

Constructor Details

- (RequiredFieldValidator) initialize(field_name, options = {})

Returns a new instance of RequiredFieldValidator

Since:

  • 0.9



33
34
35
36
# File 'lib/couchrest/validation/validators/required_field_validator.rb', line 33

def initialize(field_name, options={})
  super
  @field_name, @options = field_name, options
end

Instance Method Details

- (Object) call(target)

Since:

  • 0.9



38
39
40
41
42
43
44
45
46
47
# File 'lib/couchrest/validation/validators/required_field_validator.rb', line 38

def call(target)
  value = target.validation_property_value(field_name)
  property = target.validation_property(field_name.to_s)
  return true if present?(value, property)

  error_message = @options[:message] || default_error(property)
  add_error(target, error_message, field_name)

  false
end