GQL020: ValidateArguments method must be valid

Value
Rule ID GQL020
Category Usage
Default severity Error
Enabled by default Yes
Code fix provided No
Introduced in v8.1

Cause

This rule is triggered when parser method specified by the ValidateArguments attribute has invalid signature.

Rule description

The method specified by the ValidateArguments attribute must be:

  • static
  • return ValueTask
  • have a single argument of type FieldArgumentsValidationContext

Additionally, if the specified method is defined in a different class from where the attribute is applied, the method must be declared as public.

How to fix violations

Fix the method signature to match the required pattern.

Example of a violation

public class TestClass
{
    [ValidateArguments(nameof(Validate))]
    public string Hello1(string name) => $"Hello {name}";

    [ValidateArguments(typeof(Validators), nameof(Validators.ValidateArgs))]
    public string Hello2(string name) => $"Greeting {name}";

    // wrong argument type
    private static ValueTask Validate(object context) => ValueTask.CompletedTask;
}

public class Validators
{
    // must be public
    internal static ValueTask ValidateArgs(FieldArgumentsValidationContext context) => ValueTask.CompletedTask;
}

Example of how to fix

Fix the attribute argument to match the method name

public class TestClass
{
    [ValidateArguments(nameof(Validate))]
    public string Hello1(string name) => $"Hello {name}";

    [ValidateArguments(typeof(Validators), nameof(Validators.ValidateArgs))]
    public string Hello2(string name) => $"Greeting {name}";

    private static ValueTask Validate(FieldArgumentsValidationContext context) => ValueTask.CompletedTask;
}

public class Validators
{
    public static ValueTask ValidateArgs(FieldArgumentsValidationContext context) => ValueTask.CompletedTask;
}

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable GQL020
// The code that's violating the rule is on this line.
#pragma warning restore GQL020

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.cs]
dotnet_diagnostic.GQL020.severity = none

For more information, see How to suppress code analysis warnings.

GQL017: Could not find method
GQL018: Parser method must be valid
GQL019: Validator method must be valid