SharpSanitizer 3.0.0

dotnet add package SharpSanitizer --version 3.0.0
                    
NuGet\Install-Package SharpSanitizer -Version 3.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="SharpSanitizer" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharpSanitizer" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SharpSanitizer" />
                    
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 SharpSanitizer --version 3.0.0
                    
#r "nuget: SharpSanitizer, 3.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 SharpSanitizer@3.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=SharpSanitizer&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SharpSanitizer&version=3.0.0
                    
Install as a Cake Tool

SharpSanitizer

License: MIT Nuget NuGet Downloads issues - SharpSanitizer Build stars - SharpSanitizer

SharpSanitizer is a lightweight and flexible .NET library designed to sanitize and validate object properties through a configurable set of rules and constraints. It helps ensure data consistency, safety, and quality before persisting or processing objects.

Features

  • Strongly-typed, generic sanitizer with minimal boilerplate.
  • Built-in constraints for strings, numbers, collections, and objects.
  • Configurable via Dictionary<string, Constraint> for maximum flexibility.
  • Works with both strict and relaxed validation modes.
  • Support for advanced numeric rounding and collection-level checks.

How it works

SharpSanitizer inspects the properties of an object and applies the configured constraints for each property. The sanitization process modifies values in place based on the rules you define.

Usage Example

To use the SharpSanitizer library, first populate the set of rules to be applied to the object properties. Rules are expressed through a Dictionary in which the key matches the name of the property and the value is the constraint to be applied, for example:

var constraints = new Dictionary<string, Constraint>()
{
    { "StringMaxNotNull", new Constraint(ConstraintType.MaxNotNull, 10) },
    { "StringMax", new Constraint(ConstraintType.Max, 10) },
    { "StringNoWhiteSpace", new Constraint(ConstraintType.NoWhiteSpace) },
    { "StringNoSpecialCharacters", new Constraint(ConstraintType.NoSpecialCharacters) }
};

as a second step create an instance of the SharpSanitizer specifying the type of object to work on:

var sharpSanitizer = new SharpSanitizer<FooModel>(constraints);

at this point it is enough to invoke the method Sanitize to apply the rules to the object:

sharpSanitizer.Sanitize(fooModel);

Supported Constraints

SharpSanitizer now supports a rich set of rules for validation and sanitization:

Constraint Description
NotNull Ensures object is not null; initializes if needed.
NoDbNull Converts DBNull.Value to null.
NotNullOrEmpty Ensures string is not null or empty.
NotNullOrWhiteSpace Ensures string is not null or whitespace.
MaxLength Truncates string to a maximum length.
MinLength Pads string if shorter than required.
MaxNotNull Non-null string limited to a maximum length.
Uppercase Converts string to uppercase.
Lowercase Converts string to lowercase.
NoWhiteSpace Removes all whitespace.
NoSpecialCharacters Removes all non-alphanumeric characters except _ and ..
OnlyDigit Keeps only digits in the string.
ValidDatetime Keeps valid date strings, clears invalid.
ForceToValidDatetime Forces invalid dates to DateTime.MinValue.
SingleChar Enforces single character.
ValidGuid Enforces a valid GUID (generates new if invalid in relaxed mode).
ValidEmail Enforces a valid email format.
MaxValue Numeric: clamps to max value.
MinValue Numeric: clamps to min value.
NotNegative Numeric: clamps negatives to zero.
Positive Enforces value ≥ 0.
StrictPositive Enforces value > 0.
NonZero Disallows zero, adjusts based on severity mode.
MaxDecimalPlaces Truncates decimals to a maximum number of digits.
RoundTo Rounds decimals to a specified number of digits.
NotEmptyCollection Collection: must not be empty.
DistinctCollection Collection: removes duplicates.

NuGet

The library is available on NuGet.

Contributing

Thank you for considering to help out with the source code! If you'd like to contribute, please fork, fix, commit and send a pull request for the maintainers to review and merge into the main code base.

License

SharpSanitizer source code is available under MIT License, see license in the source.

Contact

Please contact at francesco.delre.87[at]gmail.com for any details.

Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

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
3.0.0 152 8/25/2025
2.2.0 375 8/23/2024
2.1.0 191 1/29/2024
2.0.0 344 1/15/2023
1.0.0 313 1/15/2022