module GraphQL::Schema::Validation::Rules

def self.assert_property_mapping(property_name, from_class, to_class)

Returns:
  • (Proc) - A proc to validate that validates the input by calling `property_name` and asserting that the return value is a Hash of `{from_class => to_class}` pairs

Parameters:
  • to_class (Class) -- The class for values in the return value
  • from_class (Class) -- The class for keys in the return value
  • property_name (Symbol) -- The method whose return value will be validated
def self.assert_property_mapping(property_name, from_class, to_class)
  ->(obj) {
    property_value = obj.public_send(property_name)
    if !property_value.is_a?(Hash)
      "#{property_name} must be a hash of {#{from_class.name} => #{to_class.name}}, not a #{property_value.class.name} (#{property_value.inspect})"
    else
      invalid_key, invalid_value = property_value.find { |key, value| !key.is_a?(from_class) || !value.is_a?(to_class) }
      if invalid_key
        "#{property_name} must map #{from_class} => #{to_class}, not #{invalid_key.class.name} => #{invalid_value.class.name} (#{invalid_key.inspect} => #{invalid_value.inspect})"
      else
        nil # OK
      end
    end
  }
end