class Apes::Validators::ReferenceValidator

Validates references (relationships in the JSON API nomenclature).

Public Class Methods

new(options) click to toggle source

Creates a new validator.

@param options [Hash] The options for the validations. @return [Apes::Validators::ReferenceValidator] A new validator.

Calls superclass method
# 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

Public Instance Methods

validate_each(model, attribute, values) click to toggle source

Perform validation on a attribute of a model.

@param model [Object] The object to validate. @param attribute [String|Symbol] The attribute to validate. @param values [Array] The values of the attribute.

# 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