Validation Check for Multiple Objects - C#

Viewed 107

So, I am creating a standalone application which is responsible to analyze the incoming json files and return back some result. One API call will be called consuming all the objects. On my side I need to validate the Objects before analyzing them. So, I would like to know if validation here (data.DataOne != null && data.DataTwo != null) is the correct way of validating or just Model.IsValid will suffice. Your kind help is appreciated.

JSON format

{
"DataOne": {
   "Name":"abc",
   "Age":21
},
"DataTwo": {
   "Designation":"Tiktoker",
   "Salary":"NA"
}
}

Below is my C# codes.

       [HttpPost]
        public async Task<Analyse> Analyse([FromBody] FinalData data)
        {
            if (data.DataOne != null && data.DataTwo != null)
            {
               if (ModelState.IsValid)
               {
                 // do something
               }
              else
              {
                
              }
        }

Models are below

public class FinalData {
   public class DataOne {get;set;}
   public class DataTwo {get;set;}
}

public class DataOne{
   [Required]
   public string Name  {get;set;}
   public string Age {get;set;}
}

public class DataTwo{
   public string Designation  {get;set;}
   public string Salary {get;set;}
}
0 Answers
Related