Open JCKortlang opened 3 months ago
Validating parameters that are simple types is not supported currently. This is similar to how ModelValidation
in ASP.NET Core MVC works. You need to accept parameters that are complex types whose properties have the validation attributes on them. You can combine a complex type with AsParameters
to achieve what you want:
// Will fail when id does not equal "hello" or "world". Expect 400 response
builder.MapGet("/test/{id}", async ([AsParameters] TestReuqest request) =>
{
// Will only return if properties of `request` are valid
return Results.Ok("hello world");
}).WithParameterValidation();
public class TestRequest
{
[FromRoute, RegularExpression("^(hello|world)$")]
public string? Id { get; set; }
}