Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ The conversion is done: no project references `Rocket.Surgery.Conventions` or th
- A validator holds only the rules declared for its type. Each has a public rule method for every member with rules (`Ensure{Member}`, `Ensure_Value` for the whole value; static unless it takes a service), a `ValidateDeclared` that runs them, and is an `IValidateMembers<T>` (one member at a time, which a form field needs). These methods and their `[GeneratedRule]` are the **rule manifest** the generator reads from referenced assemblies (`Model/LinkReader.cs`); `Model/Resolution.cs` decides, once every model of the compilation is in, which other validators each one runs.
- The rules of a base class or an interface stay in that type's validator, and the validator of every type that derives from it calls its `ValidateDeclared`; a type with no rules of its own still gets a validator when something it derives from has one. `[InheritFrom]` and `Include<TOther>()` carry rules member by member (LPAD3112 where a member's rules can't be carried), and the validator of a `[Mutable<T>]` view model validates the whole value as `T` and one member with `T`'s rule methods. Which members `[InheritFrom]` and `[Mutable]` write is decided in `src/Shared/GeneratedMembers.cs`, linked into both analyzer assemblies: the validation generator can't see what those generators write.
- A `Validate` function may declare rules for a type of another assembly. Its validator (`{Type}In{Assembly}GeneratedValidator`) runs the validators the referenced assemblies have for the type and then its own rules, and the registration replaces what is registered for the type with it; `[assembly: ContributedValidator]` tells the assemblies that reference this one. LPAD3113 is an error on an asynchronous rule added to a type its own assembly validates synchronously.
- Not written yet: validators for generic types (LPAD3101), and the integrations ([#3379](https://github.com/RocketSurgeonsGuild/LaunchPad/issues/3379)). `[Mutable]`'s generator still writes its ZeroAlloc.Validation validator until that library is removed ([#3380](https://github.com/RocketSurgeonsGuild/LaunchPad/issues/3380)).
- `test/Validation.Tests` runs the generator as an application does and proves what the validators do (`test/Validation.Tests.Contracts` is the library it reads rules from across an assembly boundary, and `SdkValidationTests` in `test/Sdk.Tests` does the same from packed packages); `test/Validation.Analyzers.Tests` has the snapshots of what the generator writes and reports.
- What a transport needs is in `src/Validation` too: `ValidationFailedException` (a result with errors, which `ValidationResult.ThrowIfInvalid()` throws), `ValidationNotices` (the warnings and information of the request being handled, which a transport begins and sends with the response), `ValidationJson` (failures as JSON, written by hand), `ValidationManifest` (each validator's rules as data, which the generator writes as its `Manifest` and the registration adds) and `ObjectValidator` (validation by a type known only at run time, registered for every validated type, so a form component uses no reflection). In the manifest `each` before a rule says it is about the items of a collection and `when` that it does not always reject the value.
- The integrations are packages of their own ([#3379](https://github.com/RocketSurgeonsGuild/LaunchPad/issues/3379)), each a joint that runs after its host package's and whose types live in that package's namespaces: `src/Validation.AspNetCore` (422 `ValidationFailedProblemDetails`, the `Validation-Notices` response header, and `ValidationOpenApiSchemaTransformer`, which states the rules of the manifests), `src/Validation.Grpc` (`InvalidArgument`, with the failures in the `validation-failures-bin` trailer and the notices in `validation-notices-bin`), `src/Validation.HotChocolate` (one `VALIDATION` error for each failure, and the notices as the `validationNotices` extension) and `src/Validation.Blazor` (`LaunchPadValidator`, on .NET 11's asynchronous form validation). Each turns a `ValidationFailedException` into its transport's error shape; none validates by itself. `src/Analyzers/Api/Core/SchemaRules.cs` reads LaunchPad's rule attributes as it reads ZeroAlloc.Validation's.
- Not written yet: validators for generic types (LPAD3101), and validation in the mediator pipeline, which waits for the mediator switch to land ([#3379](https://github.com/RocketSurgeonsGuild/LaunchPad/issues/3379)): until then nothing throws `ValidationFailedException` but an application's own code. The `src/ZeroAlloc.*` packages, the SDK's detection of them and `[Mutable]`'s ZeroAlloc.Validation validator stay until that library is removed ([#3380](https://github.com/RocketSurgeonsGuild/LaunchPad/issues/3380)).
- `test/Validation.Tests` runs the generator as an application does and proves what the validators do (`test/Validation.Tests.Contracts` is the library it reads rules from across an assembly boundary, and `SdkValidationTests` in `test/Sdk.Tests` does the same from packed packages); `test/Validation.Analyzers.Tests` has the snapshots of what the generator writes and reports; `test/Validation.{AspNetCore,Grpc,HotChocolate,Blazor}.Tests` prove the integrations.
- `src/Sdks/LaunchPad` is the **LaunchPad SDK** (`Rocket.Surgery.LaunchPad.Sdk`), an MSBuild SDK an application applies with an `<Sdk>` element in each project (`#:sdk` in a file-based app) or once in its `Directory.Build.props`. It chains `Joinery.Dovetail.Sdk` (the version `Directory.Packages.props` pins for it) and adds LaunchPad packages from what Dovetail's host detection finds and from the project's direct `PackageReference`s; `LaunchPadEnable<Tech>=false` opts out. Every LaunchPad SDK also chains the **Chirograph** SDK (OpenAPI clients from `OpenApiClient` items) through `src/Sdks/LaunchPad/Sdk/Rocket.Surgery.LaunchPad.Chirograph.targets`, after the project body, unless the project applies Chirograph itself or sets `LaunchPadEnableChirograph=false`; the repository's own test projects apply `<Sdk Name="Chirograph" />` directly, at the version `global.json` pins, which must equal the `Chirograph` `PackageVersion`. The versions are written into `Sdk/Packages.props` when the SDK is packed. `src/Sdks/Console` is the **console SDK** (`Rocket.Surgery.LaunchPad.Sdk.Console`): the same SDK for ConsoleAppFramework applications, chaining `Joinery.Dovetail.Sdk.Console`. `src/Sdks/{Web,Worker,Razor,BlazorWebAssembly,WebAssembly,WindowsDesktop}` are the **mirrors** (`Rocket.Surgery.LaunchPad.Sdk.Web`, ...): each is the Microsoft SDK of the same name with LaunchPad, chains Dovetail's mirror (`Joinery.Dovetail.Sdk.Web`, ...) and then applies `src/Sdks/Mirror/Rocket.Surgery.LaunchPad.Mirror.props`, which skips LaunchPad when a `Directory.Build.props` already applied it. Every SDK but `Rocket.Surgery.LaunchPad.Sdk` packs the shared files of `src/Sdks/LaunchPad/Sdk` beside its own `Sdk.props`/`Sdk.targets`; `src/Sdks/Directory.Build.props`/`.targets` hold the package settings and generate each `Sdk/Packages.props` from `LaunchPadChainedSdk`. `test/Sdk.Tests` packs it and builds throwaway applications against it, and `sample/Sample.Sdk` consumes it from packed packages (`dotnet msbuild build/pack.proj` there first). Like an application outside the repository, that sample and the throwaway projects of the tests stand outside central package management and name their own versions.
- An operation that can fail responds with a **union response** instead of throwing ([`docs/design/union-types.md`](docs/design/union-types.md), [ADR-0005](docs/adr/0005-portable-unions-shaped-like-csharp-unions.md), [ADR-0010](docs/adr/0010-net11-only-and-native-unions.md), [ADR-0006](docs/adr/0006-union-responses-over-every-transport.md)); the generated servers answer each error case in their transport's error shape. Unions are native C# 15 unions (`public union Result(RocketModel, NotFound);`), or hand-written custom unions; LaunchPad has no union generator and no polyfill ([ADR-0010](docs/adr/0010-net11-only-and-native-unions.md)). The standard error cases (`Problem`, `NotFound` and the rest) are in Foundation. Generators detect a union by name (`UnionAttribute` or `IUnion`), take its cases from its public one-parameter constructors (implicitly declared ones included) and read it through `Value`.
- Every test project runs on TUnit with Microsoft.Testing.Platform, as moved by tickets [#3050](https://github.com/RocketSurgeonsGuild/LaunchPad/issues/3050) and [#3051](https://github.com/RocketSurgeonsGuild/LaunchPad/issues/3051); `test/Directory.Build.targets` adds the TUnit, Verify.TUnit, TUnit.Mocks and TUnit.Assertions.Should packages to each of them, and there is no xunit, FakeItEasy or Shouldly anywhere. Tests assert with the Should syntax (`await x.Should().BeEqualTo(y)`), falling back to `Assert.That` only where it has no equivalent, and mock with TUnit.Mocks. A test that resolves services from a populated service collection derives from `ContainerTest<TestRecord>` (`test/Shared/ContainerTest.cs`, linked into the projects that use it). The same file gives every test project `Joinery.Dovetail` and Specular, so a test gets the joints and the Specular scan results of the projects it references. Shared test code (the web app fixtures and fake time) lives in the non-packable `test/TestSupport` project. A test creates scopes directly with `CreateScope()` / `CreateAsyncScope()`.
Expand Down
8 changes: 8 additions & 0 deletions LaunchPad.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
<Project Path="src/Primitives/Rocket.Surgery.LaunchPad.Primitives.csproj" />
<Project Path="src/Spatial/Rocket.Surgery.LaunchPad.Spatial.csproj" />
<Project Path="src/Validation.Analyzers/Rocket.Surgery.LaunchPad.Validation.Analyzers.csproj" />
<Project Path="src/Validation.AspNetCore/Rocket.Surgery.LaunchPad.Validation.AspNetCore.csproj" />
<Project Path="src/Validation.Blazor/Rocket.Surgery.LaunchPad.Validation.Blazor.csproj" />
<Project Path="src/Validation.Grpc/Rocket.Surgery.LaunchPad.Validation.Grpc.csproj" />
<Project Path="src/Validation.HotChocolate/Rocket.Surgery.LaunchPad.Validation.HotChocolate.csproj" />
<Project Path="src/Validation/Rocket.Surgery.LaunchPad.Validation.csproj" />
<Project Path="src/StrawberryShake.Spatial/Rocket.Surgery.LaunchPad.StrawberryShake.Spatial.csproj" />
<Project Path="src/StrawberryShake/Rocket.Surgery.LaunchPad.StrawberryShake.csproj" />
Expand Down Expand Up @@ -144,6 +148,10 @@
<Project Path="test/Serilog.AspNetCore.Tests/Serilog.AspNetCore.Tests.csproj" />
<Project Path="test/Spatial.Serilog.Tests/Spatial.Serilog.Tests.csproj" />
<Project Path="test/Validation.Analyzers.Tests/Validation.Analyzers.Tests.csproj" />
<Project Path="test/Validation.AspNetCore.Tests/Validation.AspNetCore.Tests.csproj" />
<Project Path="test/Validation.Blazor.Tests/Validation.Blazor.Tests.csproj" />
<Project Path="test/Validation.Grpc.Tests/Validation.Grpc.Tests.csproj" />
<Project Path="test/Validation.HotChocolate.Tests/Validation.HotChocolate.Tests.csproj" />
<Project Path="test/Validation.Tests.Contracts/Validation.Tests.Contracts.csproj" />
<Project Path="test/Validation.Tests/Validation.Tests.csproj" />
<Project Path="test/TestSupport/TestSupport.csproj" />
Expand Down
12 changes: 8 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ lists what the SDK adds for each technology.

### Validation

| Package | NuGet | What it does |
| ----------------------------------- | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Rocket.Surgery.LaunchPad.Validation | not published yet | LaunchPad's own validation, in progress ([#3374](https://github.com/RocketSurgeonsGuild/LaunchPad/issues/3374)): rule attributes and `System.ComponentModel.DataAnnotations` compiled into one generated validator per type |
| Rocket.Surgery.LaunchPad.Validation.Analyzers | not published yet | The validator generator, analyzers and code fixes Validation depends on |
| Package | NuGet | What it does |
| ------------------------------------------------ | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Rocket.Surgery.LaunchPad.Validation | not published yet | LaunchPad's own validation, in progress ([#3374](https://github.com/RocketSurgeonsGuild/LaunchPad/issues/3374)): rule attributes, `System.ComponentModel.DataAnnotations` and `Validate` functions compiled into one generated validator per type |
| Rocket.Surgery.LaunchPad.Validation.Analyzers | not published yet | The validator generator, analyzers and code fixes Validation depends on |
| Rocket.Surgery.LaunchPad.Validation.AspNetCore | not published yet | Validation failures as 422 problem details with codes and placeholder values, warnings in a response header, and the rules in OpenAPI |
| Rocket.Surgery.LaunchPad.Validation.Grpc | not published yet | Validation failures as `InvalidArgument` with the failures in a trailer, and warnings in a trailer of successful calls |
| Rocket.Surgery.LaunchPad.Validation.HotChocolate | not published yet | Validation failures as GraphQL `VALIDATION` errors, and warnings as an extension of the result |
| Rocket.Surgery.LaunchPad.Validation.Blazor | not published yet | A `LaunchPadValidator` component: a form's fields validated one at a time, asynchronously where a rule waits, and the whole model on submit |

## Contributing

Expand Down
7 changes: 7 additions & 0 deletions docs/design/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ Where the code so far differs from the text above:
- **Generic types** with rules are an error (LPAD3101) for now.
- **LPAD3001** looks only at types that have rules themselves: a type with no rules and a member whose type has them is not reported.

- **A rejected value is an exception at the transport**: `ValidationFailedException`, which carries the result and which `ValidationResult.ThrowIfInvalid()` throws. Each integration turns it into its transport's error shape, as the ZeroAlloc packages did with that library's exception; none validates by itself. What throws it for a request is the mediator pipeline, which is not written: it waits for the mediator switch (phase 5, still open).
- **Failures on the wire** carry `path`, `code`, `message`, `severity` and `arguments`, the values of the message's placeholders as text (a number or a date in its invariant form, so a JSON context needs no entry for each type). Over HTTP: a 422 `ValidationFailedProblemDetails` with `errors` by path and `notices` for what came with them. Over gRPC: `InvalidArgument`, the errors in the status detail and every failure as JSON in the `validation-failures-bin` trailer (a path is not a valid metadata key, and a message need not be ASCII). In GraphQL: one `VALIDATION` error for each failure, with `propertyName`, `errorCode`, `severity` and `arguments` as extensions.
- **Warnings on successful responses** go through `ValidationNotices`: whatever validates a request adds what did not reject it, and the transport sends it. A transport begins the notices before the request is handled and they are ambient from there (an `AsyncLocal`), registered scoped as the ones of the request being handled: a GraphQL resolver runs in a scope of its own, and a scoped service alone would never reach the response. They travel as the `Validation-Notices` response header over HTTP (a JSON array), the `validation-notices-bin` trailer over gRPC, and the `validationNotices` extension of a GraphQL result.
- **OpenAPI at run time** reads `ValidationManifest`, the rules of each validated type as data: the generator writes one as the validator's `Manifest` (with the rules of what the type derives from) and the registration adds it. `ValidationOpenApiSchemaTransformer` states the rules of a schema property from the member System.Text.Json serializes it from, whichever way the rule was declared. In the manifest a rule under a condition, a warning, and a rule about a member of a collection's item start with `when` and are left out; a rule about the items of a collection starts with `each` and goes to the `items` schema.
- **OpenAPI at build time** (the API generators, `SchemaRules`) reads LaunchPad's rule attributes from the request's members, beside ZeroAlloc.Validation's until that goes. It can't read the manifest of a type in the same compilation, which another generator writes, so the rules of a `Validate` function reach the document only through the transformer above.
- **Blazor** uses the asynchronous form validation .NET 11 added: `LaunchPadValidator` validates a field that changes with `IValidateMembers<T>`, at once when its rules are synchronous and otherwise as `EditContext.RegisterAsyncFieldValidator`, so `IsValidationPending` is the pending state and a later change cancels an earlier one; `EditContext.ValidateAsync` awaits the whole model, and `Validate` throws for a model with a rule that waits, as the form does for any asynchronous validator. The validator is found by the model's run-time type through `ObjectValidators`, which the generated registrations fill in, with no reflection. A failure of a nested member is shown on the path's field of the top model, so it is in the summary and not beside the nested input.

## Tech stack

- .NET 11, C# 15, `$(LaunchPadTargetFrameworks)`; the analyzer package targets `netstandard2.0`.
Expand Down
Loading
Loading