Error when I pass my object to validate equal null
See original GitHub issueHello everyone
I’m sorry I do not speak English and also if this channel is not considered appropriate for clarification of doubt. I am building a WebApi in .Net core 2.0 using version 7.5.2.
A validation that I must do is when my client that will consume my WebApi, should check if the object that arrived is null and for this I did the following validation
My validations
public class FaturaDtoValidator: AbstractValidator<FaturaDto>
{
public FaturaDtoValidator()
{
RuleFor(fatura => fatura).NotNull().WithMessage(Mensagem.FATURA_OBRIGATORIO);
RuleFor(fatura => fatura.Itens).SetCollectionValidator(new ContratoItemDtoValidator());
RuleFor(fatura => fatura.Itens).NotNull().WithMessage(Mensagem.ITENS_OBRIGATORIO);
RuleFor(fatura => fatura.Itens).Must(list => list.Count == 0).WithMessage(Mensagem.ITENS_OBRIGATORIO);
}
}
public class ContratoItemDtoValidator : AbstractValidator<ContratoItemDto>
{
public ContratoItemDtoValidator()
{
RuleFor(item => item.CodigoItem).NotEmpty().NotNull().WithMessage(Mensagem.CODIGO_ITEM_OBRIGATORIO);
}
}
My startup class
services
.AddTransient<IValidator<FaturaDto>, FaturaDtoValidator>()
.AddTransient<IValidator<ContratoItemDto>, ContratoItemDtoValidator>();
Meu controller (My controller)
[Produces("application/json")]
[Route("api/v1/")]
public class HubController : Controller
{
[HttpPost("{fatura}")]
[Route("faturamento")]
public async Task<IActionResult> Faturamento([FromBody]FaturaDto fatura)
{
OrchestratorApp orc = new OrchestratorApp();
return await orc.RedirectFaturamento(fatura);
}
}
And to trigger validation
public async Task<JsonResult> RedirectFaturamento(FaturaDto fatura)
{
...
FaturaDtoValidator validator = new FaturaDtoValidator();
ValidationResult results = validator.Validate(fatura);
My FaturaDto object came null and it is this test I would like to include but when the validation is triggered an error occurs “ArgumentNullException: Value can not be null. Parameter name: Can not pass null model to Validate.”
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Is it a bad idea if equals(null) throws NullPointerException ...
My argument from this angle is really simple. equals tests whether some other object is "equal to" this; null reference gives no other...
Read more >Should one check for null if he does not expect null?
The error might be that something has changed in the code and null is now an expected value, or there is some other...
Read more >Comparing object with null prototype and objects derived ...
Currently it fails but IMHO I think it should pass. The reason is both the objects have the same keys. However objA is...
Read more >null - JavaScript - MDN Web Docs - Mozilla
The null value represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy ......
Read more >Null in Python: Understanding Python's NoneType Object
In this tutorial, you'll learn about the NoneType object None, which acts as the null in Python. This object represents emptiness, and you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I understood the explanation and thank you for your time.
You’re welcome - let me know if you have any other questions.