title: Enum as Required Field in ASP.NET Core WebAPI
body: <p>Is it possible to return the <code>[Required]</code> attribute error message when a JSON request doesn't provide a proper value for an enum property?</p>
  
  <p>For example, I have a model for a POST message that contains an <code>AddressType</code> property that is an enumeration type:</p>
  
  <pre><code>public class AddressPostViewModel
  {
      [JsonProperty("addressType")]
      [Required(ErrorMessage = "Address type is required.")]
      public AddressType AddressType { get; set; }
  }
  </code></pre>
  
  <p>The <code>AddressType</code> enum accepts two values:</p>
  
  <pre><code>[JsonConverter(typeof(StringEnumConverter))]
  public enum AddressType
  {
      [EnumMember(Value = "Dropship")]
      Dropship,
      [EnumMember(Value = "Shipping")]
      Shipping
  }
  </code></pre>
  
  <p>I've noticed (or actually my QA team noticed) that if the request message JSON contains either an empty string or null for the <code>AddressType</code>, the error message isn't the expected <code>Address type is required.</code> message. Instead, the error message is a somewhat unfriendly parsing error.</p>
  
  <p>For example, if the request JSON looks like this:</p>
  
  <pre><code>{  "addressType": "" }
  </code></pre>
  
  <p>Then the error that is auto-generated by the validation framework looks like this:</p>
  
  <pre><code>{
      "message": "Validation Failed",
      "errors": [
          {
              "property": "addressType",
              "message": "Error converting value \"\" to type 'MyNamespace.AddressType'. Path 'addressType', line 4, position 19."
          }
      ]
  }
  </code></pre>
  
  <p>Is there a way to ensure that error message of the <code>[Required]</code> attribute is returned if someone doesn't include a valid value for an enum?</p>