Awesome
valdr .NET Validation
.NET plugin for valdr, an AngularJS model validator.
- Offering
- Installation
- Dependencies
- Mapping of .NET DataAnnotations attributes to valdr constraints
- License
Offering
valdr .NET parses C# classes for DataAnnotation attributes and extracts their information into a JavaScript file, which includes the metadata to be used by valdr. This allows to apply the exact same validation rules on the server and on the AngularJS client. valdr .NET core exposes only the parser logic and a few helpful attributes, so that it can be used in your own tooling as needed.
The biggest difference between the two packages is that valdr .NET does not support contract identification by attributes other than DataContract / DataMember. valdr .NET core allows use of arbitrary types in place of these.
Installation - valdr .NET
To install the Nuget package, run the following command in the Package Manager Console:
PM> Install-Package Nca.Valdr
In Visual Studio, right-click your project and under Properties/Build Events add the following Post-build event:
$(SolutionDir)packages\Nca.Valdr.1.1.4\tools\Nca.Valdr.Console.exe -i:$(TargetDir)$(TargetFileName) -o:$(ProjectDir)app\app.valdr.js
Nca.Valdr.exe accepts the following parameters:
-i:
input assembly filename (.dll)-n:
namespace filter (default: all)-o:
output JavaScript filename-a:
AngularJS application name (default: "app")-c:
Culture (optional, e.g. "en" or "en-US")
At this time, only C# classes decorated with a DataContract attribute will be used the generate the valdr metadata.
Installation - valdr .NET core
To install the Nuget package, run the following command in the Package Manager Console:
PM> Install-Package Nca.Valdr.Core
This will add the core library to your project, which brings with it the parser and a couple of attributes that can be used to tag your models. These tools that can be used to generate constraints as needed. Here is an example that will generate constraints at runtime using the provided constraints and serve through a controller action:
public class ConstraintsController : ApiController
{
private readonly IParser _constraintParser;
public ConstraintsController()
{
_constraintParser = new Parser();
}
[HttpGet]
public JObject Index()
{
return _constraintParser.Parse(
//optional culture (for resolving validation messages from resource files)
CultureInfo.CurrentCulture,
//optional namespace filter - StartsWith search
null,
//type and member name on type used to identify and name constraints
new ValdrTypeAttributeDescriptor(typeof(ValdrTypeAttribute), nameof(ValdrTypeAttribute.Name)),
//type name used to identify data members
nameof(ValdrMemberAttribute),
//assembly(s) to parse for constraint generation
Assembly.GetAssembly(typeof (MyDTO))
);
}
}
When using the parser directly, it is possible to specify different attributes to use for contract tagging. The only restriction is that they need to be defined using the "Named Argument" syntax instead of constructor parameters, eg
[ValdrType(Name = "ConstraintForMyDTO")]
public class MyDTO
{
[ValdrMember(Name = "PropertyNameOnClientSide")]
public string MyProperty { get; set; }
}
Dependencies
valdr .NET Validation is dependent on valdr in two ways:
- JSON structure is defined by valdr
- validators listed in the JSON document have to be either a supported valdr valdidator or one of your custom JavaScript validators
To indicate which valdr version a specific valdr .NET version supports there's a simple rule: the first digit of the valdr .NET version denotes the supported valdr version. Version 1.x will support valdr 1. This means that valdr .NET 1.x+1 may introduce breaking changes over 1.x because the second version digit kind-of represents the "major" version.
Mapping of .NET DataAnnotations attributes to valdr constraints
The .NET DataAnnotations attributes defines the mapping of .NET Validation to valdr constraints.
.NET DataAnnotations | valdr | Comment |
---|---|---|
Required | required | |
Range | min | |
Range | max | |
StringLength | size | |
digits | unsupported | |
RegularExpression | pattern | |
Future | future | Nca.Valdr namespace |
Past | past | Nca.Valdr namespace |
EmailAddress | ||
URL | url |
License
MIT © Netcetera AG