Skip to content

.NET plugin for valdr, an AngularJS model validator.

License

Notifications You must be signed in to change notification settings

AlexCuse/valdr-dotnet

 
 

Repository files navigation

valdr .NET Validation

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.1\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 : Controller
    {
        private readonly IParser _constraintParser;

        public LaunchController(IParser constraintParser)
        {
            _constraintParser = constraintParser;
        }

        [Route("/api/Constraints")]
        [ResponseCache(Duration = 600)]
        public IActionResult Index()
        {
            JObject constraints = _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(ValdrType), nameof(ValdrType.Name)), 
				//type name used to identify data members
				nameof(ValdrMember), 
				//assembly(s) to parse for constraint generation
                Assembly.GetAssembly(typeof (MyDTO)) 
			);
            
            return new ObjectResult(constraints);
        }
    }

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:

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 unsupported
past unsupported
EmailAddress email
URL url

License

MIT © Netcetera AG

About

.NET plugin for valdr, an AngularJS model validator.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 56.1%
  • PowerShell 41.6%
  • JavaScript 2.3%