Elephant.DataAnnotations 2.0.0

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" />
                    
Directory.Packages.props
<PackageReference Include="Elephant.DataAnnotations" />
                    
Project file
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
                    
#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
                    
Install as a Cake Addin
#tool nuget:?package=Elephant.DataAnnotations&version=2.0.0
                    
Install as a Cake Tool

Nuget downloads NuGet Downloads Workflow License: MIT

About

Contains validation attributes commonly used in backend endpoint Controllers and view models.

Installation

Choose one:

Package Manager (Visual Studio GUI)

  1. Right-click your project → "Manage NuGet Packages".
  2. Search for Elephant.DataAnnotations.
  3. 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 an ArgumentException instead of an IndexOutOfRangeException .

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 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 141 12/31/2025
1.0.14 429 11/19/2025
1.0.13 235 3/28/2025
1.0.12 325 11/15/2023
1.0.11 208 10/14/2023
1.0.10 256 7/11/2023
1.0.9 272 5/31/2023
1.0.5 6,348 2/11/2023
1.0.4 369 1/30/2023
1.0.2 419 1/15/2023
1.0.1 396 1/9/2023

Some methods return slightly different exceptions.