Qowaiv.Validation.DataAnnotations 1.4.0

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

// Install Qowaiv.Validation.DataAnnotations as a Cake Tool
#tool nuget:?package=Qowaiv.Validation.DataAnnotations&version=1.4.0

Qowaiv DataAnnotations based validation

Provides an data annotations based implementation of the Qowaiv.Validation.Abstractions.IValidator and data annotation attributes.

Annotated model validator

The AnnotatedModelValidator validates a model using its data annotations. It returns a Result<TModel> of the validated model:

var validator = new AnnotatedModelValidator<SomeModel>();
Result<SomeModel> result = validator.Validate(model);

Validation messages

Qowaiv.Validation.DataAnnotations.ValidationMessage inherits Microsoft's ValidationResult, and implements Qowaiv.Validation.Abstractions.IValidationMessage. This allows the creation of messages with different severities:

var none = ValidationMessage.None;
var info = ValidationMessage.Info(message, args);
var warn = ValidationMessage.Warning(message, args);
var error = ValidationMessage.Error(message, args);

Validation attributes

Multiple [System.ComponentModel.DataAnnotations.Validation] attributes can be used to decorate models.

Mandatory

The [Required] attribute does not work for value types. The [Mandatory] attribute does. The default value of the struct is not valid. It also is not valid for the Unknown value, unless that is explicitly allowed.

public class Model
{
    [Mandatory(AllowUnknownValue = true)]
    public EmailAddress Email { get; set; }

    [Mandatory()]
    public string SomeString { get; set; }
}

Any

The [Required] attribute does not work (well) for collections. The [Any] attribute does. It is only valid if the collection contains at least one item.

public class Model
{
    [Any()]
    public List<int> Numbers { get; set; }
}

Allowed values

The [AllowedValues] attribute allows to define a subset of allowed values. It supports type converters to get the allowed values based on a string value.

public class Model
{
    [AllowedValues("DE", "FR", "GB")]
    public Country CountryOfBirth { get; set; }
}

Forbidden values

The [ForbiddenValues] attribute allows to define a subset of forbidden values. It supports type converters to get the forbidden values based on a string value.

public class Model
{
    [ForbiddenValues("US", "IR")]
    public Country CountryOfBirth { get; set; }
}

Defined enum values only

The [DefinedEnumValuesOnly] attribute limits the allowed values to defined enums only. By default it supports all possible combinations of defined enums when dealing with flags, but that can be restricted by setting OnlyAllowDefinedFlagsCombinations to true.

public class Model
{
    [DefinedEnumValuesOnly(OnlyAllowDefinedFlagsCombinations = false)]
    public SomeEnum CountryOfBirth { get; set; }
}

Distinct values

The [DistinctValues] attribute validates that all items of the collection are distinct. If needed, a custom IEqualityComparer comparer can be defined.

public class Model
{
    [DistinctValues(typeof(CustomEqualityComparer))]
    public IEnumerable<int> Numbers { get; set; }
}

Multiple of

The [MultipleOf] attribute validates that the value of a field is a multiple of the specified factor..

public class Model
{
    [MultipleOf(0.001)]
    public Amount Total { get; set; }
}

Optional

The [Optional] attribute indicates explicitly that a field is optional.

public class Model
{
    [Optional]
    public string? Message { get; set; }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  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 is compatible.  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.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Qowaiv.Validation.DataAnnotations:

Package Downloads
Qowaiv.Validation.TestTools

Qowaiv Validation supports a generic interface for (domain) model validation.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 11,891 8/4/2023
1.3.0 18,787 3/7/2023
1.0.1 14,576 11/8/2022
1.0.0 5,596 7/12/2022
0.2.4 4,030 6/29/2022
0.2.3 428 6/28/2022
0.2.2 6,089 5/13/2022
0.2.1 10,530 4/13/2022
0.2.0 7,597 12/31/2021
0.1.1 13,281 9/10/2021
0.1.0 544 8/31/2021
0.0.4 2,340 7/2/2021
0.0.3 5,768 5/26/2020
0.0.2 3,049 11/1/2019
0.0.1 1,566 7/30/2019

v1.4.0
- Introduced [MultipleOf] validation attribute. #69
- Marked [NestedModel] attribute as obsolete. #70
v1.3.0
- Support .NET 7.0. #62
v1.0.1
- Inaccessible properties do not crash but validate as invalid #58
- Indexed properties are ignored
v1.0.0
- Properties are considered validatable objects without [NestedModel] attribute #55
- Removed unused properties from NestedModel
v0.2.4
- Roll back of change #53
v0.2.3
- All models are considered to be nested. #52
- Fix crash on validation models with properties with generic types. #53
v0.2.2
- Mandatory attribute supports Dutch messages. #51
v0.2.1
- Nullable anotations. #48
v0.2.0
- Updated Qowaiv dependency.
v0.1.1
- Decorate pure methods with attribute (#38)
v0.1.0
- Qowaiv.Validation.Abstractions dependency
v0.0.4
- Fix double dots in paths for nested properties.