{"id":21670500,"url":"https://github.com/engineering87/sharpsanitizer","last_synced_at":"2026-02-18T18:01:45.188Z","repository":{"id":45232248,"uuid":"442830324","full_name":"engineering87/SharpSanitizer","owner":"engineering87","description":"A .NET library for sanitizing and validating object properties using customizable rules to ensure clean and secure data","archived":false,"fork":false,"pushed_at":"2025-03-20T12:43:45.000Z","size":51,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-03-20T14:05:20.205Z","etag":null,"topics":["cleaning-data","csharp","dotnet","sanitizer","validation"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/engineering87.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-29T16:34:11.000Z","updated_at":"2025-03-20T12:43:49.000Z","dependencies_parsed_at":"2025-01-30T00:28:40.177Z","dependency_job_id":null,"html_url":"https://github.com/engineering87/SharpSanitizer","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/engineering87%2FSharpSanitizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/engineering87%2FSharpSanitizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/engineering87%2FSharpSanitizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/engineering87%2FSharpSanitizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/engineering87","download_url":"https://codeload.github.com/engineering87/SharpSanitizer/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248507468,"owners_count":21115607,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cleaning-data","csharp","dotnet","sanitizer","validation"],"created_at":"2024-11-25T12:32:39.495Z","updated_at":"2026-02-18T18:01:45.170Z","avatar_url":"https://github.com/engineering87.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SharpSanitizer\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Nuget](https://img.shields.io/nuget/v/SharpSanitizer?style=plastic)](https://www.nuget.org/packages/SharpSanitizer)\n![NuGet Downloads](https://img.shields.io/nuget/dt/SharpSanitizer)\n[![issues - SharpSanitizer](https://img.shields.io/github/issues/engineering87/SharpSanitizer)](https://github.com/engineering87/SharpSanitizer/issues)\n[![Build](https://github.com/engineering87/SharpSanitizer/actions/workflows/dotnet.yml/badge.svg)](https://github.com/engineering87/SharpSanitizer/actions/workflows/dotnet.yml)\n[![stars - SharpSanitizer](https://img.shields.io/github/stars/engineering87/SharpSanitizer?style=social)](https://github.com/engineering87/SharpSanitizer)\n\n**SharpSanitizer** is a lightweight and flexible .NET library designed to sanitize and validate object properties through a configurable set of rules and constraints.\nIt helps ensure data consistency, safety, and quality before persisting or processing objects.\n\n## Features\n- Strongly-typed, generic sanitizer with minimal boilerplate.\n- Built-in constraints for strings, numbers, collections, and objects.\n- Configurable via `Dictionary\u003cstring, Constraint\u003e` for maximum flexibility.\n- Works with both strict and relaxed validation modes.\n- Support for advanced numeric rounding and collection-level checks.\n\n## How it works\nSharpSanitizer inspects the properties of an object and applies the configured constraints for each property.\nThe sanitization process modifies values in place based on the rules you define.\n\n## Usage Example\nTo 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:\n\n```csharp\nvar constraints = new Dictionary\u003cstring, Constraint\u003e()\n{\n    { \"StringMaxNotNull\", new Constraint(ConstraintType.MaxNotNull, 10) },\n    { \"StringMax\", new Constraint(ConstraintType.Max, 10) },\n    { \"StringNoWhiteSpace\", new Constraint(ConstraintType.NoWhiteSpace) },\n    { \"StringNoSpecialCharacters\", new Constraint(ConstraintType.NoSpecialCharacters) }\n};\n```\nas a second step create an instance of the SharpSanitizer specifying the type of object to work on:\n\n```csharp\nvar sharpSanitizer = new SharpSanitizer\u003cFooModel\u003e(constraints);\n```\n\nat this point it is enough to invoke the method `Sanitize` to apply the rules to the object:\n\n```csharp\nsharpSanitizer.Sanitize(fooModel);\n```\n\n### Supported Constraints\n\nSharpSanitizer now supports a rich set of rules for validation and sanitization:\n\n| Constraint              | Description                                                                 |\n|--------------------------|-----------------------------------------------------------------------------|\n| `NotNull`               | Ensures object is not null; initializes if needed.                         |\n| `NoDbNull`              | Converts `DBNull.Value` to `null`.                                         |\n| `NotNullOrEmpty`        | Ensures string is not null or empty.                                       |\n| `NotNullOrWhiteSpace`   | Ensures string is not null or whitespace.                                  |\n| `MaxLength`             | Truncates string to a maximum length.                                      |\n| `MinLength`             | Pads string if shorter than required.                                      |\n| `MaxNotNull`            | Non-null string limited to a maximum length.                              |\n| `Uppercase`             | Converts string to uppercase.                                              |\n| `Lowercase`             | Converts string to lowercase.                                              |\n| `NoWhiteSpace`          | Removes all whitespace.                                                    |\n| `NoSpecialCharacters`   | Removes all non-alphanumeric characters except `_` and `.`.                |\n| `OnlyDigit`             | Keeps only digits in the string.                                           |\n| `ValidDatetime`         | Keeps valid date strings, clears invalid.                                  |\n| `ForceToValidDatetime`  | Forces invalid dates to `DateTime.MinValue`.                               |\n| `SingleChar`            | Enforces single character.                                                 |\n| `ValidGuid`             | Enforces a valid GUID (generates new if invalid in relaxed mode).          |\n| `ValidEmail`            | Enforces a valid email format.                                             |\n| `MaxValue`              | Numeric: clamps to max value.                                              |\n| `MinValue`              | Numeric: clamps to min value.                                              |\n| `NotNegative`           | Numeric: clamps negatives to zero.                                         |\n| `Positive`              | Enforces value ≥ 0.                                                        |\n| `StrictPositive`        | Enforces value \u003e 0.                                                        |\n| `NonZero`               | Disallows zero, adjusts based on severity mode.                            |\n| `MaxDecimalPlaces`      | Truncates decimals to a maximum number of digits.                          |\n| `RoundTo`               | Rounds decimals to a specified number of digits.                           |\n| `NotEmptyCollection`    | Collection: must not be empty.                                             |\n| `DistinctCollection`    | Collection: removes duplicates.  \n\n## NuGet\n\nThe library is available on [NuGet](https://www.nuget.org/packages/SharpSanitizer/).\n\n## Contributing\n\nThank you for considering to help out with the source code!\nIf 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.\n\n * [Setting up Git](https://docs.github.com/en/get-started/getting-started-with-git/set-up-git)\n * [Fork the repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo)\n * [Open an issue](https://github.com/engineering87/SharpSanitizer/issues) if you encounter a bug or have a suggestion for improvements/features\n\n## License\nSharpSanitizer source code is available under MIT License, see license in the source.\n\n## Contact\nPlease contact at francesco.delre.87[at]gmail.com for any details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengineering87%2Fsharpsanitizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fengineering87%2Fsharpsanitizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengineering87%2Fsharpsanitizer/lists"}