IfProperty 1.0.0

.NET Core 2.2
dotnet add package IfProperty --version 1.0.0
NuGet\Install-Package IfProperty -Version 1.0.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="IfProperty" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IfProperty --version 1.0.0
#r "nuget: IfProperty, 1.0.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 IfProperty as a Cake Addin
#addin nuget:?package=IfProperty&version=1.0.0

// Install IfProperty as a Cake Tool
#tool nuget:?package=IfProperty&version=1.0.0

IfProperty

========== .NET library for building attributes based validation rules

This library add few useful validation attributes.

Validation expressions:

  • Is
  • EqualTo
  • GreaterThan
  • GreaterThanOrEqualTo
  • In
  • LessThan
  • LessThanOrEqualTo
  • NotEqualTo
  • Not in
  • Not Regex
  • Regex

Custom required validators:

  • RequiredIf
  • RequiredIfNotNull

Installation

NuGet: install-package IfProperty no setup needed Good practice - Add using static IfProperty.IsOperatorEnumeration; on top of file.

Examples

Equality

using static IfProperty.IsOperatorEnumeration;

private class Model
{
    // Value of property Value2 equals constant value "test" verification
    [Is(EqualTo, Value = "test")]
    public string Value2 { get; set; }
}
        
private class Model
{
    public string Value1 { get; set; }
    
    // Value of property Value2 equal to value of property Value1 verification 
    [Is(EqualTo, Property = nameof(Value1), PassOnNull = true)]
    public string Value2 { get; set; }
}

private class Model
{
    public string Value1 { get; set; }
    
    // Value of property Value2 equal to value of property Value1 verification
    // PassOnNull - if both options are null -> true, if one is not null -> false
    [Is(EqualTo, Property = nameof(Value1), PassOnNull = true)]
    public string Value2 { get; set; }
}

private class Model
{
    public string Value1 { get; set; }
    
    // Value of property Value2 not equal to value of property Value1 verification
    [Is(NotEqualTo, Property = nameof(Value1), PassOnNull = true)]
    public string Value2 { get; set; }
}

GreaterThan/LessThan

using static IfProperty.IsOperatorEnumeration;

private class Model
{
    // Value of property is greater than 1
    [Is(GreaterThan, Value = 1)]
    public int? Value1 { get; set; }
    
    // Value of property is greater than or equal to 1
    [Is(GreaterThan, Value = 1)]
    public int? Value2 { get; set; }
    
    //Value of property is less than 1
    [Is(LessThan, Value = 1)]
    public int? Value3 { get; set; }
    
    // Value of property is less than or equal to 1
    [Is(LessThanOrEqualTo, Value = 1)]
    public int? Value4 { get; set; }
    
    // Value of property is less than 1 or null
    [Is(LessThan, Value = 1, PassOnNull = true)]
    public int? Value5 { get; set; }
}

In/Not in

using static IfProperty.IsOperatorEnumeration;

private class Model
{
    public string[] Value1 { get; set; }

    // Value of property Value2 is in Value1 array values or both values are null
    [Is(In, Property = nameof(Value1), PassOnNull = true)]
    public string Value2 { get; set; }
    
    // Value of property Value is in {"testStaticValue", "testStaticValue2" }
    [Is(In, Value = new [] {"testStaticValue", "testStaticValue2" })]
    public string Value { get; set; }
}

Regex

using static IfProperty.IsOperatorEnumeration;

private class Model
{
    // Value of property Value is match email regex. If value is invalid validator return custom message.
    [Is(RegExMatch, Value = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", CustomMessage = "Format of the email address isn't correct")]
    public string Value { get; set; }
    
    // Value of property Value2 isn't match email regex
    [Is(NotRegExMatch, Value = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$")]
    public string Value2 { get; set; }
}

Add custom expression attribute

using static IfProperty.IsOperatorEnumeration;

// Register expression
// Important! Container and System.ComponentModel.DataAnnotations.Validator are static class.
// If you want to be sure that a custom expression will be used, you should register it on startup 
IfPropertyExpressionsContainer.AddOrOverride("invalid", new CustomInvalidPropertyExpression());

// Custom validator
private class CustomInvalidPropertyExpression : IIfPropertyExpression
{
    public string ErrorMessage => "invalid";

    public bool IsValid(object value, object dependentValue)
    {
        return false;
    }
}

// Override existing expression
IfPropertyExpressionsContainer.AddOrOverride(EqualTo, new CustomInvalidPropertyExpression());
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.2 netcoreapp3.0 netcoreapp3.1
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.2

    • No dependencies.

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
1.0.0 204 4/24/2021