{"slug":"validation-2","title":"validation","summary":"How request validation works with FluentValidation and the ValidationBehaviour pipeline, including message wording and the shared paging rules. Use when adding or changing a validator, or when a request needs a new rule.","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-11-27T19:09:13.845878Z","repo":{"url":"https://github.com/LeanMeanCleanApps/CleanGraphQLApi","stars":13,"forks":7,"license":"MIT","updatedAt":"2026-11-25T23:23:22Z"},"bodyHtml":"<hr>\n<h2>name: validation\ndescription: How request validation works with FluentValidation and the ValidationBehaviour pipeline, including message wording and the shared paging rules. Use when adding or changing a validator, or when a request needs a new rule.\nmetadata:\nscope: shared</h2>\n<h1>Validation</h1>\n<h2>The rule</h2>\n<p>All input rules live in one place: a FluentValidation validator next to the request in Application. <code>ValidationBehaviour</code> runs every validator for a request before its handler and throws <code>FluentValidation.ValidationException</code> with all failures. Each presentation layer turns that into its own \"bad request\" shape. Nothing else validates input.</p>\n<p>Validators check the shape of a request: ids present, values in range, text the right length. Handlers check anything that needs storage, such as whether a movie exists. So validators are synchronous, take no dependencies and never call a repository.</p>\n<p>Validators are found by <code>AddValidatorsFromAssembly</code> in <code>src/Application/DependencyInjection.cs</code>, so a new one needs no registration.</p>\n<h2>Steps</h2>\n<ol>\n<li>Create <code>&lt;UseCase&gt;Validator.cs</code> beside the request: <code>public class XValidator : AbstractValidator&lt;XRequest&gt;</code>.</li>\n<li>Add one <code>RuleFor</code> per property, each discarded with <code>_ =</code> and qualified with <code>this.</code>.</li>\n<li>Write the message for a person reading an API error: what's wrong and what's allowed. End with a full stop.</li>\n<li>Paged queries use the shared extensions in <code>src/Application/Common/Paging/PagingValidatorExtensions.cs</code>: <code>ValidOffset()</code>, <code>ValidLimit()</code>, <code>ValidSearchTerm()</code>.</li>\n<li>Test every rule (<code>unit-tests</code>, <code>test-design</code>): one valid case, one failing case per rule, and both sides of every boundary in a <code>[Theory]</code>.</li>\n</ol>\n<h2>Example</h2>\n<pre><code>// src/Application/Reviews/Commands/CreateReview/CreateReviewValidator.cs\npublic class CreateReviewValidator : AbstractValidator&lt;CreateReviewCommand&gt;\n{\n    public CreateReviewValidator()\n    {\n        _ = this.RuleFor(r =&gt; r.AuthorId).NotEqual(Guid.Empty).WithMessage(\"An author id was not supplied to create the review.\");\n        _ = this.RuleFor(r =&gt; r.MovieId).NotEqual(Guid.Empty).WithMessage(\"A movie id was not supplied to create the review.\");\n        _ = this.RuleFor(r =&gt; r.Stars).InclusiveBetween(1, 5).WithMessage(\"A star rating must be between 1 and 5.\");\n    }\n}\n</code></pre>\n<pre><code>// src/Application/Movies/Queries/GetMovies/GetMoviesValidator.cs\n_ = this.RuleFor(r =&gt; r.Query).ValidSearchTerm();\n_ = this.RuleFor(r =&gt; r.Offset).ValidOffset();\n_ = this.RuleFor(r =&gt; r.Limit).ValidLimit();\n</code></pre>\n<p>A rule shared by several requests becomes an extension method on <code>IRuleBuilder&lt;T, TProperty&gt;</code> returning <code>IRuleBuilderOptions&lt;T, TProperty&gt;</code>, with its message inside, like the paging ones.</p>\n<h2>Don't</h2>\n<ul>\n<li>Use data annotations (<code>[Required]</code>, <code>[Range]</code>) on requests.</li>\n<li>Check existence in a validator (does this movie exist?). That needs storage, so it's the handler's job and throws <code>NotFoundException</code> (<code>errors-and-exceptions</code>).</li>\n<li>Inject a repository or use <code>MustAsync</code> in a validator.</li>\n<li>Throw your own <code>ValidationException</code>. Let the behaviour do it.</li>\n<li>Write messages like \"Invalid input\" or \"'Stars' must be between 1 and 5.\" (the FluentValidation default). Say it in a sentence.</li>\n</ul>\n<h2>Check</h2>\n<pre><code>dotnet test tests/Application.Tests.Unit --filter \"FullyQualifiedName~ValidatorTests\"\n</code></pre>\n","files":[{"path":"SKILL.md","sizeBytes":3284,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-11-27T19:10:39.658525Z","sha256":"F263864AB43CDF5691BA88F15614CE2B2639C07CF732A16DE029D7678D26E012","sizeBytes":1578},"review":null,"source":{"repositoryUrl":"https://github.com/LeanMeanCleanApps/CleanGraphQLApi","path":".agents/skills/validation","license":"MIT","commit":"1168fead2e0c8143a44bdce3344e0ab5fe1db90a","subtreeSha":"CD15BC7B6A027A3F0C2820B6C01B556389DD5F33F0F54865BBB12B9E25F6EEC8","lastSyncedAt":"2026-11-27T19:09:06.789278Z"},"reviewedAt":"2026-11-27T19:13:35.973649Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/LeanMeanCleanApps/CleanGraphQLApi/tree/main/.agents/skills/validation"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install leanmeancleanapps-cleangraphqlapi@llmmart"},{"target":"git","command":"git clone https://github.com/LeanMeanCleanApps/CleanGraphQLApi.git"}]}