• author: Nick Chapsas

A Comprehensive Guide to the Brand New Data Annotation Attributes in .NET 8

With the introduction of the .NET 8, new data annotation attributes have been added to the .NET and ASP.NET Core frameworks, mainly for validation purposes. In this article, we will take a closer look at these new data annotation attributes and explore how you can use them in your applications.

Overview of Data Annotation Attributes in .NET

Data annotations can be used to enforce constraints on your data model properties or class. This results in improved data validation and helps to eliminate any errors or conflicts that may arise. Some common data annotation attributes include:

  • Required
  • Range
  • Max length
  • Min length
  • String length
  • Regular expression

New Data Annotation Attributes Added in .NET 8

1. Required Attribute: Disallow All Default Values

The first attribute we will discuss is the Required attribute, which is widely used in both .NET and ASP.NET Core for validation purposes. However, there's an issue that arises when you try to use this attribute with non-null struct types.

To fix this issue, the DisallowAllDefaultValues option has been added, which tells the Required attribute to disallow all default values. This attribute makes it clearer to understand what is required for non-nullable struct types.

2. Range Attribute: Exclusive Bounds

The Range attribute has also undergone a change with the introduction of the Exclusive Bounds option. This allows you to specify exclusive minimum and maximum values. If the entered value lies outside this range, it will not be accepted.

3. Length Attribute: From One to Three

The Length attribute is widely used for specifying constraints on string lengths. However, to define a range of string lengths, you would usually have to use both the MinLength and MaxLength attributes. In .NET 8, the Length attribute has been revamped to allow for a single attribute to define both MinLength and MaxLength. This is quite useful when dealing with input data constraints where string length is a requirement.

4. Denied and Allowed Values

This attribute is particularly useful when you need to restrict the possible values that an input can take. The AllowedValues attribute allows you to specify a list of acceptable values for a given input, while the DeniedValues attribute does the opposite.

5. Base64 Encoding

The Base64 attribute is a new addition that specifies that the input must be a valid Base64 encoding. It does not verify whether the provided input is valid binary, which is something that should be carefully considered.

Usage of Data Annotations

Now that we have covered some of the new additions in .NET 8, let's take a quick look at how to use data annotations in your projects.

publicclassUser{[Required(AllowEmptyStrings = false, ErrorMessage = "Name is required")][StringLength(100, MinimumLength = 2, ErrorMessage = "Name must be between {2} and {1} characters long")]publicstringName{get;set;}[Range(1, 130, ErrorMessage = "Age must be between {1} and {2}")]publicintAge{get;set;}[EmailAddress(ErrorMessage = "Invalid Email Address")]publicstringEmail{get;set;}[CreditCard(ErrorMessage = "Invalid Credit Card Number")]publicstringCreditCardNumber{get;set;}}

Conclusion

With the implementation of new data annotation attributes, you can ensure that the data coming into your application meets the requirements for your input data. Additionally, through the integration of these new attributes, .NET 8 intends to make coding easier and more efficient for developers.

Have you used data annotations in your projects, or do you prefer to use other validation frameworks like Fluent Validation? Do let us know your thoughts in the comments section below!

Previous Post

Retrieving Single or Multiple Resources in ASP.NET Core Web API

Next Post

Three Formerly Used .NET Best Practices and Why I Don't Use Them Anymore

About The auther

New Posts

Popular Post