Skip to content

Conversation

javiercn
Copy link
Member

@javiercn javiercn commented Aug 4, 2025

The ValidationsGenerator now supports discovering types marked with ValidatableTypeAttribute from two sources:

Framework attribute: Microsoft.Extensions.Validation.ValidatableTypeAttribute (from Microsoft.Extensions.Validation package)
SDK-generated attribute: Microsoft.Extensions.Validation.Embedded.ValidatableTypeAttribute (embedded by Razor SDK in projects with .razor files)
This enables developers to use [ValidatableType] in Blazor projects without explicitly referencing the experimental validation framework attribute, as the Razor SDK automatically embeds the attribute as needed on the assembly

@github-actions github-actions bot added the area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework label Aug 4, 2025
@javiercn javiercn marked this pull request as ready for review August 6, 2025 10:08
@Copilot Copilot AI review requested due to automatic review settings August 6, 2025 10:08
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 generated ValidatableTypeAttribute to work alongside the existing framework version in Blazor scenarios. The main purpose is to enable validation generators to recognize and process both the framework-provided ValidatableTypeAttribute and a generated version that can be provided by the Razor SDK.

Key Changes

  • Modified the validation generator to process both framework and generated versions of ValidatableTypeAttribute
  • Added test snapshots demonstrating the generated code output for auto-generated attributes
  • Created comprehensive test cases covering scenarios with both attribute types

Reviewed Changes

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

File Description
ValidationsGenerator.cs Modified to collect and combine validatable types from both framework and generated attributes
ValidationsGenerator.AttributeParser.cs Updated parameter names to follow discard pattern conventions
ValidationsGenerator.AutoGeneratedAttribute.cs Added test cases for discovering and using generated ValidatableTypeAttribute
Snapshot files (3) Test verification snapshots for generated code output

@javiercn javiercn linked an issue Aug 6, 2025 that may be closed by this pull request
@lewing lewing requested a review from a team August 6, 2025 14:25
@@ -0,0 +1,9 @@
//HintName: ValidatableTypeAttribute.g.cs
// <auto-generated/>
namespace TestApp;
Copy link
Member

Choose a reason for hiding this comment

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

This namespace looks incorrect. Is it because the source strings in the test code use file-scoped namespaces? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I regenerated the tests. For some reason the tests were passing but nothing was happening. I even introduced a compilation issue on purpose to make them fail and were green.

I switched to use the old style namespaces and Program main and that got them "working" again

@oroztocil
Copy link
Member

@javiercn

  1. Is there a complementary PR that adds emitting the attribute into projects?
  2. Does this mean that you get access to the generated attribute only in Razor SDK based projects? I.e. you can't have models in a (non-Razor) class library?

@javiercn
Copy link
Member Author

javiercn commented Aug 7, 2025

@javiercn

  1. Is there a complementary PR that adds emitting the attribute into projects?
  2. Does this mean that you get access to the generated attribute only in Razor SDK based projects? I.e. you can't have models in a (non-Razor) class library?

dotnet/sdk#50083 <- Complementary PR for emitting the attribute.

  1. Does this mean that you get access to the generated attribute only in Razor SDK based projects? I.e. you can't have models in a (non-Razor) class library?

Yep, but that's not a big deal. Referencing the Razor SDK is not a problem and the embedded (I'm going to start calling it like that) is a thing only for .net 10.0, we plan to move out of it once the other attribute stabilizes.

@javiercn javiercn force-pushed the javiercn/custom-validatabletypeattribute branch from 460cca1 to c887ba4 Compare August 7, 2025 20:56
@javiercn javiercn merged commit a0991ea into main Aug 8, 2025
29 checks passed
@javiercn javiercn deleted the javiercn/custom-validatabletypeattribute branch August 8, 2025 20:33
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Validation] Support embedded validatabletypeattribute

3 participants