Skip to content

netceteragroup/valdr-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

valdr .NET Validation

Build status Build Status Coverage Status

.NET plugin for valdr, an AngularJS model validator.

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

NuGet License

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

NuGet License

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:

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 email
URL url

License

MIT © Netcetera AG