Repository navigation
Description
Description
I have the below
#pragma warning disable ASP0029
[ValidatableType]
#pragma warning restore ASP0029
public class WeatherForecast
{
public int? Id { get; set; }
public required DateTime Date { get; set; }
[Range(-20, 55, ErrorMessage = "Temperature must be between -20 and 55.")]
public required int TemperatureC { get; set; }
[Required(ErrorMessage = "Summary is required.")]
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
[ValidateObjectMembers]
public SubWeatherForecast SubWeather { get; set; } = new();
}
#pragma warning disable ASP0029
[ValidatableType]
#pragma warning restore ASP0029
public class SubWeatherForecast
{
[Required(ErrorMessage = "Name is required.")]
public string? Name { get; set; }
}
[OptionsValidator]
public partial class WeatherForecastValidator : IValidateOptions<WeatherForecast>
{
}
[OptionsValidator]
public partial class SubWeatherForecastValidator : IValidateOptions<SubWeatherForecast>
{
}When this is triggered through Minimal API I get the correct error message of Temperature must be between -20 and 55.
However, when I execute through:
var validator = new WeatherForecastValidator();
var result = validator.Validate(null, weatherForecast);The error message reverts to default "The field WeatherForecast.TemperatureC must be between -20 and 55."
When I do the same thing with [Required(ErrorMessage = "Summary is required.")] it works correctly.
In addition when validating with Minimal API default the key on the sub class is correctly SubWeather.Name but when processing with validator.Validate the key is cut off and it is only "Name"
Reproduction Steps
Use the Dot Net 10 template, use minimal api end point, include builder.Services.Validation(). On the end point .DisableValidation(), then
var validator = new WeatherForecastValidator();
var result = validator.Validate(null, weatherForecast);Expected behavior
validator.Validate(null, weatherForecast); is processed in the same way that Minimal API is processed.
Actual behavior
The ErrorMessage in [Range] is not respected with validator.Validate and sub classes cut off the name of the key.
Regression?
No response
Known Workarounds
Don't use validator.Validate.
Configuration
No response
Other information
No response
Activity
dotnet-policy-service commentedon Jan 13, 2026
Tagging subscribers to this area: @dotnet/area-system-componentmodel-dataannotations
See info in area-owners.md if you want to be subscribed.
mguinness commentedon Jan 27, 2026
Repro
Tests
curl -d "Date=2000-01-01&TemperatureC=10&Summary=Cool" http://localhost:5000Forecast is 'Cool' with temp 10C.
curl -d "Date=2000-01-01&TemperatureC=100&Summary=Cool" http://localhost:5000{"title":"One or more validation errors occurred.","errors":{"TemperatureC":["Temperature must be between -20 and 55."]}}
curl -d "Date=2000-01-01&TemperatureC=10&Summary=Cool&SubWeather.Name=" http://localhost:5000{"title":"One or more validation errors occurred.","errors":{"SubWeather.Name":["Name is required."]}}
curl -d "Date=2000-01-01&TemperatureC=10&Summary=Cool" http://localhost:5000/validatorForecast is 'Cool' with temp 10C.
curl -d "Date=2000-01-01&TemperatureC=100&Summary=Cool&SubWeather.Name=Test" http://localhost:5000/validatorTemperatureC: The field WeatherForecast.TemperatureC must be between -20 and 55.
curl -d "Date=2000-01-01&TemperatureC=10&Summary=Cool&SubWeather.Name=" http://localhost:5000/validatorName: Name is required.
dotnet-policy-service commentedon Mar 26, 2026
Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.
RichardD2 commentedon Mar 26, 2026
For some reason, the source generator synthesizes its own version of the
Rangeattribute, rather than relying on the one fromSystem.ComponentModel.Annotations. The synthesized attribute ignores any custom error message, and always returns a default message:Searching this repo for
__SourceGen__suggests the problem also affects:LengthAttributeMinLengthAttributeMaxLengthAttributeCompareAttributeIt may affect other attributes as well, but those are the ones that come up in the test projects in this repo.
RichardD2 commentedon Mar 26, 2026
The documentation explains why the attributes are replaced - although it makes no mention of the
CompareAttribute.I'm guessing the custom error message is ignored because it could potentially end up using reflection - specifically, if you use the
ErrorMessageResourceType, theSetResourceAccessorByPropertyLookupmethod has to use reflection to locate the property or method used to return the error message.But it's slightly annoying that the synthesized attributes ignore hard-coded, non-localized error messages. Particularly when other validation attributes respect the custom error messages, and could even end up triggering the reflection code path.
Laurianti commentedon Oct 16, 2026
I'd like to take the
ErrorMessagepart of this.The generator replaces
[Range]with a synthesized__SourceGen__RangeAttribute, and itsFormatErrorMessagealways uses the built-in message. SoErrorMessageandErrorMessageResourceName/ErrorMessageResourceTypeare ignored. The fix is to use the built-in message only when none of them is set, asRangeAttributedoes.MaxLength,MinLength,LengthandComparealready useErrorMessageString, so onlyRangeis affected.The nested member name (
SubWeather.Namereported asName) is a separate change, and I'm leaving it out.Honor ErrorMessage in OptionsValidator generated Range attribute (#13…