What is the correct way to use advance property validation when deserializing JSON to Model? I am providing the MyClass
as an example. I need to validate Name(required
) and Email(e-mail address validation
). I do find only [JsonProperty(Required = Required.Always)]
to validate the required properties and nothing for e-mail validation. Unfortunately, Data Annotation validators can't be used from MVC for validation.
One idea which comes to my mind is to create custom ContractResolver
and attach to deserializer where I could perform custom validation. Any other methods to consider?
public class MyClass
{
[JsonProperty(Required = Required.Always)]
public string Name { get; set; }
public string Email { get; set; }
}
_dto = JsonConvert.DeserializeObject<MyClass>(content);