module ContentfulLite::Validations::IncludedChildValidator

@api private

Constants

BASE_OPTIONS

Public Instance Methods

validate_each(record, attr_name, value) click to toggle source
# File lib/contentful_lite/validations/included_child_validator.rb, line 8
def validate_each(record, attr_name, value)
  if value.blank?
    record_error(record, attr_name, "value is blank") unless options[:allow_blank]
  elsif options[:array]
    validate_array(record, attr_name, value)
  else
    validate_child(record, attr_name, value)
  end
end

Private Instance Methods

add_options_keys(*options) click to toggle source
# File lib/contentful_lite/validations/included_child_validator.rb, line 30
def add_options_keys(*options)
  @options_keys = BASE_OPTIONS + options
end
options_keys() click to toggle source
# File lib/contentful_lite/validations/included_child_validator.rb, line 34
def options_keys
  @options_keys
end
record_error(record, attr_name, message) click to toggle source
# File lib/contentful_lite/validations/included_child_validator.rb, line 20
def record_error(record, attr_name, message)
  record.errors.add(attr_name, :invalid, { message: message }.merge(options.except(self.class.options_keys)))
end
validate_array(record, attr_name, value) click to toggle source
# File lib/contentful_lite/validations/included_child_validator.rb, line 24
def validate_array(record, attr_name, value)
  record_error(record, attr_name, "value is not an array") && return unless value.is_a?(Array)
  value.each_with_index { |asset, idx| validate_child(record, attr_name, asset, "[#{idx}]") }
end