Skip to content

Make new validations consistent with System.ComponentModel.DataAnnotations behavior #63231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 16, 2025

Conversation

oroztocil
Copy link
Member

@oroztocil oroztocil commented Aug 12, 2025

  • Adds support for type-level validation attributes to the new validation infrastructure.
  • Changes validation logic in ValidatableTypeInfo.ValidateAsync so that it follows the same ordering and short-circuiting rules as System.ComponentModel.DataAnnotations.Validator (See the original implementation.)
  • Adds tests for the new ordering rules and removes/updates existing tests that depended on the previous behavior.

Fixes #62584

@github-actions github-actions bot added the area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework label Aug 12, 2025
@oroztocil oroztocil changed the title Oroztocil/scm validator behavior Make new validations consistent with System.ComponentModel.DataAnnotations behavior Aug 12, 2025
@oroztocil oroztocil removed the area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework label Aug 13, 2025
@oroztocil oroztocil force-pushed the oroztocil/scm-validator-behavior branch from 8c0481c to 48cf20a Compare August 14, 2025 11:39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just chmod +x, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, is it ok to add it here?

@oroztocil oroztocil added area-blazor Includes: Blazor, Razor Components area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-validation Issues related to model validation in minimal and controller-based APIs labels Aug 14, 2025
@oroztocil oroztocil marked this pull request as ready for review August 14, 2025 15:06
@Copilot Copilot AI review requested due to automatic review settings August 14, 2025 15:06
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for type-level validation attributes to the new validation infrastructure and updates validation logic to follow the same ordering and short-circuiting rules as System.ComponentModel.DataAnnotations.Validator. The key changes include:

  • Implementation of type-level validation attribute support in the validation framework
  • Modification of validation order to match DataAnnotations behavior: property attributes → type attributes → IValidatableObject
  • Addition of short-circuiting logic to stop validation when errors are found in earlier phases

Reviewed Changes

Copilot reviewed 31 out of 32 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
ValidatableTypeInfo.cs Core validation logic updated with proper ordering and short-circuiting behavior
TestValidatableTypeInfo.cs New test helper class to support type-level attribute testing
Generator snapshots Updated generated code to support type-level validation attributes
Test files Added comprehensive tests for validation ordering and short-circuiting behavior
Generator parsers/emitters Enhanced to detect and generate code for type-level validation attributes

Copy link
Member

@captainsafia captainsafia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall -- posted one comment about the caches for types.

@@ -244,6 +244,7 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex

private class MockValidatableTypeInfo(Type type, ValidatablePropertyInfo[] members) : ValidatableTypeInfo(type, members)
{
protected override ValidationAttribute[] GetValidationAttributes() => [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the ValidatableTypeInfo is marked as experimental, we're OK to make this change without API review. Also, it follows the same patter as the ValidatablePropertyInfo that was already code reviewed so we should be good there anyways.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this file look good. I appreciate the readability improvements from moving each stage to a separate method.

// Get attributes from the type itself and its super types
foreach (var attr in t.GetCustomAttributes(typeof(global::System.ComponentModel.DataAnnotations.ValidationAttribute), true))
{
if (attr is global::System.ComponentModel.DataAnnotations.ValidationAttribute validationAttribute)
Copy link
Member

@BrennanConroy BrennanConroy Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this redundant?
Edit: Oh you need the type, use GetCustomAttributes<T> then?

Copy link
Member Author

@oroztocil oroztocil Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I replaced it with the generic extension. I think I had it like this because I initially did not realize that Type is actually derived from MemberInfo (so I didn't see the overload), and later I forgot to change it.

oroztocil and others added 3 commits August 15, 2025 01:42
Co-authored-by: Brennan <brecon@microsoft.com>
Co-authored-by: Brennan <brecon@microsoft.com>
@captainsafia
Copy link
Member

/ba-g Current test failure appears to be an unrelated flake.

@lewing lewing merged commit 09ad011 into main Aug 16, 2025
28 of 29 checks passed
@lewing lewing deleted the oroztocil/scm-validator-behavior branch August 16, 2025 00:17
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 16, 2025
wtgodbe added a commit that referenced this pull request Aug 18, 2025
* Avoid race that can cause Kestrel's RequestAborted to not fire (#62385)

* Send Keep-Alive Ping Immediately When Previous Ping Is Overdue (#63195)

* Make new validations consistent with System.ComponentModel.DataAnnotations behavior (#63231)

* Add support for type-level validation attributes, update validation ordering

* Code review fix, test fix

* Fix trimming annotation

* Fix trimming annotation

* Separate caches for property and type attributes

* Fix typo

Co-authored-by: Brennan <brecon@microsoft.com>

* Fix typo

Co-authored-by: Brennan <brecon@microsoft.com>

* Fix and simplify the emitted code

* Update src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGeneratorTestBase.cs

---------

Co-authored-by: Brennan <brecon@microsoft.com>
Co-authored-by: Safia Abdalla <safia@microsoft.com>

* Search for slnx files when setting solution-relative content root (#61305)

* Address API review feedback for what was IApiEndpointMetadata (#63283)

---------

Co-authored-by: Stephen Halter <halter73@gmail.com>
Co-authored-by: Reagan Yuan <reaganyuan27@gmail.com>
Co-authored-by: Ondřej Roztočil <roztocil@outlook.com>
Co-authored-by: Brennan <brecon@microsoft.com>
Co-authored-by: Safia Abdalla <safia@microsoft.com>
Co-authored-by: Jacob Bundgaard <jbu@templafy.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-validation Issues related to model validation in minimal and controller-based APIs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update Minimal API validation logic for backwards compatibility
5 participants