From 4c0a01e53c45e99ef1a5ba634cf6fb946c603d2b Mon Sep 17 00:00:00 2001 From: Varun Teja <35555010+VarunSaiTeja@users.noreply.github.com> Date: Thu, 10 Mar 2022 10:41:05 +0530 Subject: [PATCH] added sample --- Graph.ArgumentValidator.sln | 45 +++++++++++++++++--- Sample/Program.cs | 20 +++++++++ Sample/Properties/launchSettings.json | 30 +++++++++++++ Sample/Sample.csproj | 16 +++++++ Sample/Startup.cs | 52 +++++++++++++++++++++++ Sample/appsettings.Development.json | 9 ++++ Sample/appsettings.json | 10 +++++ Shared/DuplicateEmailValidatorService.cs | 18 ++++++++ Shared/DuplicateEmailValidtorAttribute.cs | 14 ++++++ Shared/MyInput.cs | 12 ++++++ Shared/Query.cs | 18 ++++++++ Shared/Shared.csproj | 11 +++++ src/Graph.ArgumentValidator.csproj | 2 +- test/Graph.ArgumentValidator.Tests.csproj | 11 +++-- test/ValidationTests.cs | 52 ++--------------------- 15 files changed, 259 insertions(+), 61 deletions(-) create mode 100644 Sample/Program.cs create mode 100644 Sample/Properties/launchSettings.json create mode 100644 Sample/Sample.csproj create mode 100644 Sample/Startup.cs create mode 100644 Sample/appsettings.Development.json create mode 100644 Sample/appsettings.json create mode 100644 Shared/DuplicateEmailValidatorService.cs create mode 100644 Shared/DuplicateEmailValidtorAttribute.cs create mode 100644 Shared/MyInput.cs create mode 100644 Shared/Query.cs create mode 100644 Shared/Shared.csproj diff --git a/Graph.ArgumentValidator.sln b/Graph.ArgumentValidator.sln index fe60a34..1211292 100644 --- a/Graph.ArgumentValidator.sln +++ b/Graph.ArgumentValidator.sln @@ -1,11 +1,15 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26124.0 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32228.430 MinimumVisualStudioVersion = 15.0.26124.0 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Graph.ArgumentValidator", "src\Graph.ArgumentValidator.csproj", "{59435C05-1155-47A5-9361-2702B320EB83}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Graph.ArgumentValidator", "src\Graph.ArgumentValidator.csproj", "{59435C05-1155-47A5-9361-2702B320EB83}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Graph.ArgumentValidator.Tests", "test\Graph.ArgumentValidator.Tests.csproj", "{F31CE4C8-0B88-492B-9AE2-92D731D51B88}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Graph.ArgumentValidator.Tests", "test\Graph.ArgumentValidator.Tests.csproj", "{F31CE4C8-0B88-492B-9AE2-92D731D51B88}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "Sample\Sample.csproj", "{F7DE9337-9B11-4126-B6BD-233DA24E5EA1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -16,9 +20,6 @@ Global Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {59435C05-1155-47A5-9361-2702B320EB83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {59435C05-1155-47A5-9361-2702B320EB83}.Debug|Any CPU.Build.0 = Debug|Any CPU @@ -44,5 +45,35 @@ Global {F31CE4C8-0B88-492B-9AE2-92D731D51B88}.Release|x64.Build.0 = Release|Any CPU {F31CE4C8-0B88-492B-9AE2-92D731D51B88}.Release|x86.ActiveCfg = Release|Any CPU {F31CE4C8-0B88-492B-9AE2-92D731D51B88}.Release|x86.Build.0 = Release|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Debug|x64.ActiveCfg = Debug|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Debug|x64.Build.0 = Debug|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Debug|x86.ActiveCfg = Debug|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Debug|x86.Build.0 = Debug|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Release|Any CPU.Build.0 = Release|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Release|x64.ActiveCfg = Release|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Release|x64.Build.0 = Release|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Release|x86.ActiveCfg = Release|Any CPU + {F7DE9337-9B11-4126-B6BD-233DA24E5EA1}.Release|x86.Build.0 = Release|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Debug|x64.ActiveCfg = Debug|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Debug|x64.Build.0 = Debug|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Debug|x86.ActiveCfg = Debug|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Debug|x86.Build.0 = Debug|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Release|Any CPU.Build.0 = Release|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Release|x64.ActiveCfg = Release|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Release|x64.Build.0 = Release|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Release|x86.ActiveCfg = Release|Any CPU + {60CAB6E7-8015-4A6C-91CB-EDAB13A09F3D}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F48721F7-2E9A-45D3-B934-6F1C154AB522} EndGlobalSection EndGlobal diff --git a/Sample/Program.cs b/Sample/Program.cs new file mode 100644 index 0000000..b288b19 --- /dev/null +++ b/Sample/Program.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; + +namespace Sample +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/Sample/Properties/launchSettings.json b/Sample/Properties/launchSettings.json new file mode 100644 index 0000000..5a0ac48 --- /dev/null +++ b/Sample/Properties/launchSettings.json @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:23573", + "sslPort": 44334 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Sample": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Sample/Sample.csproj b/Sample/Sample.csproj new file mode 100644 index 0000000..62e28ce --- /dev/null +++ b/Sample/Sample.csproj @@ -0,0 +1,16 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + diff --git a/Sample/Startup.cs b/Sample/Startup.cs new file mode 100644 index 0000000..41338b1 --- /dev/null +++ b/Sample/Startup.cs @@ -0,0 +1,52 @@ +using Graph.ArgumentValidator; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Shared; + +namespace Sample +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + services.AddGraphQLServer() + .AddArgumentValidator() + .AddQueryType(); + + services.AddSingleton(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseHttpsRedirection(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + endpoints.MapGraphQL(); + }); + } + } +} diff --git a/Sample/appsettings.Development.json b/Sample/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/Sample/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/Sample/appsettings.json b/Sample/appsettings.json new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ b/Sample/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/Shared/DuplicateEmailValidatorService.cs b/Shared/DuplicateEmailValidatorService.cs new file mode 100644 index 0000000..63bfb0c --- /dev/null +++ b/Shared/DuplicateEmailValidatorService.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; + +namespace Shared +{ + public class DuplicateEmailValidatorService + { + public bool IsEmailExist(string newEmail) + { + var existingEmails = new List + { + "varun@gmail.com", + "teja@gmail.com" + }; + + return !existingEmails.Contains(newEmail); + } + } +} diff --git a/Shared/DuplicateEmailValidtorAttribute.cs b/Shared/DuplicateEmailValidtorAttribute.cs new file mode 100644 index 0000000..84af9c3 --- /dev/null +++ b/Shared/DuplicateEmailValidtorAttribute.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; + +namespace Shared +{ + public class DuplicateEmailValidtorAttribute : ValidationAttribute + { + protected override ValidationResult IsValid(object valueObj, ValidationContext validationContext) + { + var value = valueObj as string; + var service = (DuplicateEmailValidatorService)validationContext.GetService(typeof(DuplicateEmailValidatorService)); + return service.IsEmailExist(value) ? ValidationResult.Success : new ValidationResult("Email already exist"); + } + } +} diff --git a/Shared/MyInput.cs b/Shared/MyInput.cs new file mode 100644 index 0000000..df924fc --- /dev/null +++ b/Shared/MyInput.cs @@ -0,0 +1,12 @@ +using Graph.ArgumentValidator; +using System.ComponentModel.DataAnnotations; + +namespace Shared +{ + [Validatable] + public class MyInput + { + [EmailAddress, Required] + public string Email { get; set; } + } +} diff --git a/Shared/Query.cs b/Shared/Query.cs new file mode 100644 index 0000000..06ffb85 --- /dev/null +++ b/Shared/Query.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace Shared +{ + public class Query + { + public string ArgIsEmail([EmailAddress, Required] string email) => email; + + /// + /// Gives validation failed result if email already exist. Other wise *You are good to go...* + /// + /// + /// + public string CheckDuplicateEmail([EmailAddress, Required][DuplicateEmailValidtor] string email) => "You are good to go, this email not registred yet."; + + public string ArgIsInput(MyInput input) => input.Email; + } +} diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj new file mode 100644 index 0000000..8425cf5 --- /dev/null +++ b/Shared/Shared.csproj @@ -0,0 +1,11 @@ + + + + netcoreapp3.1 + + + + + + + diff --git a/src/Graph.ArgumentValidator.csproj b/src/Graph.ArgumentValidator.csproj index ff10b59..04424eb 100644 --- a/src/Graph.ArgumentValidator.csproj +++ b/src/Graph.ArgumentValidator.csproj @@ -12,7 +12,7 @@ - + diff --git a/test/Graph.ArgumentValidator.Tests.csproj b/test/Graph.ArgumentValidator.Tests.csproj index 1759264..c3c7d45 100644 --- a/test/Graph.ArgumentValidator.Tests.csproj +++ b/test/Graph.ArgumentValidator.Tests.csproj @@ -7,21 +7,24 @@ - - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all + + + + diff --git a/test/ValidationTests.cs b/test/ValidationTests.cs index 619b5eb..7293a47 100644 --- a/test/ValidationTests.cs +++ b/test/ValidationTests.cs @@ -1,13 +1,12 @@ using HotChocolate; using HotChocolate.Execution; using Microsoft.Extensions.DependencyInjection; +using Shared; using Snapshooter.Xunit; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Xunit; -namespace Graph.ArgumentValidator +namespace Graph.ArgumentValidator.Tests { public class ValidationTests { @@ -19,7 +18,7 @@ public class ValidationTests static async Task ExecuteRequest(string request) { var resonse = await new ServiceCollection() - .AddScoped(_ => new DataValidatorService()) + .AddScoped(_ => new DuplicateEmailValidatorService()) .AddGraphQL() .AddQueryType() .AddArgumentValidator() @@ -76,49 +75,4 @@ public async Task Ensure_Validation_Works_On_NonDuplicateEmail() result.MatchSnapshot(); } } - - public class Query - { - public string ArgIsEmail([EmailAddress] string email) => email; - - /// - /// Gives validation failed result if email already exist. Other wise *You are good to go...* - /// - /// - /// - public string CheckDuplicateEmail([EmailAddress][DuplicateEmailValidtor] string email) => "You are good to go, this email not registred yet."; - - public string ArgIsInput(MyInput input) => input.Email; - } - - [Validatable] - public class MyInput - { - [EmailAddress] - public string Email { get; set; } - } - - public class DataValidatorService - { - public bool IsEmailExist(string newEmail) - { - var existingEmails = new List - { - "varun@gmail.com", - "teja@gmail.com" - }; - - return !existingEmails.Contains(newEmail); - } - } - - public class DuplicateEmailValidtorAttribute : ValidationAttribute - { - protected override ValidationResult IsValid(object valueObj, ValidationContext validationContext) - { - var value = valueObj as string; - var service = (DataValidatorService)validationContext.GetService(typeof(DataValidatorService)); - return service.IsEmailExist(value) ? ValidationResult.Success : new ValidationResult("Email already exist"); - } - } }