ComponentModel attributes that assist in altering data for integrity #16953
Labels
api-needs-work
API needs work before it is approved, it is NOT ready for implementation
area-System.ComponentModel.DataAnnotations
Milestone
It's been asked, how data annotations can change data, and replace or trim property values. I don't feel annotations should be used in this way, but there could be benefit in attributes that would preform common and simple tasks that do alter data.
These attributes might appropriately be called mutation attributes, which could include: Trim, Replace, RegexReplace, EnsureCase, etc.
Simple Example
It's common that models will trim whitespace form string properties and change empty strings to null.
This could be the business requirement for all string properties of the user class. With mutation attributes this could be shortened drastically.
Advanced Example
Let's assume we have sign up screen that accepts a username and password, and a model that annotates
User.UserName
as accepting any alphanumeric string including underscores as valid characters. Should the end-user enterm@x_speed.01
as their desired username, validation would cause exceptions. Then the program should give an appropriate error message: "Username must contain only alphanumeric character and underscores".However if the designer would provide mutation attributes, the program could try to determine a valid value from the input. We could then provide a better message to the user: "That username is invalid. Would you like to use 'mx_speed01' instead?"
Proposed API
Implemented as Proposed
Time has been spent on an implementation as it was needed for a project. Further documentation is provided on the repo. I will continue to update the repo according to this issue.
Source: roydukkey/Dado.ComponentModel.Mutations
MyGet: http://www.myget.org/gallery/roydukkey
Updates
[DefaultValue("~/data/"), Trim, ToDefaultValue("")] string path { get; set; } = " ";
will have a different result than[DefaultValue("~/data/"), ToDefaultValue(""), Trim] string path { get; set; } = " ";
.The text was updated successfully, but these errors were encountered: