GQL019: Validator method must be valid
Value | |
---|---|
Rule ID | GQL019 |
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 Validator
attribute
has invalid signature.
Rule description
The method specified by the Validator
attribute must be:
static
- return
void
- have a single argument of type
object
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
{
[Validator(nameof(Validate))]
public string Hello1 { get; set; }
[Validator(typeof(Validators), nameof(Validators.ValidateValue))]
public string Hello2 { get; set; }
// must be static
private void Validate(object value) => _ = Convert.ToInt32(value);
}
public class Validators
{
// must be public
internal static void ValidateValue(object value) => _ = Convert.ToInt32(value);
}
Example of how to fix
Fix the attribute argument to match the method name
public class TestClass
{
[Validator(nameof(Validate))]
public string Hello1 { get; set; }
[Validator(typeof(Validators), nameof(Validators.ValidateValue))]
public string Hello2 { get; set; }
// must be static
private static void Validate(object value) => _ = Convert.ToInt32(value);
}
public class Validators
{
public static void ValidateValue(object value) => _ = Convert.ToInt32(value);
}
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 GQL019
// The code that's violating the rule is on this line.
#pragma warning restore GQL019
To disable the rule for a file, folder, or project, set its severity to none
in the
configuration file.
[*.cs]
dotnet_diagnostic.GQL019.severity = none
For more information, see How to suppress code analysis warnings.
Related rules
GQL017: Could not find method
GQL018: Parser method must be valid
GQL020: ValidateArguments method must be valid