Class: Apes::Validators::ReferenceValidator
- Inherits:
-
BaseValidator
- Object
- ActiveModel::EachValidator
- BaseValidator
- Apes::Validators::ReferenceValidator
- Defined in:
- lib/apes/validators.rb
Overview
Validates references (relationships in the JSON API nomenclature).
Instance Method Summary (collapse)
-
- (Apes::Validators::ReferenceValidator) initialize(options)
constructor
Creates a new validator.
-
- (Object) validate_each(model, attribute, values)
Perform validation on a attribute of a model.
Constructor Details
- (Apes::Validators::ReferenceValidator) initialize(options)
Creates a new validator.
33 34 35 36 37 |
# File 'lib/apes/validators.rb', line 33 def initialize() @class_name = [:class_name] label = [:label] || [:class_name].classify super(.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.
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 |