How to run same model validation as it does on controllerValidation validation (using FromBody attribute) ?
I try to do it in my manualValidation endpoint as test example but I don't reach same results
Example: I send payload {"customerNumber": 33,"routingKey": null} to controllerValidation endpoint
and I get this result as expected
{
"errors": {
"routingKey": [
"The RoutingKey field is required."
]
}
}
If I run code shown manualValidation endpoint then it says that model is valid. I don't understand why cause RoutingKey property is null.
Model:
public class TestModel
{
public long CustomerNumber { get; set; }
public string RoutingKey { get; set; } = null!;//Should not be null
}
Controller:
[ApiController]
public class TestController : ControllerBase
{
[HttpPost]
[Route("controllerValidation")]
public TestModel Create([FromBody] TestModel model)
{
//false
return model;
}
}
When I run unit test then I get true as validation result
[TestMethod]
public void TestModel()
{
var model = new TestModel();
model.CustomerNumber = 33;
var context = new ValidationContext(model);
var results = new List<ValidationResult>();
var isValid = Validator.TryValidateObject(model, context, results, true);//returns true , expect to have false cause RoutingKey is null
Assert.IsFalse(isValid);
}
The only thing how to make it work properly in unit test it to add [Required] attribute to RoutingKey but that's not good solution as I have to do it for all models everywhere as well ...
UPD:this is .NET6