{"id":24554007,"url":"https://github.com/fkucukkara/fluentvalidationworkshop","last_synced_at":"2025-03-16T14:46:34.669Z","repository":{"id":269847784,"uuid":"908247438","full_name":"fkucukkara/fluentValidationWorkshop","owner":"fkucukkara","description":"This repository demonstrates the usage of Fluent Validation in both Controller-based APIs and Minimal APIs.","archived":false,"fork":false,"pushed_at":"2025-01-12T14:02:18.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-23T02:17:01.251Z","etag":null,"topics":["csharp","fluentvalidation","netcore-webapi"],"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/fkucukkara.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":"2024-12-25T15:11:21.000Z","updated_at":"2025-01-12T14:02:22.000Z","dependencies_parsed_at":"2024-12-26T16:26:15.415Z","dependency_job_id":"300b2aae-4ca7-4c9f-8bd4-d4b0db5c27fa","html_url":"https://github.com/fkucukkara/fluentValidationWorkshop","commit_stats":null,"previous_names":["fkucukkara/fluentvalidationworkshop"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2FfluentValidationWorkshop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2FfluentValidationWorkshop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2FfluentValidationWorkshop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkucukkara%2FfluentValidationWorkshop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fkucukkara","download_url":"https://codeload.github.com/fkucukkara/fluentValidationWorkshop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243885891,"owners_count":20363644,"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":["csharp","fluentvalidation","netcore-webapi"],"created_at":"2025-01-23T02:17:03.223Z","updated_at":"2025-03-16T14:46:34.642Z","avatar_url":"https://github.com/fkucukkara.png","language":"C#","readme":"# Fluent Validation in ASP.NET Core APIs\n\nThis repository demonstrates the usage of Fluent Validation in both Controller-based APIs and Minimal APIs. The examples cover core validation scenarios, including manual validation and automatic validation. Key differences between the two approaches are highlighted.\n\n## Overview\n\nFluent Validation is a popular library for defining complex validation rules for .NET objects in a fluent, expressive way. It integrates seamlessly with ASP.NET Core, allowing developers to create cleaner and more maintainable validation logic.\n\nThis repository covers:\n- How to implement Fluent Validation for Controller-based APIs.\n- How to implement Fluent Validation for Minimal APIs.\n- Auto-validation support in Controller-based APIs.\n- The use of `ValidationProblemDetails` for consistent error handling.\n\n## Validation\n\nValidation rules are defined using custom validator classes that inherit from `AbstractValidator\u003cT\u003e`. These rules can be applied manually or automatically:\n\n- **Controller-based API**: Leverages ModelState for binding validation results automatically.\n- **Minimal API**: Requires explicit invocation of the validation logic, as there is no ModelState equivalent.\n\nExample validation rule:\n```csharp\npublic class ExampleModelValidator : AbstractValidator\u003cExampleModel\u003e\n{\n    public ExampleModelValidator()\n    {\n        RuleFor(x =\u003e x.Name).NotEmpty().WithMessage(\"Name is required.\");\n        RuleFor(x =\u003e x.Age).InclusiveBetween(18, 60).WithMessage(\"Age must be between 18 and 60.\");\n    }\n}\n```\n\n## Auto Validation\n\nAutomatic validation is available in Controller-based APIs and is handled by ASP.NET Core when Fluent Validation is properly integrated. This involves:\n\n1. Adding Fluent Validation to the service collection:\n   ```csharp\n   services.AddFluentValidationAutoValidation();\n   services.AddValidatorsFromAssemblies([typeof(SampleValidator).Assembly]);\n   ```\n\n2. Errors being added to `ModelState` automatically.\n\nIn Minimal APIs, there is no `ModelState`, so auto-validation is not applicable. Instead, you need to manually call the validator:\n\n```csharp\nvar validator = new ExampleModelValidator();\nvar result = validator.Validate(exampleModel);\n\nif (!result.IsValid)\n{\n    return Results.ValidationProblem(result.ToDictionary());\n}\n```\n\n## License\n[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\nThis project is licensed under the MIT License, which allows you to freely use, modify, and distribute the code. See the [`LICENSE`](LICENSE) file for full details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffkucukkara%2Ffluentvalidationworkshop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffkucukkara%2Ffluentvalidationworkshop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffkucukkara%2Ffluentvalidationworkshop/lists"}