Skip to content

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

@stephentoub

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions