{"id":43208904,"url":"https://github.com/goodtocode/aspect-validation","last_synced_at":"2026-02-01T07:13:06.251Z","repository":{"id":333960079,"uuid":"1139431589","full_name":"goodtocode/aspect-validation","owner":"goodtocode","description":"Lightweight, dependency-free validation library for .NET, designed for use in CQRS, Clean Architecture, and modern .NET applications. It provides a fluent API for defining validation rules for commands, queries, DTOs, and more.","archived":false,"fork":false,"pushed_at":"2026-01-22T06:58:21.000Z","size":45,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-01-22T15:06:31.205Z","etag":null,"topics":[],"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/goodtocode.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-01-22T00:25:22.000Z","updated_at":"2026-01-22T06:58:21.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/goodtocode/aspect-validation","commit_stats":null,"previous_names":["goodtocode/aspect-validation"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/goodtocode/aspect-validation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goodtocode%2Faspect-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goodtocode%2Faspect-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goodtocode%2Faspect-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goodtocode%2Faspect-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goodtocode","download_url":"https://codeload.github.com/goodtocode/aspect-validation/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goodtocode%2Faspect-validation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28971713,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-01T06:46:42.625Z","status":"ssl_error","status_checked_at":"2026-02-01T06:44:56.173Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2026-02-01T07:13:05.498Z","updated_at":"2026-02-01T07:13:06.242Z","avatar_url":"https://github.com/goodtocode.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Goodtocode.Validation\n\n[![NuGet CI/CD](https://github.com/goodtocode/aspect-validation/actions/workflows/gtc-validation-nuget.yml/badge.svg)](https://github.com/goodtocode/aspect-validation/actions/workflows/gtc-validation-nuget.yml)\n\nGoodtocode.Validation is a lightweight, dependency-free validation library for .NET, designed for use in CQRS, Clean Architecture, and modern .NET applications. It provides a fluent API for defining validation rules for commands, queries, DTOs, and more.\n\n## Features\n- Fluent, expressive rule definitions\n- Supports conditional and cross-property validation\n- Throws custom exceptions for invalid data\n- No external dependencies\n\n## Core Concepts\n- **Validator\u003cT\u003e**: Base class for defining validation rules for a type.\n- **RuleBuilder**: Fluent builder for chaining rules on properties.\n- **ValidationResult**: Contains validation errors and validity state.\n- **CustomValidationException**: Thrown when validation fails (optional).\n\n## Example: Paginated Query Validator (Clean Architecture Use Case)\n```csharp\npublic class GetEntitiesQueryValidator : Validator\u003cGetEntitiesQuery\u003e\n{\n    public GetEntitiesQueryValidator()\n    {\n        RuleFor(v =\u003e v.StartDate).NotEmpty()\n            .When(v =\u003e v.EndDate != null)\n            .LessThanOrEqualTo(v =\u003e v.EndDate);\n\n        RuleFor(v =\u003e v.EndDate)\n            .NotEmpty()\n            .When(v =\u003e v.StartDate != null)\n            .GreaterThanOrEqualTo(v =\u003e v.StartDate);\n\n        RuleFor(x =\u003e x.PageNumber).NotEqual(0);\n        RuleFor(x =\u003e x.PageSize).NotEqual(0);\n    }\n}\n```\n\n## How to Use\n1. Create a validator by inheriting from `Validator\u003cT\u003e`.\n2. Use `RuleFor(x =\u003e x.Property)` to define rules with fluent methods like `.NotEmpty()`, `.NotEqual()`, `.LessThanOrEqualTo()`, `.GreaterThanOrEqualTo()`, `.When()`, etc.\n3. Call `ValidateAndThrow(instance)` or `Validate(instance)` to check validity.\n\n\n## RuleBuilder, Validation, and Exception Handling\n\n### RuleBuilder\nThe `RuleBuilder` class provides a fluent API for defining validation rules on your object's properties. You can chain methods such as `.NotEmpty()`, `.NotEqual()`, `.LessThanOrEqualTo()`, `.GreaterThanOrEqualTo()`, and `.When()` to express complex validation logic in a readable way. Each rule is registered with the validator and will be checked when validation is performed.\n\n### Validate\nThe `Validate` method (and its async variant) executes all defined rules for a given object instance. It returns a `ValidationResult` containing a list of `ValidationFailure` errors and an `IsValid` flag. If you use `ValidateAndThrow`, a `CustomValidationException` is thrown if any rule fails.\n\n### Handling Validation in WebApi\nWhen using `ValidateAndThrow`, any validation failures will result in a `CustomValidationException` being thrown. In ASP.NET Core WebApi projects, it is recommended to catch this exception in your global exception handling middleware. You can then translate the exception into a `400 Bad Request` response, returning the validation errors to the client in a structured format. This ensures that clients receive clear feedback on why their request was invalid, and keeps your controller actions clean and focused on business logic.\n\n**Example: Handling Validation Exceptions in Middleware**\n```csharp\napp.Use(async (context, next) =\u003e\n{\n    try\n    {\n        await next();\n    }\n    catch (CustomValidationException ex)\n    {\n        context.Response.StatusCode = StatusCodes.Status400BadRequest;\n        context.Response.ContentType = \"application/json\";\n        var result = JsonSerializer.Serialize(new { errors = ex.Errors });\n        await context.Response.WriteAsync(result);\n    }\n});\n```\n\n## Installation\nInstall via NuGet:\n\n```\ndotnet add package Goodtocode.Validation\n```\n\n## License\nMIT\n\n## Contact\n- [GitHub Repo](https://github.com/goodtocode/aspect-validation)\n- [@goodtocode](https://twitter.com/goodtocode)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoodtocode%2Faspect-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoodtocode%2Faspect-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoodtocode%2Faspect-validation/lists"}