Runtime: Feature request: Proper validation in .NET!

Created on 30 Jul 2020  路  5Comments  路  Source: dotnet/runtime

Background and Motivation

Validation is one of the most critical building blocks in any application, server or client. Yet the .NET lacks an official and adequate solution to this matter.

DataAnnotations validation have been integrated with the .NET for a long time.
With the advancement of .NET it's been discovered that although decorating properties with inline attributes is a neat way to define validation, it comes with many limitations by the compiler (i.e. attributes may only take in constants, no generics etc.), as well as other downsides. DA has over time become completely abandoned and from what I read (will update when I find source), is no longer planned to be updated or be taken to the next level.

Additionally, UI frameworks (especially UWP and WinUI), don't have a conventional way of rendering the validation errors and exchanging validation data across app layers.

Proposed solution

My suggestion hereby is for Microsoft and the .NET to thoroughly rethink the subject of validation and introduce (or officially adopt an existing) a fully integrated validation framework open for configuration, customization and extensibility, engineered from scratch to cover or enable extensibility of as many scenarios as possible, for instance (some less or not important as others - no specific order):

  • Highly extensible and configurable
  • Validation groups / rulesets (nested? inherited?)
  • Dependency injection
  • Open for mocking & unit testing
  • Entire graph validation
  • Scalar value inline validation
  • Static validation / argument validation Check.IsNotNull style
  • Property and group human names and descriptions
  • Error message and field it group name localization
  • Inter-property or inter-entity validation
  • Short circuiting on first error in group / entity in list / entity in graph / combination etc.
  • Reuse validation in client & server entities, for example if client entities are generated by a tool (e.g. ODataConnectedService)
  • Validation process interception or customization
  • Client side error tracking and notification (ViewModel)
  • UI validation error rendering
  • Respect INotifyDataErrorInfo, or have adapters to it
  • Ability to bridge with Entity Framework entity configuration (nullable columns, column length, precision etc.)
  • Ability to bridge with OData EDM model builder

Microsoft.Extensions sounds to me like the perfect family validation would fit in. It would benefit from integration with its siblings:

  • DependencyInjection
  • Options
  • Configuration
  • Logging
  • Localization (.NET 5)

It could come in two blocks:

  • Microsoft.Extensions.Validation.Abstractions (primitives and contracts)
  • Microsoft.Extensions.Validation

Future / 2nd blcok:

  • Microsoft.Extensions.Validation.AspNetCore (controller/API-controller middleware and Razor/UI integration)
  • Mcirosoft.Extensions.Validation.WinUI (WinUI UI integration)
  • Microsoft.Extensions.Validation.Wpf (WPF UI integration)
Discussion api-suggestion area-Meta

Most helpful comment

@weitzhandler I've had discussions on this with several people in the last few months. It's something we're thinking about, but we don't have any concrete plans yet.

/cc @jeffhandley @DamianEdwards

All 5 comments

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ericstj @maryamariyan - thoughts?

It sounds like an idea for a brand new library / suite of libraries. Typically we don't start those out here in dotnet/runtime unless there is a super compelling reason they need to be in this layer (like intrinsics). @weitzhandler why not start out a project independent of of dotnet/runtime and see how it goes. If you can make a compelling case for integration it can be something to consider. Also cc @davidfowl (extensions architect) and @ajcvickers (annotations and EF owner) for opinions.

@weitzhandler I've had discussions on this with several people in the last few months. It's something we're thinking about, but we don't have any concrete plans yet.

/cc @jeffhandley @DamianEdwards

Typically we don't start those out here in dotnet/runtime unless there is a super compelling reason they need to be in this layer

Personally I think validation is one of the most important building blocks in any application. As of today, the .NET lacks an adequate validation engine that covers today's validation requirements.

It's something we're thinking about, but we don't have any concrete plans yet.

Thanks for your input!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

v0l picture v0l  路  3Comments

EgorBo picture EgorBo  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

Timovzl picture Timovzl  路  3Comments

yahorsi picture yahorsi  路  3Comments