IfProperty 1.0.0

.NET library for building attributes based validation rules.

Install-Package IfProperty -Version 1.0.0
dotnet add package IfProperty --version 1.0.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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());

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());

  • .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 History

Version Downloads Last updated
1.0.0 89 4/24/2021