IfProperty 1.0.0

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 and Polyglot Notebooks. 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 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.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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 355 4/24/2021