LinkDotNet.ValidationExtensions 2.4.0

.NET 6.0 .NET Standard 2.0
dotnet add package LinkDotNet.ValidationExtensions --version 2.4.0
NuGet\Install-Package LinkDotNet.ValidationExtensions -Version 2.4.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LinkDotNet.ValidationExtensions" Version="2.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LinkDotNet.ValidationExtensions --version 2.4.0
#r "nuget: LinkDotNet.ValidationExtensions, 2.4.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install LinkDotNet.ValidationExtensions as a Cake Addin
#addin nuget:?package=LinkDotNet.ValidationExtensions&version=2.4.0

// Install LinkDotNet.ValidationExtensions as a Cake Tool
#tool nuget:?package=LinkDotNet.ValidationExtensions&version=2.4.0

ValidationExtensions

.NET nuget

The motivation behind this small project is simple. Just imagine you have the following model in Blazor:

public class MyModel
{
    [Required]
    public string Title { get; set; }

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

    [Required]
    public bool IsPublished { get; set; }
}

Now as a consumer you have to provide all of those 3 values. That is all good and nice, but what if we want to say: "Okay as long as it doesn't get published, we don't have to provide the content?". Well that does not work with the default implementation.

Here is where this small library comes into play:

public class MyModel
{
    [Required]
    public string Title { get; set; }

    [RequiredIf(nameof(IsPublished), true)]
    public string Content { get; set; }

    [Required]
    public bool IsPublished { get; set; }
}

Now Title will always be required. But as long as IsPublished is false Content can be null or empty.

Get Started

To install either go the nuget or execute the following command:

dotnet add LinkDotNet.ValidationExtensions

Example

using LinkDotNet.ValidationExtensions;

public class BlogArticle
{
    [Required]
    public string Title { get; set; }

    [RequiredIf(nameof(IsPublished), true)]
    public string ArticleContent { get; set; }

    [RequiredIfNot(nameof(ArticleContent), null)]
    public string ReplacementContent { get; set; }
    
    [Required]
    public bool? NoticeByEmail { get; set; }

    [RequiredDynamic(nameof(ValidateRequired_NoticeByEmail), "Notice by email is activated")]
    public string? EmailAddress { get; set; }
    
    [DynamicRange(typeof(decimal), minimum: 9.99, maximumPropertyName: nameof(MaximumPrice))]
    public decimal? MinimumPrice { get; set; }

    [DynamicRange(typeof(decimal), minimumPropertyName: nameof(MinimumPrice), maximum: 199.99)]
    public decimal? MaximumPrice { get; set; }

    [DynamicRange<double>(minimum: 0.1d, maximumPropertyName: nameof(MaximumWeight))]
    public double? MinimumWeight { get; set; }

    [DynamicRange<double>(minimumPropertyName: nameof(MinimumWeight), maximum: 500d)]
    public double? MaximumWeight { get; set; }

    private static bool ValidateRequired_NoticeByEmail(BlogArticle value)
    {
        if (!value.NoticeByEmail.HasValue)
        {
            return false;
        }

        if (!value.NoticeByEmail.Value)
        {
            return false;
        }

        if (string.IsNullOrWhiteSpace(value.EmailAddress))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Currently implemented additional attributes:

  • RequiredIf
  • MinLengthIf / MaxLengthIf
  • RangeIf
  • MinIf / MaxIf
  • Min / Max
  • Dynamic
  • DynamicRange
  • DynamicRange<T> C# 11 Generic Attributes
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.4.0 69 1/16/2023
2.3.2 80 1/9/2023
2.2.0 84 1/3/2023
2.1.0 245 3/4/2022
2.0.0 219 2/17/2022
1.1.0 220 2/16/2022
1.0.0 221 2/15/2022