I have the following request model :
public class VisitRequest
{
public ProviderInfo Provider { get; set; }
[Required]
[MaxLength(64)]
public string PayerId { get; set; }
}
And i have the custom class as follows :
public class ProviderInfo
{
[Required]
[MaxLength(15)]
public string TaxId { get; set; }
[Required(ErrorMessage = " Qualifier is required.")]
[MaxLength(15)]
public string NPI { get; set; }
}
I am currently using ValidationContext to do the model validations :
var visitData = JsonConvert.DeserializeObject<VisitRequest>(jsonString);
var vc = new ValidationContext(visitData);
var errorResults = new List<ValidationResult>();
var isValid = Validator.TryValidateObject(visitData, vc, errorResults, true);
My demo json request is as follows :
{
'Member': {
'Qualifier': 'MedicaidID',
'Identifier': '100'
},
'ExternalVisitID': '123456789',
}
Now I am receiving the ErrorMessage in ErrorResult as :
PayerId is required.
But I am not getting the validations of the custom class. How to implement this in .Net Core Console application? I am quite new to .Net Core so any help would be appreciated.Thanks!
UPDATE :
I followed the answer given by @John H but still there is one issue .
If my request is :
'Provider' : {
'TaxId' : null
}
it will work fine , because it is able to identify that this is the type ProviderInfo object.
But if my request does not contain any info regarding the Provider :
{
'ExternalVisitID': '123456789',
'EVVMSID': '100',
}
then it is not able to validate the object.