C
C#10mo ago
GooBad

✅ Help understanding validation magic in ASPNET WebAPI

imagine what, you fucking can't
65 Replies
GooBad
GooBadOP10mo ago
cuz its 2024 and there's 99999 bilion into AI but can't fix debugger so I can actually follow the stack where is the fucking validation hppening? i think noone really knows just fuck you microsfot
Ainz
Ainz10mo ago
For an ASP.NET Core MVC app?
GooBad
GooBadOP10mo ago
The naming... i think yes, but who knows?
C#
[ApiController]
[Route("[controller]")]
public class ProjectsController : ControllerBase
{
C#
[ApiController]
[Route("[controller]")]
public class ProjectsController : ControllerBase
{
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
lol i think yes i ended up using IValidatableObject but still have no idea where its executed I would really love to know how can I pass HttpRequest into Validate
C#
public class ProjectCreateBody: IValidatableObject
{
[Required]
public string Name { get; set; }

[Required]
public string[] Query { get; set; }

[Required]
public string[] Urls { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{

var db = validationContext.GetService<Database>();

if (db.Projects.Exists(p => p.Name == Name))
{
yield return new ValidationResult("Project already exists", [nameof(Name)]);
}
}
}

[HttpPost(Name = "CreateProject")]
[ProducesResponseType(StatusCodes.Status201Created, Type=typeof(Project))]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ValidationProblemDetails))]
public IActionResult Post([FromBody] ProjectCreateBody project)
{
return Ok(project);
}
C#
public class ProjectCreateBody: IValidatableObject
{
[Required]
public string Name { get; set; }

[Required]
public string[] Query { get; set; }

[Required]
public string[] Urls { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{

var db = validationContext.GetService<Database>();

if (db.Projects.Exists(p => p.Name == Name))
{
yield return new ValidationResult("Project already exists", [nameof(Name)]);
}
}
}

[HttpPost(Name = "CreateProject")]
[ProducesResponseType(StatusCodes.Status201Created, Type=typeof(Project))]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ValidationProblemDetails))]
public IActionResult Post([FromBody] ProjectCreateBody project)
{
return Ok(project);
}
Anton
Anton10mo ago
Put breakpoints on property getters maybe enable external sources debugging also it's off by default because usually people don't look into how libraries work How it works in short: it makes a metadata structure, then it goes through the attributes and stores ones that are for validation. If you go into Required, it will probably have a method for validation. There are some systems that let it add extra stuff, iirc (providers) Don't do that in mvc validation btw It's synchronous, don't access the db in there All of that should be done manually If you want to generalize that, you have to make a custom system for it
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
thanks a lot Anton yea and what comes back is exactly what makes it so confusing all the information t hat search engines and ai brings is shit comapred to what Anton posted for example cuz it requires deep understanding which docs dont provide
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
as search engine
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
its pretty good, but not with MSdocs
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
its not enough data to properly understand the proiblem
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX10mo ago
TeBeCo
really ? which part ?
React with ❌ to remove this embed.
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
im not saying u have no point here
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
thanks for sharing ill check this out
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
ill check in debugger first with external sources
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
That doc is mostly how to use it
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
There is some info on the internals, but not enough to understand it to a reasonable extent
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
hmm i have external sources enabled wondering if its the rider that cannot pickup and i should open visual studio
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
Reading the code and possible stepping through code is more helpful in this case imo
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
It should work in rider
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
Where's your breakpoint at?
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
yea but its just the design
Anton
Anton10mo ago
Sure, it is pretty high level though
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
i dont trust the image i want the code :d
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
The docs are too high level to be able to understand how it ties together deeper
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
Yeah probably, because the only thing that helped me when I needed a thing that had to do with that, I had to look at the sources And I did read all that at some point
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
Don't use the db context there, still
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
MVC validation is synchronous
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
Don't forget that minimal API doesn't have built-in validation, so you have to do something manually, or add an external dependency
GooBad
GooBadOP10mo ago
im checking debugging in vs code now, with breakpoints on properties rider still has some problems
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
I don't mean it's bad, just mentioning that they will need to depend on a lib for validation
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
so can you both agree that MVC is the past ?
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
can i get minimal api to work so well with swagger like the MVC controller?
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
If you add your models as parameters to the request handler delegate, they will be visible in Swagger It's more complicated otherwise Well, it is kind of the same with mvc in this regard
GooBad
GooBadOP10mo ago
maybe this is largely an XY problem? my problem, trying to find a linear reproducable way of correctly doing the APIS with some "OpenApi" auto genreated schema that can be reuysed by 3rd party utilizing the API
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Anton
Anton10mo ago
One problem I have with their OpenAPI metadata abstractions, which I think both MVC and minimal API generate, is that they can't represent everything OpenAPI can, and you'll have easier time adjusting the schema generation with a filter sometimes. At least in my experience
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
now I am lost which way would be the correct one damn you guys 😂 more questions than answers but back to the root of the topic i guess it makes sense thank you for info and patience to my stupid ass Visual Studio debugger works fine this makes sense + FluentValidation, + its explicit and it wont be synchronous + i can use builtin openapi with swashbuckle still thats where we will see how it works ou
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GooBad
GooBadOP10mo ago
debugging in rider seems to work too but had to switch some inputs perfect timing 🙂

Did you find this page helpful?