Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/engineering87/sharpsanitizer
A .NET library for sanitizing and validating object properties using customizable rules to ensure clean and secure data
https://github.com/engineering87/sharpsanitizer
cleaning-data csharp dotnet sanitizer validation
Last synced: about 1 month ago
JSON representation
A .NET library for sanitizing and validating object properties using customizable rules to ensure clean and secure data
- Host: GitHub
- URL: https://github.com/engineering87/sharpsanitizer
- Owner: engineering87
- License: mit
- Created: 2021-12-29T16:34:11.000Z (about 3 years ago)
- Default Branch: develop
- Last Pushed: 2024-11-15T20:21:20.000Z (about 2 months ago)
- Last Synced: 2024-11-15T21:22:52.648Z (about 2 months ago)
- Topics: cleaning-data, csharp, dotnet, sanitizer, validation
- Language: C#
- Homepage:
- Size: 44.9 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SharpSanitizer
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Nuget](https://img.shields.io/nuget/v/SharpSanitizer?style=plastic)](https://www.nuget.org/packages/SharpSanitizer)
![NuGet Downloads](https://img.shields.io/nuget/dt/SharpSanitizer)
[![issues - SharpSanitizer](https://img.shields.io/github/issues/engineering87/SharpSanitizer)](https://github.com/engineering87/SharpSanitizer/issues)
[![Build](https://github.com/engineering87/SharpSanitizer/actions/workflows/dotnet.yml/badge.svg)](https://github.com/engineering87/SharpSanitizer/actions/workflows/dotnet.yml)
[![stars - SharpSanitizer](https://img.shields.io/github/stars/engineering87/SharpSanitizer?style=social)](https://github.com/engineering87/SharpSanitizer)SharpSanitizer is a .NET library that allows to sanitize the properties of a generic object by specifying rules and constraints for the individual properties.
The constraints and contextual sanitization rules are specified parametrically.### How it works
SharpSanitizer proceeds to evaluate each property of the object to be sanitized, checking the presence of rules and constraints specific to the individual properties. Depending on the type of data, it applies a cleaning rule to the final value.### How to use it
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:```csharp
var constraints = new Dictionary()
{
{ "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:```csharp
var sharpSanitizer = new SharpSanitizer(constraints);
```at this point it is enough to invoke the method `Sanitize` to apply the rules to the object:
```csharp
sharpSanitizer.Sanitize(fooModel);
```### Supported rules
Currently the supported rules are as follows:
```csharp
///The object property cannot be NULL
NotNull,
///The string property length cannot be greater than the constraint
Max,
///The string property cannot be NULL or greater than the constraint
MaxNotNull,
///The integer property cannot be less than the constraint
Min,
///The integer property cannot be negative
NotNegative,
///The string property must be uppercase
Uppercase,
///The string property must be lowercase
Lowercase,
///The string property cannot contain spaces
NoWhiteSpace,
///The string property cannot contain special characters
NoSpecialCharacters,
///The string property must contains only digits
OnlyDigit,
///The number property must have a limited number of decimals places
MaxDecimalPlaces,
///The object property cannot be DbNull
NoDbNull,
///The string property is a valid Datetime if parsed
ValidDatetime,
///The string property is a valid Datetime if parsed, forced to the MinValue
ForceToValidDatetime,
///The string property must be a single char
SingleChar,
///The string property represent a valid Guid
ForceValidGuid
```rules and data types will be extended in future versions.
### NuGet
The library is available on NuGet packetmanager.
https://www.nuget.org/packages/SharpSanitizer/
## 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.* [Setting up Git](https://docs.github.com/en/get-started/getting-started-with-git/set-up-git)
* [Fork the repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo)
* [Open an issue](https://github.com/engineering87/SharpSanitizer/issues) if you encounter a bug or have a suggestion for improvements/features### Licensee
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.