Skip to content

Commit

Permalink
added sample
Browse files Browse the repository at this point in the history
  • Loading branch information
VarunSaiTeja committed Mar 10, 2022
1 parent d2d5647 commit 4c0a01e
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 61 deletions.
45 changes: 38 additions & 7 deletions Graph.ArgumentValidator.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
20 changes: 20 additions & 0 deletions Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Startup>();
});
}
}
30 changes: 30 additions & 0 deletions Sample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
16 changes: 16 additions & 0 deletions Sample/Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HotChocolate.AspNetCore" Version="12.6.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>


</Project>
52 changes: 52 additions & 0 deletions Sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -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<Query>();

services.AddSingleton<DuplicateEmailValidatorService>();
}

// 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();
});
}
}
}
9 changes: 9 additions & 0 deletions Sample/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
10 changes: 10 additions & 0 deletions Sample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
18 changes: 18 additions & 0 deletions Shared/DuplicateEmailValidatorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace Shared
{
public class DuplicateEmailValidatorService
{
public bool IsEmailExist(string newEmail)
{
var existingEmails = new List<string>
{
"[email protected]",
"[email protected]"
};

return !existingEmails.Contains(newEmail);
}
}
}
14 changes: 14 additions & 0 deletions Shared/DuplicateEmailValidtorAttribute.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
12 changes: 12 additions & 0 deletions Shared/MyInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Graph.ArgumentValidator;
using System.ComponentModel.DataAnnotations;

namespace Shared
{
[Validatable]
public class MyInput
{
[EmailAddress, Required]
public string Email { get; set; }
}
}
18 changes: 18 additions & 0 deletions Shared/Query.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;

namespace Shared
{
public class Query
{
public string ArgIsEmail([EmailAddress, Required] string email) => email;

/// <summary>
/// Gives validation failed result if email already exist. Other wise *You are good to go...*
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
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;
}
}
11 changes: 11 additions & 0 deletions Shared/Shared.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Graph.ArgumentValidator.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Graph.ArgumentValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HotChocolate.Execution" Version="12.3.2" />
<PackageReference Include="HotChocolate.Execution" Version="12.6.2" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 7 additions & 4 deletions test/Graph.ArgumentValidator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../src/Graph.ArgumentValidator.csproj" />
<PackageReference Include="Snapshooter.Xunit" Version="0.6.1" />
<PackageReference Include="Snapshooter.Xunit" Version="0.7.1" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

</Project>
52 changes: 3 additions & 49 deletions test/ValidationTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -19,7 +18,7 @@ public class ValidationTests
static async Task<string> ExecuteRequest(string request)
{
var resonse = await new ServiceCollection()
.AddScoped(_ => new DataValidatorService())
.AddScoped(_ => new DuplicateEmailValidatorService())
.AddGraphQL()
.AddQueryType<Query>()
.AddArgumentValidator()
Expand Down Expand Up @@ -76,49 +75,4 @@ public async Task Ensure_Validation_Works_On_NonDuplicateEmail()
result.MatchSnapshot();
}
}

public class Query
{
public string ArgIsEmail([EmailAddress] string email) => email;

/// <summary>
/// Gives validation failed result if email already exist. Other wise *You are good to go...*
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
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<string>
{
"[email protected]",
"[email protected]"
};

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");
}
}
}

0 comments on commit 4c0a01e

Please sign in to comment.