Add FluentValidation to application services
TaxBaik CI/CD / build-and-deploy (push) Successful in 2m45s

This commit is contained in:
2026-07-07 16:31:29 +09:00
parent 68cc97eda6
commit c54599304b
9 changed files with 110 additions and 38 deletions
@@ -1,5 +1,6 @@
namespace TaxBaik.Application.Tests;
using FluentValidation;
using TaxBaik.Application.DTOs;
using TaxBaik.Application.Services;
using TaxBaik.Domain.Entities;
@@ -12,9 +13,9 @@ public class BlogServiceTests
[Fact]
public async Task CreateAsync_WhenPublishedWithoutSeoTitle_ThrowsValidationException()
{
var service = new BlogService(new FakeBlogPostRepository(), new MemoryCache(new MemoryCacheOptions()));
var service = new BlogService(new FakeBlogPostRepository(), new MemoryCache(new MemoryCacheOptions()), new PassThroughValidator<CreateBlogPostDto>());
await Assert.ThrowsAsync<ValidationException>(() => service.CreateAsync(new CreateBlogPostDto
await Assert.ThrowsAsync<TaxBaik.Application.Services.ValidationException>(() => service.CreateAsync(new CreateBlogPostDto
{
Title = "테스트 포스트",
Content = "본문",
@@ -33,7 +34,7 @@ public class BlogServiceTests
new BlogPost { Id = 1, Title = "같은 제목", Content = "본문", Slug = "같은-제목" }
]
};
var service = new BlogService(repository, new MemoryCache(new MemoryCacheOptions()));
var service = new BlogService(repository, new MemoryCache(new MemoryCacheOptions()), new PassThroughValidator<CreateBlogPostDto>());
var post = await service.CreateAsync(new CreateBlogPostDto
{
@@ -54,7 +55,7 @@ public class BlogServiceTests
new BlogPost { Id = 1, Title = "삭제 대상", Content = "본문", Slug = "delete-me", IsPublished = true }
]
};
var service = new BlogService(repository, new MemoryCache(new MemoryCacheOptions()));
var service = new BlogService(repository, new MemoryCache(new MemoryCacheOptions()), new PassThroughValidator<CreateBlogPostDto>());
await service.DeleteAsync(1);
@@ -129,4 +130,14 @@ public class BlogServiceTests
public Task IncrementViewCountAsync(int id, CancellationToken cancellationToken = default) => Task.CompletedTask;
}
private sealed class PassThroughValidator<T> : IValidator<T>
{
public FluentValidation.Results.ValidationResult Validate(T instance) => new();
public Task<FluentValidation.Results.ValidationResult> ValidateAsync(T instance, CancellationToken cancellation = default) => Task.FromResult(new FluentValidation.Results.ValidationResult());
public FluentValidation.Results.ValidationResult Validate(IValidationContext context) => new();
public Task<FluentValidation.Results.ValidationResult> ValidateAsync(IValidationContext context, CancellationToken cancellation = default) => Task.FromResult(new FluentValidation.Results.ValidationResult());
public IValidatorDescriptor CreateDescriptor() => throw new NotImplementedException();
public bool CanValidateInstancesOfType(Type type) => true;
}
}