Skip to content

OptionsValidator source-gen shouldn't cover static and const members #88150

@xakep139

Description

@xakep139

Description

Recently added OptionsValidator (issue #85475 and PR #87587) considers all members of a TOption for validation, however we shouldn't validate static ones. Additionally, const fields should be ignored or a warning should be emitted if such members are annotated with data validation attributes (inherited from ValidationAttribute).

Reproduction Steps

[OptionsValidator]
public partial class MyValidator : IValidateOptions<MyOptions>
{
}

public class MyOptions
{
    [Required]
    public static string? StaticStringField;

    [Required]
    public static string? StaticStringProperty => "static";

    [Required]
    public const string? ConstString = null;
}

Expected behavior

A warning is emitted for all three members, no static or const members are validated in the generated code.

Actual behavior

The generator produces the next code which won't compile:
image

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK 8.0.100-preview.7.23327.3

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions