title: Net Core 3: Dynamic Model Validation at API runtime
body: <p>We have 1000 small lookup table editor in database. Each having few rows maximum. Instead of writing 1000 Apis, services, angular proxies we created an Object lookup viewer/editor in grid cell. Small Tables like ProductCodeLookup, AddressType, SupplyCategory, FurnitureType</p>
  <p>Also there is API Operator from a webpage, where people can See and Add Rows to the lookup Tables. (see picture link below)
  The question came up, how do we dynamically apply Model Validation on the Request Object in API?</p>
  <p>Front end validation is on Angular</p>
  <p>However, for C#, we want all LookupIds and int category to be 1-100 max. String request members to be 255 characters max. Emails to be in @email regex format etc. We may not know what Object request looks like at runtime, until table is selected.</p>
  <pre><code>[HttpPost(&quot;[Action]&quot;)]
  public void AddObject(List&lt;object&gt; addListRequest, string lookupTableName)
  {
      foreach (var addItem in addListRequest)
      {
          var addItemCast = addItem.ConvertObjectToTypeWithSerialization(atype);
          context.Add(addItemCast);
      }
  }
  </code></pre>
  <p>This answer for older Net MVC, we are using Net Core 3 API.  <a href="https://stackoverflow.com/questions/18919758/dynamically-apply-validation-rules-at-runtime-with-asp-net-mvc-4">Dynamically apply validation rules at runtime with ASP.NET MVC 4</a></p>
  <p>Thought there is method to conduct this with new library.</p>
  <p><a href="https://i.stack.imgur.com/zpSim.png" rel="nofollow noreferrer">EditorImage</a></p>