class Graphiti::RequestValidators::UpdateValidator

Public Instance Methods

validate() click to toggle source
Calls superclass method
# File lib/graphiti/request_validators/update_validator.rb, line 4
def validate
  if required_payload? && payload_matches_endpoint?
    super
  else
    false
  end
end

Private Instance Methods

attribute_mismatch(attr_path) click to toggle source
# File lib/graphiti/request_validators/update_validator.rb, line 14
def attribute_mismatch(attr_path)
  @error_class = Graphiti::Errors::ConflictRequest
  @errors.add(
    attr_path.join("."),
    :attribute_mismatch,
    message: "does not match the server endpoint"
  )
end
payload_matches_endpoint?() click to toggle source
# File lib/graphiti/request_validators/update_validator.rb, line 34
def payload_matches_endpoint?
  unless @params.dig(:data, :id) == @params.dig(:filter, :id)
    attribute_mismatch([:data, :id])
  end

  meta_type = @params.dig(:data, :type)

  # NOTE: calling #to_s and comparing 2 strings is slower than
  # calling #to_sym and comparing 2 symbols. But pre ruby-2.2
  # #to_sym on user supplied data would lead to a memory leak.
  if @root_resource.type.to_s != meta_type
    if @root_resource.polymorphic?
      begin
        @root_resource.class.resource_for_type(meta_type).new
      rescue Errors::PolymorphicResourceChildNotFound
        attribute_mismatch([:data, :type])
      end
    else
      attribute_mismatch([:data, :type])
    end
  end

  errors.blank?
end
required_payload?() click to toggle source
# File lib/graphiti/request_validators/update_validator.rb, line 23
def required_payload?
  [
    [:data],
    [:data, :type],
    [:data, :id]
  ].each do |required_attr|
    attribute_mismatch(required_attr) unless @params.dig(*required_attr)
  end
  errors.blank?
end