question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when I pass my object to validate equal null

See original GitHub issue

Hello 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);

codigo

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.”

erro

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
raffaelmirandacommented, Jun 20, 2018

I understood the explanation and thank you for your time.

0reactions
JeremySkinnercommented, Jun 20, 2018

You’re welcome - let me know if you have any other questions.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found