Skip to content

Commit

Permalink
加入驗證器
Browse files Browse the repository at this point in the history
  • Loading branch information
YuChia-Wei committed May 6, 2024
1 parent 571aba5 commit 7915bb6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/dotnetLab.WebApi/Controllers/SampleController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using dotnetLab.UseCase.SimpleDocument.Commands;
using dotnetLab.UseCase.SimpleDocument.Queries;
using dotnetLab.WebApi.Controllers.Validator;
using dotnetLab.WebApi.Infrastructure.Attributes;
using Mediator;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -29,6 +31,7 @@ public SampleController(IMediator mediator)
/// <param name="dataCommand"></param>
/// <returns></returns>
[HttpGet]
[ParameterValidator<SimpleDocQueryValidator>]
// [Authorize]
public async Task<IActionResult> Get([FromQuery] SimpleDocQuery dataCommand)
{
Expand All @@ -41,6 +44,7 @@ public async Task<IActionResult> Get([FromQuery] SimpleDocQuery dataCommand)
/// <param name="dataCommand"></param>
/// <returns></returns>
[HttpPost]
[ParameterValidator<InputSimpleDocumentCommandValidator>]
// [Authorize(nameof(LoginUserRequestedPolicy))]
public async Task<IActionResult> Post([FromBody] InputSimpleDocumentCommand dataCommand)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using dotnetLab.UseCase.SimpleDocument.Commands;
using FluentValidation;

namespace dotnetLab.WebApi.Controllers.Validator;

/// <summary>
/// InputSimpleDocumentCommand 的參數驗證器
/// </summary>
public class InputSimpleDocumentCommandValidator : AbstractValidator<InputSimpleDocumentCommand>
{
public InputSimpleDocumentCommandValidator()
{
this.RuleFor(o => o.DocumentNum)
.NotEmpty();

this.RuleFor(o => o.Description)
.NotEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using dotnetLab.UseCase.SimpleDocument.Queries;
using FluentValidation;

namespace dotnetLab.WebApi.Controllers.Validator;

/// <summary>
/// SimpleDocQuery 的參數驗證器
/// </summary>
public class SimpleDocQueryValidator : AbstractValidator<SimpleDocQuery>
{
public SimpleDocQueryValidator()
{
this.RuleFor(o => o.SerialId)
.NotEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using FluentValidation;

namespace dotnetLab.WebApi.Infrastructure.Attributes;

/// <summary>
/// 參數驗證器 Attribute
/// 參數驗證器 (泛型版本,可利用泛型型別限制避免誤用)
/// </summary>
/// <seealso cref="Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute" />
public class ParameterValidatorAttribute<T> : ParameterValidatorAttribute
/// <typeparam name="TValidator"></typeparam>
public class ParameterValidatorAttribute<TValidator> : ParameterValidatorAttribute
where TValidator : IValidator
{
/// <summary>
/// Initializes a new instance of the
Expand All @@ -15,7 +18,7 @@ public class ParameterValidatorAttribute<T> : ParameterValidatorAttribute
/// </summary>
/// <param name="parameterName">參數名稱</param>
public ParameterValidatorAttribute(string? parameterName = null)
: base(typeof(T), parameterName)
: base(typeof(TValidator), parameterName)
{
}
}

0 comments on commit 7915bb6

Please sign in to comment.