Graph.ArgumentValidator
1.0.0
See the version list below for details.
dotnet add package Graph.ArgumentValidator --version 1.0.0
NuGet\Install-Package Graph.ArgumentValidator -Version 1.0.0
<PackageReference Include="Graph.ArgumentValidator" Version="1.0.0" />
paket add Graph.ArgumentValidator --version 1.0.0
#r "nuget: Graph.ArgumentValidator, 1.0.0"
// Install Graph.ArgumentValidator as a Cake Addin #addin nuget:?package=Graph.ArgumentValidator&version=1.0.0 // Install Graph.ArgumentValidator as a Cake Tool #tool nuget:?package=Graph.ArgumentValidator&version=1.0.0
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 below steps.
Steps for configuring validator
Step 1
Go to StartUp.cs file and inside ConfigureServices make the following changes.
Step 2
Keep this code to validate all inputs of GraphQL Query/Mutation.
ValidatorSettings.ValidateAllInputs = true;
Step 3
Add argument validator to services by referring to below code
services
.AddGraphQLServer()
.AddArgumentValidator();
Additional Configuration
If you don't want to validate all inputs then you can skip step 2 and follow the optional step i.e
Just add the Validatable
attribute to the class u defined for input.
Ex:
using System.ComponentModel.DataAnnotations;
using Graph.ArgumentValidator;
namespace Demo
{
[Validatable]
public class AddUserInput
{
[Required, MinLength(4, ErrorMessage = "Username must be atleast 4 characters.")]
public string UserName { get; set; }
[Required, RegularExpression(@"^[^@\s]+@[^@\s]+\.[^@\s]+$", 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; }
}
}
When the user is given wrong values to input, this is how the response from GraphQL Server will look like
{
"errors": [
{
"message": "Username must be at least 4 characters."
},
{
"message": "Password needs to be more strong."
}
]
}
Product | Versions 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. |
-
.NETCoreApp 3.1
- HotChocolate.Execution (>= 11.0.9)
-
.NETStandard 2.0
- HotChocolate.Execution (>= 11.0.9)
-
net5.0
- HotChocolate.Execution (>= 11.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.