Elephant.DataAnnotations
2.0.0
.NET 5.0
This package targets .NET 5.0. The package is compatible with this framework or higher.
.NET Core 3.0
This package targets .NET Core 3.0. The package is compatible with this framework or higher.
dotnet add package Elephant.DataAnnotations --version 2.0.0
NuGet\Install-Package Elephant.DataAnnotations -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Elephant.DataAnnotations" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Elephant.DataAnnotations" Version="2.0.0" />
<PackageReference Include="Elephant.DataAnnotations" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Elephant.DataAnnotations --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Elephant.DataAnnotations, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Elephant.DataAnnotations@2.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Elephant.DataAnnotations&version=2.0.0
#tool nuget:?package=Elephant.DataAnnotations&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
About
Contains validation attributes commonly used in backend endpoint Controllers and view models.
Installation
Choose one:
Package Manager (Visual Studio GUI)
- Right-click your project → "Manage NuGet Packages".
- Search for
Elephant.DataAnnotations. - Click "Install".
.NET CLI (Command Line)
dotnet add package Elephant.DataAnnotations
PackageReference (Project File)
<PackageReference Include="Elephant.DataAnnotations" Version="x.x.x" />
Package Manager (CLI)
nuget install Elephant.DataAnnotations
How to Use
Examples
[IfEmptyMakeItNull] // Note that this attribute may also be placed on the property or properties themselves.
private class Customer
{
public string? Notes { get; set; } = string.Empty; // Note that this MUST be a nullable type.
..
}
..
private void Foo()
{
DataAnnotationService dataAnnotationService = new(); // Use DI if possible.
Customer customer = new();
dataAnnotationService.ReplaceEmptyStringsWithNulls(ref customer);
// customer.Notes value is now null.
}
// Example: list constraints.
public class TagRequest
{
[ListNotEmptyRequired] // list must be non-null and contain at least one element.
public List<string>? Tags { get; set; }
[ListMinRequired(2)] // Must contain at least 2 items.
[ListMax(10)] // must contain at most 10 items.
public List<int>? Ids { get; set; }
[ListOneItemRequired] // Exactly one item required (useful for single-selection fields).
public List<string>? SingleSelection { get; set; }
}
// Example: using in a controller action (MVC / minimal).
[HttpPost]
public IActionResult Create([FromBody] TagRequest request)
{
if (!ModelState.IsValid)
return BadRequest(ModelState); // Validation attributes above populate ModelState errors.
return Ok();
}
Validation attribute list
- [EqualsAnotherPropertyStringAttribute(string otherPropertyName)]
- [EqualsAnotherPropertyStringRequiredAttribute(string otherPropertyName)]
- [FilenameAllowAlphaNumericOnly(bool allowDot, bool allowUnderscore, string extraAllowedCharacters)] (compatible with string, byte, sbyte, int, long, short)
- [FilenameAllowAlphaNumericOnlyRequired(bool allowDot, bool allowUnderscore, string extraAllowedCharacters)] (compatible with string, byte, sbyte, int, long, short)
- [FileSignature(params AllowedFileExtensionType[] allowedFileExtensions)]
- [FileSignatureRequired(params AllowedFileExtensionType[] allowedFileExtensions)]
- [FileSizeMax(int fileSizeMax)]
- [FileSizeMin(int fileSizeMin)]
- [GreaterThanZero] (compatible with all standard numeric types)
- [GreaterThanZeroRequired] (compatible with all standard numeric types)
- [GuidNotEmpty(allowNull)]
- [ListMax(int maxValue)]
- [ListMin(int minValue)]
- [ListMinRequired(int minValue)]
- [ListNotEmptyRequired]
- [ListOneItemRequired]
Attribute list
- [IfEmptyMakeItNull] (To use it without writing code, use the DataAnnotationService.ReplaceEmptyStringsWithNulls(..))
Services
- IDataAnnotationService
Upgrade instructions
1.0.0 → 2.0.0
[FileSignature]now may throw anArgumentExceptioninstead of anIndexOutOfRangeException.
Contributing
Contributions are welcome. Please read our CONTRIBUTING.md file for guidelines on how to proceed.
License
This project is licensed under the MIT License. See the LICENSE.txt file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 is compatible. netcoreapp3.1 is compatible. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETCoreApp 3.0
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
-
net10.0
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
-
net5.0
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
-
net6.0
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
-
net7.0
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
-
net8.0
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
-
net9.0
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Some methods return slightly different exceptions.