Skip to content

Incorrect nullable annotation on Validator.TryValidateValue's value? #91162

Closed

Description

@stephentoub

It's defined like:

public static bool TryValidateValue(object value, ValidationContext validationContext, ICollection<ValidationResult>? validationResults, IEnumerable<ValidationAttribute> validationAttributes)

and the XML doc comment states:

/// <param name="value">The value to test.  It cannot be null.</param>

However, the implementation appears to permit null (the only place it's used is being passed to an object? argument in a helper method), the purpose of an attribute like [Required] is to validate one isn't null (which suggests null could otherwise be valid, and the new .NET 8 source generator for options is having to emit code like:

if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.Path!, context, validationResults, validationAttributes))
{
    builder.AddResults(validationResults);
}

(note the ! on the first argument) because the input might be null but the method is defined to not accept null.

cc: @jeffhandley, @eiriktsarpalis

Activity

  1. added this to the 8.0.0 milestone on Aug 26, 2023
  2. jeffhandley commented on Aug 28, 2023

    @jeffhandley
    Member

    I tried doing some archaeology to figure out where the mismatch between the comment and the behavior came from, but I came up empty-handed.

  3. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Aug 29, 2023
  4. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Aug 29, 2023
  5. ghost locked as resolved and limited conversation to collaborators on Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions