DataAnnotatedModelValidations 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DataAnnotatedModelValidations --version 1.0.0
                    
NuGet\Install-Package DataAnnotatedModelValidations -Version 1.0.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="DataAnnotatedModelValidations" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DataAnnotatedModelValidations" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DataAnnotatedModelValidations" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DataAnnotatedModelValidations --version 1.0.0
                    
#r "nuget: DataAnnotatedModelValidations, 1.0.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.
#:package DataAnnotatedModelValidations@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DataAnnotatedModelValidations&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DataAnnotatedModelValidations&version=1.0.0
                    
Install as a Cake Tool

DataAnnotatedModelValidations

Data Annotated Model Validation Middleware for HotChocolate.

The purpose of this Middleware is to provide the same behavior like a ASP.Net controllers where all models would be validated according to the specified Data Annotations or the IValidatableObject implementation; in essence it's always on.

In addition individual method arguments can be validated using annotations from System.ComponentModel.Annotations.

Usage

Locate your GraphQL Server registration and append .AddDataAnnotationsValidator()

ex.

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    // ...
    services
        .AddGraphQLServer()
        .AddDataAnnotationsValidator()
        .AddQueryType<Query>();
    // ...
}

Excluding a model from being validated

To exlude a certain method argument from being validated just add the IgnoreModelValidation attribute.

ex.

public class Sample
{
    [Required]
    [MinLength(3)]
    [EmailAddress]
    public string? Email { get; set; }
}

public class Query
{
    public string? GetTextIgnoreValidation([IgnoreModelValidation][MinLength(5)] string? text) => text;

    public Sample? GetSampleIgnoreValidation([IgnoreModelValidation] Sample? sample) => sample;
}

Notes

When implementing the IValidatableObject interface HotChocolate considers the Validate as a resolver; to avoid getting schema errors said method needs to be ignored.

ex.

public class Sample : IValidatableObject
{
    [Required]
    [MinLength(3)]
    [EmailAddress]
    public string? Email { get; set; }

    [GraphQLIgnore]
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) =>
        Email?.StartsWith("no-property-name") == true
            ? new[] { new ValidationResult("no-property-name") }
            : Enumerable.Empty<ValidationResult>();
}

Similar Projects

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
8.1.2 8,023 4/27/2025
8.1.1 6,676 2/19/2025
8.1.0 412 2/17/2025
8.0.1 418 2/16/2025
8.0.0 342 2/15/2025
7.0.0 2,033 2/7/2025
6.3.0 14,577 12/25/2024
6.2.0 5,341 12/4/2024
6.1.0 2,217 11/16/2024
6.0.0 7,429 10/16/2024
5.2.0 70,881 3/15/2024
5.1.0 36,370 11/21/2023
5.0.0 587 11/21/2023
4.2.0 3,047 10/26/2023
4.1.0 17,833 8/22/2023
4.0.2 58,960 5/5/2023
4.0.1 49,185 3/10/2023
4.0.0 4,853 2/13/2023
3.0.1 7,861 1/15/2023
3.0.0 369 1/15/2023
2.1.4 356,309 10/22/2022
2.1.3 556 10/20/2022
2.1.2 458 10/20/2022
2.1.1 588 10/19/2022
2.1.0 98,981 1/11/2022
2.0.1 4,488 10/10/2021
2.0.0 402 10/1/2021
1.5.3 16,932 8/30/2021
1.5.2 432 8/11/2021
1.5.1 584 7/6/2021
1.5.0 696 6/10/2021
1.4.0 593 4/21/2021
1.3.0 458 4/17/2021
1.2.1 434 4/15/2021
1.2.0 424 4/13/2021
1.1.0 538 3/30/2021
1.0.0 713 3/11/2021