Class: Graphiti::RequestValidators::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti/request_validators/validator.rb

Direct Known Subclasses

UpdateValidator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_resource, raw_params, action) ⇒ Validator

Returns a new instance of Validator.



6
7
8
9
10
11
# File 'lib/graphiti/request_validators/validator.rb', line 6

def initialize(root_resource, raw_params, action)
  @root_resource = root_resource
  @params = normalized_params(raw_params)
  @errors = Graphiti::Util::SimpleErrors.new(raw_params)
  @action = action
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/graphiti/request_validators/validator.rb', line 4

def errors
  @errors
end

Instance Method Details

#deserialized_payloadObject



45
46
47
# File 'lib/graphiti/request_validators/validator.rb', line 45

def deserialized_payload
  @deserialized_payload ||= Graphiti::Deserializer.new(@params)
end

#validateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/graphiti/request_validators/validator.rb', line 13

def validate
  # Right now, all requests - even reads - go through the validator
  # In the future these should have their own validation logic, but
  # for now we can just bypass
  return true unless @params.has_key?(:data)

  resource = @root_resource

  if @params[:data].has_key?(:type)
    if (meta_type = deserialized_payload.meta[:type].try(:to_sym))
      if @root_resource.type != meta_type && @root_resource.polymorphic?
        resource = @root_resource.class.resource_for_type(meta_type).new
      end
    end

    typecast_attributes(resource, deserialized_payload.attributes, @action, deserialized_payload.meta[:payload_path])
    process_relationships(resource, deserialized_payload.relationships, deserialized_payload.meta[:payload_path])
  else
    errors.add(:"data.type", :missing)
  end

  errors.blank?
end

#validate!Object



37
38
39
40
41
42
43
# File 'lib/graphiti/request_validators/validator.rb', line 37

def validate!
  unless validate
    raise @error_class || Graphiti::Errors::InvalidRequest, errors
  end

  true
end