Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hookyns/validly
Powerful, efficient, and highly customizable validation library for .NET, leveraging the capabilities of C# Source Generators to provide compile-time validation logic generation.
https://github.com/hookyns/validly
c-sharp csharp data-annotations dotnet dotnet-core library netstandard nuget roslyn source-generator validation validator
Last synced: 2 months ago
JSON representation
Powerful, efficient, and highly customizable validation library for .NET, leveraging the capabilities of C# Source Generators to provide compile-time validation logic generation.
- Host: GitHub
- URL: https://github.com/hookyns/validly
- Owner: Hookyns
- License: mit
- Created: 2024-11-05T16:54:43.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-11-24T02:23:33.000Z (2 months ago)
- Last Synced: 2024-11-24T02:29:28.770Z (2 months ago)
- Topics: c-sharp, csharp, data-annotations, dotnet, dotnet-core, library, netstandard, nuget, roslyn, source-generator, validation, validator
- Language: C#
- Homepage:
- Size: 426 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Validly
This repository contains a powerful, efficient, and highly customizable validation library for .NET, leveraging the capabilities of C# Source Generators to provide compile-time validation logic generation. The library is designed to simplify model validation in .NET applications by automatically generating validation code based on attributes and custom rules, reducing runtime overhead and enhancing code maintainability.
## Key Features
- Compile-Time Code Generation: Uses C# Source Generators to create validation code at compile-time, eliminating the need for runtime reflection and improving performance.
- Attribute-Based Validation: Define validation rules using simple attributes directly on your models, allowing clear and maintainable validation rules.
- Property and Object-Level Validation: Supports validation of individual properties as well as the model as a whole, enabling both fine-grained and aggregate validations.
- Customizable Validation Logic: Extend the library with custom validation attributes and rules to meet unique validation requirements.
- Detailed Validation Results: Provides rich validation results, including per-property error messages and model-wide validation summaries.
- Seamless Integration: Designed to work smoothly in .NET applications, with support for dependency injection and easy configuration.## Getting Started
To start using the library, add it to your project as a NuGet package. Decorate your model properties with validation attributes and let the source generator handle the rest. Generated code will include optimized validation methods that you can call to validate instances of your models.## Example Usage
Define validation rules using attributes on your model properties:```csharp
[Validatable]
public partial class CreateUserRequest
{
[Required]
[MinLength(3)]
[MaxLength(100)]
public string Name { get; set; }[Required]
[EmailAddress]
public string Email { get; set; }[Range(18, 99)]
public int Age { get; set; }
}
```Validate the model in your application code:
```csharp
app.MapPost("/users", async (CreateUserRequest request) =>
{
var validationResult = request.Validate(); // This method is generated by the source generatorif (!validationResult.Valid)
{
return Results.BadRequest(validationResult);
}// create user...
return Results.Created($"/users/{user.Id}");
});
```## Installation
Install the package via NuGet:
```bash
dotnet add package Validly
```Install the source generator package to enable compile-time validation code generation:
```bash
dotnet add package Validly.SourceGenerator
```Install the package with default validators:
```bash
dotnet add package Validly.Validators
```## Contributions
Contributions are welcome! Feel free to open issues, submit pull requests, or suggest enhancements to improve the library further.## License
This project is licensed under the MIT License.---
This .NET validation library aims to streamline validation in .NET applications, leveraging modern C# features for performance and ease of use. It’s ideal for developers looking to minimize boilerplate code and maximize performance in their validation routines.