-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2d5647
commit 4c0a01e
Showing
15 changed files
with
259 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
@@ -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() | ||
|
@@ -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"); | ||
} | ||
} | ||
} |