module MyJohnDeereApi::Validators::Base

This is a mix-in for Create/Update Reqest classes. It assumes that the class in question has a hash of attributes that will be passed to the request.

This module creates the errors hash as a reader. The key of the hash is the attribute name, and the value is an array of error messages for that attribute. Follow this format when defining custom validations in the `validate_attributes` method.

Attributes

errors[R]

Public Instance Methods

valid?() click to toggle source

Runs validations, adding to the errors hash as needed. Returns true if the errors hash is still empty after all validations have been run.

# File lib/my_john_deere_api/validators/base.rb, line 28
def valid?
  return @is_valid if defined?(@is_valid)

  @errors = {}
  validate_required
  validate_attributes

  @is_valid = errors.empty?
end
validate!() click to toggle source

Raises an error if the record is invalid. Passes the errors hash to the error, in order to build a useful message string.

# File lib/my_john_deere_api/validators/base.rb, line 19
def validate!
  raise(InvalidRecordError, errors) unless valid?
  true
end

Private Instance Methods

required_attributes() click to toggle source

Attributes that must be specified, override in child module if needed

# File lib/my_john_deere_api/validators/base.rb, line 52
def required_attributes
  []
end
validate_attributes() click to toggle source

Handle any custom validation for this class, override in child module if needed.

Add messages to errors hash with the attribute name as the key, and an array of error messages as the value.

# File lib/my_john_deere_api/validators/base.rb, line 62
def validate_attributes
  # Example:
  # errors[:name] = "can't be blank" if errors[:name].size == 0
end
validate_required() click to toggle source

Validates required attributes

# File lib/my_john_deere_api/validators/base.rb, line 43
def validate_required
  required_attributes.each do |attr|
    errors[attr] = 'is required' unless attributes[attr]
  end
end