Graph.ArgumentValidator 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Graph.ArgumentValidator --version 1.0.1                
NuGet\Install-Package Graph.ArgumentValidator -Version 1.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="Graph.ArgumentValidator" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Graph.ArgumentValidator --version 1.0.1                
#r "nuget: Graph.ArgumentValidator, 1.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 Graph.ArgumentValidator as a Cake Addin
#addin nuget:?package=Graph.ArgumentValidator&version=1.0.1

// Install Graph.ArgumentValidator as a Cake Tool
#tool nuget:?package=Graph.ArgumentValidator&version=1.0.1                

Adds support for validating input arguments in HotChocolate

Generally, we use attributes from System.ComponentModel.DataAnnotations for validating our input models in controllers.

As HotChocolate doesn't validate input arguments, After installing this package, By just adding 2 lines of code in your Startup.cs file. You will be adding support for validation to all input models in your Queries/Mutations.

Tech Note: You can use all validation attributes/rules from System.ComponentModel.DataAnnotations (ex: Required, MinLength, Regex etc). This package just adds a middleware to the hot chocolate resolver for validating your input models as configured in the below steps.

Steps for configuring validator

Step 1

Go to StartUp.cs file and inside ConfigureServices make the following changes.

Step 2

Add argument validator to services by referring to below code

services
  .AddGraphQLServer()
  .AddArgumentValidator();

Step 3

Just add the Validatable attribute to all the classes you defined for input.

Ex:

    using System.ComponentModel.DataAnnotations;
    using Graph.ArgumentValidator;
    
    namespace Demo
    {
        [Validatable]
        public class RegisterUserInput
        {
            [Required, MinLength(4, ErrorMessage = "Username must be atleast 4 characters.")]
            public string UserName { get; set; }

            [Required, EmailAddress(ErrorMessage = "Email Id format is invalid.")]
            public string Email { get; set; }

            [Required, RegularExpression(@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$", ErrorMessage = "Password needs to be more strong.")]
            public string Password { get; set; }
            
            [Compare(nameof(Password), ErrorMessage = "Passwords do not match!")]
            public string ConfirmPassword { get; set; }
        }
        
        public class UserMutations
        {
            public int RegisterUser(RegisterUserInput input, [Service] UserService userService)
            {
                return userService.RegisterUser(input);
            }
        }
    }

Another way of providing inline validation for Primitive data types

  using System.ComponentModel.DataAnnotations;
  using Graph.ArgumentValidator;
  
  namespace Demo
  {
      public class Mutations
      {
          public bool RegisterUserPhone([Required(ErrorMessage = "Email is required")string email,
              [Phone(ErrorMessage = "Invalid Phone Number")] string phone)
          {
              return userService.RegisterUserPhone(email,phone);
          }
      }
  }

When the user is given the following wrong values to the mutation input

     mutation{
      registerUser(input:{userName:"va2", password:"weak", confirmPassword:"strong", email:"varun"})
     }

This is the response we got from GraphQL Server

{
  "errors": [
    {
      "message": "Username must be atleast 4 characters.",
      "path": [
        "input"
      ],
      "extensions": {
        "field": "userName"
      }
    },
    {
      "message": "Email Id format is invalid.",
      "path": [
        "input"
      ],
      "extensions": {
        "field": "email"
      }
    },
    {
      "message": "Password needs to be more strong.",
      "path": [
        "input"
      ],
      "extensions": {
        "field": "password"
      }
    },
    {
      "message": "Passwords do not match!",
      "path": [
        "input"
      ],
      "extensions": {
        "field": "confirmPassword"
      }
    }
  ]
}
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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.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

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
3.0.0 11,353 11/7/2023
2.1.2 39,593 6/19/2022
2.1.1 16,169 3/10/2022
2.1.0 4,064 12/3/2021
2.0.0 7,950 10/10/2021
1.0.1 42,464 3/18/2021
1.0.0 887 3/6/2021