H3rm.Validation 6.0.1

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

// Install H3rm.Validation as a Cake Tool
#tool nuget:?package=H3rm.Validation&version=6.0.1

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
6.0.1 135 10/8/2023
6.0.0 115 10/8/2023