Dryv 1.0.0-preview.2
See the version list below for details.
dotnet add package Dryv --version 1.0.0-preview.2
NuGet\Install-Package Dryv -Version 1.0.0-preview.2
<PackageReference Include="Dryv" Version="1.0.0-preview.2" />
paket add Dryv --version 1.0.0-preview.2
#r "nuget: Dryv, 1.0.0-preview.2"
// Install Dryv as a Cake Addin #addin nuget:?package=Dryv&version=1.0.0-preview.2&prerelease // Install Dryv as a Cake Tool #tool nuget:?package=Dryv&version=1.0.0-preview.2&prerelease
Dryv
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 browser-specific JS file directly from here into your project and reference it from your page:
<script src="js/dryv-jquery-unobtrusive.browser.min.js"></script>
Usage
In the ASP.NET Core startup class, add Dryv in the ConfigureServices method using the AddDryv extension method:
using Dryv;
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
services.AddDryv()
.AddMvc();
}
}
Also in the startup class, use Dryv in the Configure method using the UseDryv extension method:
using Dryv;
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDryv()
.UseMvc();
}
// ...
}
Examples
Simple Example
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.");
public string Company { get; set; }
[Required]
public string Name { get; set; }
[DryvRules]
public string TaxId { get; set; }
}
<form method="post">
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control">
<span asp-validation-for="Name"></span>
</div>
<div class="form-group">
<label asp-for="Company"></label>
<input asp-for="Company" class="form-control">
<span asp-validation-for="Company"></span>
</div>
<div class="form-group">
<label asp-for="TaxId"></label>
<input asp-for="TaxId" class="form-control">
<span asp-validation-for="TaxId"></span>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
Pre-evaluated Options
Pre-evaluated options are objects that get evaluated prior to the actual validation phase.
The pre-evaluated option values are then later used during model validation. For instance,
the pre-evaluated option values are inserted into the generated client code, as opposed to a translated
expression as it would normally be the case. In the example below, the string Conglom-O
will be
hard-coded into the generated JavaScript code.
public class Options
{
public string CompanyPrefix { get; set; }
}
public class Model
{
public static readonly DryvRules Rules = DryvRules
.For<Model>()
.Rule<IOptions<Options>>(m => m.Company,
(m, o) => m.Company.StartsWith(o.Value.CompanyPrefix)
? DryvResult.Success
: $"The company name must begin with '{o.Value.CompanyPrefix}'.");
[DryvRules]
public string Company { get; set; }
}
Objects used for pre-evaluation must be registered in the web applications service collection. That is, they must be registered as a service in the startup class. For example:
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
services.RegisterSingleton(Options.Create(new Options
{
CompanyPrefix = "Conglom-O"
}));
}
}
Rule Switches
Using application options (registered as an application service like above), validation rules
can dynamically be activated or deactivated. In the example below, the validation rule will only
be applied if the value of Options.CompanyNameRequired
is set to 'true'.
public class Options
{
public bool CompanyNameRequired { get; set; }
}
public class Model
{
public static readonly DryvRules Rules = DryvRules
.For<Model>()
.Rule<IOptions<Options>>(m => m.Company,
(m, o) => string.IsNullOrWhiteSpace(m.Company)
? "The company name must be specified."
: DryvResult.Success,
o => o.Value.CompanyNameRequired);
[DryvRules]
public string Company { get; set; }
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.0
- Microsoft.AspNetCore.Mvc.ViewFeatures (>= 2.0.2)
- Microsoft.AspNetCore.Razor.Runtime (>= 2.0.1)
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 | Downloads | Last updated |
---|---|---|
6.0.4 | 1,385 | 9/27/2024 |
6.0.3 | 1,234 | 8/19/2024 |
6.0.2 | 206 | 8/19/2024 |
6.0.1 | 169 | 7/25/2024 |
6.0.0 | 17,282 | 10/3/2021 |
6.0.0-pre.40 | 10,606 | 4/20/2021 |
6.0.0-pre.38 | 413 | 4/14/2021 |
6.0.0-pre.37 | 187 | 3/17/2021 |
6.0.0-pre.36 | 637 | 3/17/2021 |
6.0.0-pre.35 | 1,459 | 1/20/2021 |
6.0.0-pre.34 | 2,239 | 10/30/2020 |
6.0.0-pre.29 | 335 | 9/27/2020 |
6.0.0-pre.28 | 277 | 9/25/2020 |
6.0.0-pre.27 | 293 | 9/17/2020 |
6.0.0-pre.26 | 1,206 | 8/1/2020 |
6.0.0-pre.25 | 253 | 7/31/2020 |
6.0.0-pre.24 | 324 | 7/31/2020 |
6.0.0-pre.23 | 250 | 7/30/2020 |
6.0.0-pre.22 | 307 | 7/29/2020 |
6.0.0-pre.21 | 339 | 7/29/2020 |
6.0.0-pre.20 | 292 | 7/28/2020 |
6.0.0-pre.19 | 294 | 7/27/2020 |
6.0.0-pre.18 | 353 | 7/26/2020 |
6.0.0-pre.17 | 288 | 7/25/2020 |
6.0.0-pre.16 | 262 | 7/23/2020 |
6.0.0-pre.15 | 262 | 7/22/2020 |
6.0.0-pre.14 | 280 | 7/22/2020 |
6.0.0-pre.13 | 291 | 7/20/2020 |
6.0.0-pre.12 | 259 | 7/20/2020 |
6.0.0-pre.11 | 248 | 7/20/2020 |
6.0.0-pre.10 | 238 | 7/17/2020 |
6.0.0-pre.9 | 342 | 7/16/2020 |
6.0.0-pre.8 | 317 | 7/16/2020 |
6.0.0-pre.7 | 249 | 7/15/2020 |
6.0.0-pre.6 | 303 | 7/10/2020 |
6.0.0-pre.5 | 288 | 7/9/2020 |
6.0.0-pre.4 | 259 | 7/8/2020 |
6.0.0-pre.3 | 275 | 7/7/2020 |
6.0.0-pre.2 | 286 | 7/5/2020 |
6.0.0-pre.1 | 275 | 7/2/2020 |
5.0.0 | 648 | 6/5/2020 |
4.0.0 | 572 | 5/16/2020 |
3.1.0 | 540 | 5/9/2020 |
3.0.1 | 941 | 4/17/2020 |
3.0.0 | 663 | 4/16/2020 |
2.0.0 | 1,294 | 5/14/2018 |
1.0.0 | 1,087 | 4/15/2018 |
1.0.0-preview.6 | 611 | 4/9/2018 |
1.0.0-preview.5 | 621 | 4/8/2018 |
1.0.0-preview.4 | 614 | 3/29/2018 |
1.0.0-preview.3 | 661 | 3/20/2018 |
1.0.0-preview.2 | 657 | 3/19/2018 |
1.0.0-preview.1 | 569 | 2/28/2018 |