Dryv 1.0.0-preview.1

Server- and client-based model validation for ASP.NET Core 2.0 with rules written once in C#.

This is a prerelease version of Dryv.
There is a newer version of this package available.
See the version list below for details.
Install-Package Dryv -Version 1.0.0-preview.1
dotnet add package Dryv --version 1.0.0-preview.1
<PackageReference Include="Dryv" Version="1.0.0-preview.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dryv --version 1.0.0-preview.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Dryv

NuGet NuGet

Overview

According to Rick Anderson, "One of the design tenets of MVC is DRY ("Don't Repeat Yourself")" and
"The validation support provided by MVC and Entity Framework Core Code First is a good example of the DRY principle in action.
You can declaratively specify validation rules in one place (in the model class) and the rules are enforced everywhere in the app"
(from Microsoft Docs).

While this is the case for simple validation rules, applying complex validations rules is a different story. For instance, see the foloowing model.

public class Customer
{
	[Required]
	public string Name { get; set; }

	public string Company { get; set; }

	public string TaxId { get; set; }
}

Using the annotation attributes provides by .NET, we can state that the Name property must be specified. ASP.NET MVC will render JavaScript
code for us that performs model validation in the browser, and it will peroform server-side model validation in the controller. That's cool, certainly.
But consider the following case: neither the company nor the tax ID fields are required at first. But if the user enters a company name, the tax ID
field becomes required. How would you implement such a validation? The recommended way is to write an own validation attribute subclassing
ValidationAttribute,
make it implement IClientModelValidator,
implement server side validation and add client-side code to implement a jQuery validator. Real-world application can have lots and lots of different
validation rules and implementing them in C# as well as JavaScript can become a cumbersome task.

That's where Dryv comes in. The name "Dryv" is derived from the term "DRY Validation".Using Dryv, you define the rules using C# expressions
and some inner magic will translate them to JavaScript. Taking teh example above, using Dryv it would look like this:

public class Customer
{
    public static readonly DryvRules Rules = DryvRules
        .For<Customer>()
        .Rule(m => m.TaxId,
            m => string.IsNullOrWhiteSpace(m.Company) || !string.IsNullOrWhiteSpace(m.TaxId)
                ? DryvResult.Success
                : $"The tax ID for {m.Company} must be specified.");

    [Required]
    public string Name { get; set; }

    public string Company { get; set; }

    [DryvRules]
    public string TaxId { get; set; }
}

In the code above, a set of rules for the class Customer is defined. This set of rules contains a rule for the property TaxId.
The property TaxId has an attribute DryvRulesAttributes that makes Dryv play nicely with the ASP.NET MVC validation framework.
On the client, you need to load the appropriate JavaScript implementation of Dryv. Currently, an implementation exists for jQuery unobtrusive. Other
implementations (e.g. for VueJS or React) can easily be added (and will be over time).

Installation

Server

On the server, install the NuGet package:

dotnet add package Dryv 

... or ...

Install-Package Dryv 

... or ...

paket add Dryv 

Client

On the client, install the NPM package:

npm install --save dryv-jquery-unobtrusive 

... or download the JS file directly into your project and reference it from your page:

<script src="js/dryv-jquery-unobtrusive.min.js"></script>

Dryv

NuGet NuGet

Overview

According to Rick Anderson, "One of the design tenets of MVC is DRY ("Don't Repeat Yourself")" and
"The validation support provided by MVC and Entity Framework Core Code First is a good example of the DRY principle in action.
You can declaratively specify validation rules in one place (in the model class) and the rules are enforced everywhere in the app"
(from Microsoft Docs).

While this is the case for simple validation rules, applying complex validations rules is a different story. For instance, see the foloowing model.

public class Customer
{
	[Required]
	public string Name { get; set; }

	public string Company { get; set; }

	public string TaxId { get; set; }
}

Using the annotation attributes provides by .NET, we can state that the Name property must be specified. ASP.NET MVC will render JavaScript
code for us that performs model validation in the browser, and it will peroform server-side model validation in the controller. That's cool, certainly.
But consider the following case: neither the company nor the tax ID fields are required at first. But if the user enters a company name, the tax ID
field becomes required. How would you implement such a validation? The recommended way is to write an own validation attribute subclassing
ValidationAttribute,
make it implement IClientModelValidator,
implement server side validation and add client-side code to implement a jQuery validator. Real-world application can have lots and lots of different
validation rules and implementing them in C# as well as JavaScript can become a cumbersome task.

That's where Dryv comes in. The name "Dryv" is derived from the term "DRY Validation".Using Dryv, you define the rules using C# expressions
and some inner magic will translate them to JavaScript. Taking teh example above, using Dryv it would look like this:

public class Customer
{
    public static readonly DryvRules Rules = DryvRules
        .For<Customer>()
        .Rule(m => m.TaxId,
            m => string.IsNullOrWhiteSpace(m.Company) || !string.IsNullOrWhiteSpace(m.TaxId)
                ? DryvResult.Success
                : $"The tax ID for {m.Company} must be specified.");

    [Required]
    public string Name { get; set; }

    public string Company { get; set; }

    [DryvRules]
    public string TaxId { get; set; }
}

In the code above, a set of rules for the class Customer is defined. This set of rules contains a rule for the property TaxId.
The property TaxId has an attribute DryvRulesAttributes that makes Dryv play nicely with the ASP.NET MVC validation framework.
On the client, you need to load the appropriate JavaScript implementation of Dryv. Currently, an implementation exists for jQuery unobtrusive. Other
implementations (e.g. for VueJS or React) can easily be added (and will be over time).

Installation

Server

On the server, install the NuGet package:

dotnet add package Dryv 

... or ...

Install-Package Dryv 

... or ...

paket add Dryv 

Client

On the client, install the NPM package:

npm install --save dryv-jquery-unobtrusive 

... or download the JS file directly into your project and reference it from your page:

<script src="js/dryv-jquery-unobtrusive.min.js"></script>

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Dryv:

Package Downloads
Dryv.AspNetCore
ASP.NET Core support for Dryv. Dryv provides complex model validation for server and client made easy. Write complex model validation rules in C# once. Dryv will generate JavaScript for client-side validation.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
6.0.0-pre.26 171 8/1/2020
6.0.0-pre.25 32 7/31/2020
6.0.0-pre.24 114 7/31/2020
6.0.0-pre.23 29 7/30/2020
6.0.0-pre.22 72 7/29/2020
6.0.0-pre.21 115 7/29/2020
6.0.0-pre.20 75 7/28/2020
6.0.0-pre.19 68 7/27/2020
6.0.0-pre.18 141 7/26/2020
6.0.0-pre.17 76 7/25/2020
6.0.0-pre.16 44 7/23/2020
6.0.0-pre.15 28 7/22/2020
6.0.0-pre.14 56 7/22/2020
6.0.0-pre.13 79 7/20/2020
6.0.0-pre.12 46 7/20/2020
6.0.0-pre.11 38 7/20/2020
6.0.0-pre.10 25 7/17/2020
6.0.0-pre.9 129 7/16/2020
6.0.0-pre.8 111 7/16/2020
6.0.0-pre.7 48 7/15/2020
6.0.0-pre.6 88 7/10/2020
6.0.0-pre.5 41 7/9/2020
6.0.0-pre.4 38 7/8/2020
6.0.0-pre.3 63 7/7/2020
6.0.0-pre.2 74 7/5/2020
6.0.0-pre.1 52 7/2/2020
5.0.0 195 6/5/2020
4.0.0 114 5/16/2020
3.1.0 67 5/9/2020
3.0.1 84 4/17/2020
3.0.0 86 4/16/2020
2.0.0 406 5/14/2018
1.0.0 393 4/15/2018
1.0.0-preview.6 265 4/9/2018
1.0.0-preview.5 266 4/8/2018
1.0.0-preview.4 266 3/29/2018
1.0.0-preview.3 282 3/20/2018
1.0.0-preview.2 293 3/19/2018
1.0.0-preview.1 237 2/28/2018
Show less