Zhomfr.Validation.Rules.Abstractions
0.7.0
dotnet add package Zhomfr.Validation.Rules.Abstractions --version 0.7.0
NuGet\Install-Package Zhomfr.Validation.Rules.Abstractions -Version 0.7.0
<PackageReference Include="Zhomfr.Validation.Rules.Abstractions" Version="0.7.0" />
<PackageVersion Include="Zhomfr.Validation.Rules.Abstractions" Version="0.7.0" />
<PackageReference Include="Zhomfr.Validation.Rules.Abstractions" />
paket add Zhomfr.Validation.Rules.Abstractions --version 0.7.0
#r "nuget: Zhomfr.Validation.Rules.Abstractions, 0.7.0"
#:package Zhomfr.Validation.Rules.Abstractions@0.7.0
#addin nuget:?package=Zhomfr.Validation.Rules.Abstractions&version=0.7.0
#tool nuget:?package=Zhomfr.Validation.Rules.Abstractions&version=0.7.0
Validation Rule Abstractions
⚠️ Warning: Active Development (< 1.0.0)
Prior to the release of stable version
1.0.0, this library follows SemVer for initial development. This means breaking changes may occur even in minor releases (0.x).We strongly recommend pinning the package version in your
.csprojto avoid unexpected build errors.
A lightweight, extensible foundational library for building custom validation rules in .NET. It provides a robust base class with essential infrastructure, property access helpers, and a dedicated set of validation exceptions to build clean and predictable validation pipelines.
Key Features
- Base Rule Infrastructure: Standardized core logic for defining custom, reusable validation rules.
- Property Value Extraction: Effortlessly extract object property values by name using built-in helper methods.
- Display Name Resolution: Automatically retrieve human-readable property names from attributes (such as
[Display(Name = "...")]). - Null-Safe Evaluation: Designed to return positive results on
nullinputs, ensuringRequiredrules can properly manage presence validation without unexpected side effects. - Error Message Formatting: Built-in template formatting for consistent and customizable error output.
- Validation Exception Hierarchy: A comprehensive set of domain-specific validation exceptions for robust error handling.
📦 Installation
Install package:
dotnet add package Zhomfr.Validation.Rules.Abstractions
Zhomfr.Validation.Rules.Abstractions
Zhomfr.Validation.Rules.Abstractions.Errors
ValuesHasDifferentTypesException
AbstractRule
Namespace: Zhomfr.Validation.Rules.Abstractions
public abstract class AbstractRule : System.ComponentModel.DataAnnotations.ValidationAttribute
Inheritance Object → Attribute → ValidationAttribute → AbstractRule<br> Attributes NullableContextAttribute, NullableAttribute, AttributeUsageAttribute
Properties
MessageArgs
Arguments for error message.
protected List<object> MessageArgs { get; }
Property Value
List<Object><br>
ErrorMessageString
protected string ErrorMessageString { get; }
Property Value
String<br>
RequiresValidationContext
public virtual bool RequiresValidationContext { get; }
Property Value
Boolean<br>
ErrorMessage
public string? ErrorMessage { get; set; }
Property Value
String<br>
ErrorMessageResourceName
public string? ErrorMessageResourceName { get; set; }
Property Value
String<br>
ErrorMessageResourceType
public Type? ErrorMessageResourceType { get; set; }
Property Value
Type<br>
TypeId
public virtual object TypeId { get; }
Property Value
Object<br>
Constructors
AbstractRule ()
protected AbstractRule()
Methods
CheckCondition (Object, ValidationContext)
Checks the validation condition for the specified value.
protected abstract bool CheckCondition(object value, ValidationContext context)
Parameters
value Object<br>
The value to validate.
context ValidationContext<br>
The validation context.
Returns
Boolean<br> True if the condition is met; otherwise, false.
Exceptions
UnsupportedTypeException<br> Thrown when the rule cannot process the data type.
Exception<br> Any other exceptions thrown by the logic.
FormatedMessage (String, Object)
Formats the error message using the display name, message arguments, and value.
protected virtual string FormatedMessage(string displayName, object value)
Parameters
displayName String<br>
The display name of the field being validated.
value Object<br>
The value that failed validation.
Returns
String<br> The formatted error message string.
IsValid (Object, ValidationContext)
protected override ValidationResult? IsValid(object? value, ValidationContext context)
Parameters
value Object?<br>
context ValidationContext<br>
Returns
ValidationResult?<br>
Exceptions
UnsupportedTypeException<br> Thrown when the rule cannot process the data type.
Exception<br> Any other exceptions thrown by the logic.
ThrowIfFieldsHasDifferentTypes (Object, Object)
Check if validated fields has same types.
protected static void ThrowIfFieldsHasDifferentTypes(object value, object anotherValue)
Parameters
value Object<br>
Current value.
anotherValue Object<br>
Value of another field.
Exceptions
ValuesHasDifferentTypesException<br> If fields has different types/
GetFieldValue<T> (String, ValidationContext)
Get field value by name.
protected static T GetFieldValue<T>(string fieldName, ValidationContext context)
Type Parameters
T<br>
Expected type of value.
Parameters
fieldName String<br>
Field name.
context ValidationContext<br>
Validation context.
Returns
T<br> Value of field.
Exceptions
MissingFieldException<br> Then field or property not exists in type.
GetDisplayNameForProperty (String, ValidationContext)
Return display name by field name.
protected static string GetDisplayNameForProperty(string fieldName, ValidationContext context)
Parameters
fieldName String<br>
Name of field.
context ValidationContext<br>
Validation context.
Returns
String<br> Return display name of field.
Exceptions
MissingFieldException<br> Then field or property not exists in type.
AddListAsOneArgumentToMessageArgs (Object[])
Add attribute argument to error message argument like one argument.
protected void AddListAsOneArgumentToMessageArgs(Object[] args)
Parameters
args Object[]<br>
Arguments what need add.
UnsupportedTypeException
Namespace: Zhomfr.Validation.Rules.Abstractions.Errors
Exception what indicate what rule can't process type of value.
public class UnsupportedTypeException : System.Exception, System.Runtime.Serialization.ISerializable
Inheritance Object → Exception → UnsupportedTypeException<br> Implements ISerializable
Properties
TargetSite
public MethodBase? TargetSite { get; }
Property Value
MethodBase<br>
Message
public virtual string Message { get; }
Property Value
String<br>
Data
public virtual IDictionary Data { get; }
Property Value
IDictionary<br>
InnerException
public Exception? InnerException { get; }
Property Value
Exception<br>
HelpLink
public virtual string? HelpLink { get; set; }
Property Value
String<br>
Source
public virtual string? Source { get; set; }
Property Value
String<br>
HResult
public int HResult { get; set; }
Property Value
Int32<br>
StackTrace
public virtual string? StackTrace { get; }
Property Value
String<br>
Constructors
UnsupportedTypeException (Type)
Exception what indicate what rule can't process type of value.
public UnsupportedTypeException(Type type)
Parameters
type Type<br>
Type of value what can't be processed.
Events
SerializeObjectState
Caution
BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.
protected event EventHandler<SafeSerializationEventArgs>? SerializeObjectState;
ValuesHasDifferentTypesException
Namespace: Zhomfr.Validation.Rules.Abstractions.Errors
Exception what indicates what 2 fields what validated has different type when rule expect same types.
public class ValuesHasDifferentTypesException : System.Exception, System.Runtime.Serialization.ISerializable
Inheritance Object → Exception → ValuesHasDifferentTypesException<br> Implements ISerializable
Properties
TargetSite
public MethodBase? TargetSite { get; }
Property Value
MethodBase<br>
Message
public virtual string Message { get; }
Property Value
String<br>
Data
public virtual IDictionary Data { get; }
Property Value
IDictionary<br>
InnerException
public Exception? InnerException { get; }
Property Value
Exception<br>
HelpLink
public virtual string? HelpLink { get; set; }
Property Value
String<br>
Source
public virtual string? Source { get; set; }
Property Value
String<br>
HResult
public int HResult { get; set; }
Property Value
Int32<br>
StackTrace
public virtual string? StackTrace { get; }
Property Value
String<br>
Constructors
ValuesHasDifferentTypesException (String)
Exception what indicates what 2 fields what validated has different type when rule expect same types.
public ValuesHasDifferentTypesException(string message = "The validated values has different types.")
Parameters
message String<br>
Custom error message.
Events
SerializeObjectState
Caution
BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.
protected event EventHandler<SafeSerializationEventArgs>? SerializeObjectState;
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Zhomfr.Validation.Rules.Abstractions:
| Package | Downloads |
|---|---|
|
Zhomfr.Validation.Rules
A powerful, intuitive collection of custom validation attributes built directly on top of the standard .NET System.ComponentModel.DataAnnotations infrastructure. |
|
|
Zhomfr.Validation.Rules.Database
A powerful, intuitive collection of custom validation attributes built directly on top of the standard .NET System.ComponentModel.DataAnnotations infrastructure. |
|
|
Zhomfr.Validation.Rules.Files
A powerful, intuitive collection of custom validation attributes built directly on top of the standard .NET System.ComponentModel.DataAnnotations infrastructure. |
GitHub repositories
This package is not used by any popular GitHub repositories.