Wednesday 24 October 2012

MS Build Task to create jQuery validation metadata from DataAnnotation attributes in ASP.NET Web Api

DataAnnotations provide a great mechanism for simple model validation (string lengths, regex, ranges etc) upon submission to the server. However in today's solutions we usually want to evaluate the same rules on the client as the user is entering data - this adds an overhead of creating the same rules twice (once in js and once on the server).

One solution is to build the js rules directly from the DataAnnotations - the following snippet of code does this and demonstrates loading both built in DataAnnotations and custom annotations, it generates a module that can be loaded using requirejs to provide rule data for jQuery validation.

A set of rules is generated for every object in the namespace suplied in "ModelNamespace" that has DataAnnotations.

The following example is best used in builds build from the command line - the reason being that the assembly cannot be unloaded following inspection (I have not been able to come up with a solution for this short of building a full plugin mechanism for loading the assembly to be inspected).  My workaround for this is to only generate the js for release builds using the following task configuration:

  <Target Name="AfterBuild" Condition="$(Configuration) == 'Release'">
    <CreateJsFromAnnotations ModelAssembly="bin\WebInterface.dll" ModelNamespace="WebInterface.Models" OutputFile="Scripts\common\generated\ModelMetadata.js" />
  </Target>

Sample output would be something along the lines of:
define(
    [],
    function () {
        return {
  "folderValidation": {
    "folderapp": {
      "locationparsing": "PathOnly",
      "regex": "^[^\\s]*$",
      "maxlength": 256,
      "required": true
    },
    "folderproxy": {
      "locationparsing": "PathOnly",
      "regex": "^[^\\s]*$",
      "maxlength": 256,
      "required": true
    }
  }
};
    }
);

No comments:

Post a Comment