Class: Apes::Validators::ReferenceValidator

Inherits:
BaseValidator
  • Object
show all
Defined in:
lib/apes/validators.rb

Overview

Validates references (relationships in the JSON API nomenclature).

Instance Method Summary (collapse)

Constructor Details

- (Apes::Validators::ReferenceValidator) initialize(options)

Creates a new validator.

Parameters:

  • options (Hash)

    The options for the validations.



33
34
35
36
37
# File 'lib/apes/validators.rb', line 33

def initialize(options)
  @class_name = options[:class_name]
  label = options[:label] || options[:class_name].classify
  super(options.reverse_merge(default_message: "must be a valid #{label} (cannot find a #{label} with id \"%s\")"))
end

Instance Method Details

- (Object) validate_each(model, attribute, values)

Perform validation on a attribute of a model.

Parameters:

  • model (Object)

    The object to validate.

  • attribute (String|Symbol)

    The attribute to validate.

  • values (Array)

    The values of the attribute.



44
45
46
47
48
49
50
51
# File 'lib/apes/validators.rb', line 44

def validate_each(model, attribute, values)
  values = Serializers::JSON.load(values, false, values)

  values.ensure_array.each do |value|
    checked = @class_name.classify.constantize.find_with_any(value)
    add_failure(attribute, model, value) unless checked
  end
end