Skip to content

Add SkipValidationAttribute to Microsoft.Extensions.Validation #63103

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 3 commits into from
Aug 11, 2025

Conversation

oroztocil
Copy link
Member

@oroztocil oroztocil commented Aug 4, 2025

Adds an experimental attribute [SkipValidation] that can be used to opt out of validation for a specific property, method parameter, or type.

  • When applied to a property, the validator skips that property during validation.
  • When aplied to a method parameter, the validator skips the parameter during validation.
  • When applied to a type, the validator skips all properties and parameters of that type during validation.
  • The source generator skips creating validation code for a type/property/parameter that is only present in the compilation as annotated with the attribute. (If there is at least one use that is not skipped from validation, the code is generated.)

Also fixes issue with GeneratorTests that were using VerifyValidatableType - now the tests can actually fail because the call is awaited and the assertion exceptions are not lost. (Tests using VerifyEndpoint were already awaited.)

Fixes #62862

@github-actions github-actions bot added the area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework label Aug 4, 2025
@oroztocil oroztocil force-pushed the oroztocil/skip-validation-attribute branch from 5b29848 to c30a1b8 Compare August 6, 2025 18:00
@oroztocil oroztocil marked this pull request as ready for review August 6, 2025 19:33
@Copilot Copilot AI review requested due to automatic review settings August 6, 2025 19:33
@oroztocil oroztocil requested a review from a team as a code owner August 6, 2025 19:33
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 pull request adds an experimental [SkipValidation] attribute to Microsoft.Extensions.Validation that allows developers to opt out of validation for specific properties, method parameters, or entire types. This provides fine-grained control over validation behavior while maintaining the default validation functionality.

Key changes:

  • Introduces the SkipValidationAttribute that can be applied to classes, properties, or parameters
  • Updates the source generator to skip validation code generation for annotated elements
  • Fixes test infrastructure to properly await VerifyValidatableType calls to prevent lost assertion exceptions

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
SkipValidationAttribute.cs New experimental attribute class with proper target specification
ValidationsGenerator.TypesParser.cs Updates source generator to check for and skip validation-skipped elements
ITypeSymbolExtensions.cs Helper methods to determine if symbols have skip validation attributes
RuntimeValidatableParameterInfoResolver.cs Runtime resolver updated to respect skip validation attributes
ValidationsGeneratorTestBase.cs Test infrastructure fix to properly await validation type verification
Multiple test snapshots Generated code snapshots showing proper validation skipping behavior

Copy link
Member

@javiercn javiercn 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 so far.

I'll take a more detailed look at the tests tomorrow

Copy link
Member

@javiercn javiercn 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 to me.

Let's have @captainsafia take a look too before we merge.

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 on overall structure and implementation!

I left some non-blocking feedback inline about test coverage and source location.

@oroztocil
Copy link
Member Author

Looks good on overall structure and implementation!

I left some non-blocking feedback inline about test coverage and source location.

Thanks, I made the suggested changes 👍

@oroztocil oroztocil enabled auto-merge (squash) August 11, 2025 11:57
@oroztocil oroztocil merged commit 9a3d379 into main Aug 11, 2025
29 checks passed
@oroztocil oroztocil deleted the oroztocil/skip-validation-attribute branch August 11, 2025 14:30
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 11, 2025
@oroztocil oroztocil added the feature-validation Issues related to model validation in minimal and controller-based APIs label Aug 11, 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 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.

Support excluding selected object properties/parameters from validation
3 participants