module GraphQL::Schema::Validation::Rules

def self.assert_property(property_name, *allowed_classes)

Returns:
  • (Proc) - A proc which will validate the input by calling `property_name` and asserting it is an instance of one of `allowed_classes`

Parameters:
  • allowed_classes (Class) -- Classes which the return value may be an instance of
  • property_name (Symbol) -- The method to validate
def self.assert_property(property_name, *allowed_classes)
  allowed_classes_message = allowed_classes.map(&:name).join(" or ")
  ->(obj) {
    property_value = obj.public_send(property_name)
    is_valid_value = allowed_classes.any? { |allowed_class| property_value.is_a?(allowed_class) }
    is_valid_value ? nil : "#{property_name} must return #{allowed_classes_message}, not #{property_value.class.name} (#{property_value.inspect})"
  }
end