Skip to content

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

Closed

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

Activity

  1. ghost added
    untriaged
    on Jun 28, 2023
  2. xakep139 commented on Jun 28, 2023

    @xakep139
    ContributorAuthor
  3. added this to the 8.0.0 milestone on Jun 29, 2023
  4. ghost removed
    untriaged
    on Jun 29, 2023
  5. self-assigned this
    on Jun 29, 2023
  6. ghost added
    in-pr
    on Jun 30, 2023
  7. ghost removed
    in-pr
    on Jul 2, 2023
  8. ghost locked as resolved and limited conversation to collaborators on Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions