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.
- 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.
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.
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);
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. |
The library is available on NuGet.
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.
- Setting up Git
- Fork the repository
- Open an issue if you encounter a bug or have a suggestion for improvements/features
SharpSanitizer source code is available under MIT License, see license in the source.
Please contact at francesco.delre.87[at]gmail.com for any details.