Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
seungyongshim committed Feb 17, 2024
1 parent fd5aa3b commit 98de6f6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/WebApplicationMinimalApi8/Dto/MessageDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using FluentValidation;

namespace WebApplicationMinimalApi8.Dto;

public record MessageDto
{
/// <summary>
///
/// </summary>
/// <example>안녕하세요</example>
public required string Body { get; init; }

}

public class MessageDtoValidator : AbstractValidator<MessageDto>
{
public MessageDtoValidator()
{
RuleFor(x => x.Body).NotEmpty();
}
}
35 changes: 33 additions & 2 deletions src/WebApplicationMinimalApi8/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using Microsoft.Extensions.Hosting;
using System.Reflection.Metadata.Ecma335;
using FluentValidation;
using Microsoft.OpenApi.Models;
using WebApplicationMinimalApi8.Dto;
using WebApplicationMinimalApi8.ExceptionHandlers;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
builder.AddFluentValidationEndpointFilter();

if (builder.Environment.IsEnvironment("Best"))
{
builder.Services.AddExceptionHandler<BadRequestExceptionHandler>();
Expand All @@ -21,7 +27,32 @@
{
app.UseExceptionHandler();
}


app.MapPost("/validate", (MessageDto message) => message)
.WithDescription("메시지를 검증합니다.")

.AddFluentValidationFilter()
.WithOpenApi(x =>
{
x.RequestBody = new OpenApiRequestBody
{
Content =
{
["application/json"] = new OpenApiMediaType
{
Schema = new OpenApiSchema
{
Reference = new OpenApiReference
{
Id = "MessageDto",
Type = ReferenceType.Schema
}
}
}
}
};
return x;
});

app.MapGet("/500", () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.9.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="ForEvolve.FluentValidation.AspNetCore.Http" Version="1.0.26" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
Expand Down

0 comments on commit 98de6f6

Please sign in to comment.