lib/graphiti/request_validators/validator.rb in graphiti-1.2.21 vs lib/graphiti/request_validators/validator.rb in graphiti-1.2.25

- old
+ new

@@ -1,14 +1,15 @@ module Graphiti module RequestValidators class Validator attr_reader :errors - def initialize(root_resource, raw_params) + def initialize(root_resource, raw_params, action) @root_resource = root_resource @raw_params = raw_params @errors = Graphiti::Util::SimpleErrors.new(raw_params) + @action = action end def validate resource = @root_resource if (meta_type = deserialized_payload.meta[:type].try(:to_sym)) @@ -66,9 +67,14 @@ end end def typecast_attributes(resource, attributes, payload_path) attributes.each_pair do |key, value| + # Only validate id if create action, otherwise it's only used for lookup + next if @action != :create && + key == :id && + resource.class.config[:attributes][:id][:writable] == false + begin attributes[key] = resource.typecast(key, value, :writable) rescue Graphiti::Errors::UnknownAttribute @errors.add(fully_qualified_key(key, payload_path), :unknown_attribute, message: "is an unknown attribute") rescue Graphiti::Errors::InvalidAttributeAccess