H3rm.Validation
6.0.0
See the version list below for details.
dotnet add package H3rm.Validation --version 6.0.0
NuGet\Install-Package H3rm.Validation -Version 6.0.0
<PackageReference Include="H3rm.Validation" Version="6.0.0" />
paket add H3rm.Validation --version 6.0.0
#r "nuget: H3rm.Validation, 6.0.0"
// Install H3rm.Validation as a Cake Addin #addin nuget:?package=H3rm.Validation&version=6.0.0 // Install H3rm.Validation as a Cake Tool #tool nuget:?package=H3rm.Validation&version=6.0.0
ValidationAttributeBase Class
Overview
ValidationAttributeBase
is an abstract class located in the Validation
namespace. It serves as the base class for
validation attributes, extending the functionality of ValidationAttribute
from System.ComponentModel.DataAnnotations
.
Features
- ThrowException Property: Determines whether an exception is thrown when validation fails.
- ValidationCondition Method: Abstract method to be overridden by derived classes to provide specific condition logic.
- DefaultErrorMessage Property: Holds the default error message to be used when validation fails, allowing derived classes to provide specific error messages.
- IsValid Method: Evaluates the provided value against the condition and returns a
ValidationResult
that encapsulates the result of the validation.
Usage
Defining a Custom Validation Attribute
Create a derived class and override the ValidationCondition
to define the validation logic.
namespace Validation
{
public class CustomValidationAttribute : ValidationAttributeBase
{
protected override Func<string, bool> ValidationCondition => value => !string.IsNullOrEmpty(value);
protected override string DefaultErrorMessage => "Value cannot be null or empty";
}
}
Applying the Custom Validation Attribute
Apply the custom validation attribute to properties or fields of your classes.
public class SampleClass
{
[CustomValidation]
public string SampleProperty { get; set; }
}
Configuration
Set ThrowException
to true
if an exception should be thrown when the validation fails.
[CustomValidation(ThrowException = true)]
public string SampleProperty { get; set; }
API
Properties
- bool ThrowException { get; set; }
- string DefaultErrorMessage { get; }
Methods
- Func<string, bool> ValidationCondition { get; }
- ValidationResult IsValid(object value, ValidationContext validationContext)
Conclusion
ValidationAttributeBase
allows for the creation of custom validation attributes with configurable conditions, error
messages, and exception handling, providing enhanced validation capabilities in the .NET ecosystem.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- 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.