From 7915bb6b343af2eb05c32c34b3fcd6c111376946 Mon Sep 17 00:00:00 2001 From: YuChia Date: Mon, 6 May 2024 23:59:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E9=A9=97=E8=AD=89=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/SampleController.cs | 4 ++++ .../InputSimpleDocumentCommandValidator.cs | 19 +++++++++++++++++++ .../Validator/SimpleDocQueryValidator.cs | 16 ++++++++++++++++ .../ParameterValidatorAttribute_Type.cs | 11 +++++++---- 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/dotnetLab.WebApi/Controllers/Validator/InputSimpleDocumentCommandValidator.cs create mode 100644 src/dotnetLab.WebApi/Controllers/Validator/SimpleDocQueryValidator.cs diff --git a/src/dotnetLab.WebApi/Controllers/SampleController.cs b/src/dotnetLab.WebApi/Controllers/SampleController.cs index b870e73..aed01fa 100644 --- a/src/dotnetLab.WebApi/Controllers/SampleController.cs +++ b/src/dotnetLab.WebApi/Controllers/SampleController.cs @@ -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; @@ -29,6 +31,7 @@ public SampleController(IMediator mediator) /// /// [HttpGet] + [ParameterValidator] // [Authorize] public async Task Get([FromQuery] SimpleDocQuery dataCommand) { @@ -41,6 +44,7 @@ public async Task Get([FromQuery] SimpleDocQuery dataCommand) /// /// [HttpPost] + [ParameterValidator] // [Authorize(nameof(LoginUserRequestedPolicy))] public async Task Post([FromBody] InputSimpleDocumentCommand dataCommand) { diff --git a/src/dotnetLab.WebApi/Controllers/Validator/InputSimpleDocumentCommandValidator.cs b/src/dotnetLab.WebApi/Controllers/Validator/InputSimpleDocumentCommandValidator.cs new file mode 100644 index 0000000..0435d9b --- /dev/null +++ b/src/dotnetLab.WebApi/Controllers/Validator/InputSimpleDocumentCommandValidator.cs @@ -0,0 +1,19 @@ +using dotnetLab.UseCase.SimpleDocument.Commands; +using FluentValidation; + +namespace dotnetLab.WebApi.Controllers.Validator; + +/// +/// InputSimpleDocumentCommand 的參數驗證器 +/// +public class InputSimpleDocumentCommandValidator : AbstractValidator +{ + public InputSimpleDocumentCommandValidator() + { + this.RuleFor(o => o.DocumentNum) + .NotEmpty(); + + this.RuleFor(o => o.Description) + .NotEmpty(); + } +} \ No newline at end of file diff --git a/src/dotnetLab.WebApi/Controllers/Validator/SimpleDocQueryValidator.cs b/src/dotnetLab.WebApi/Controllers/Validator/SimpleDocQueryValidator.cs new file mode 100644 index 0000000..4346872 --- /dev/null +++ b/src/dotnetLab.WebApi/Controllers/Validator/SimpleDocQueryValidator.cs @@ -0,0 +1,16 @@ +using dotnetLab.UseCase.SimpleDocument.Queries; +using FluentValidation; + +namespace dotnetLab.WebApi.Controllers.Validator; + +/// +/// SimpleDocQuery 的參數驗證器 +/// +public class SimpleDocQueryValidator : AbstractValidator +{ + public SimpleDocQueryValidator() + { + this.RuleFor(o => o.SerialId) + .NotEmpty(); + } +} \ No newline at end of file diff --git a/src/dotnetLab.WebApi/Infrastructure/Attributes/ParameterValidatorAttribute_Type.cs b/src/dotnetLab.WebApi/Infrastructure/Attributes/ParameterValidatorAttribute_Type.cs index 92e10a9..b231af0 100644 --- a/src/dotnetLab.WebApi/Infrastructure/Attributes/ParameterValidatorAttribute_Type.cs +++ b/src/dotnetLab.WebApi/Infrastructure/Attributes/ParameterValidatorAttribute_Type.cs @@ -1,10 +1,13 @@ +using FluentValidation; + namespace dotnetLab.WebApi.Infrastructure.Attributes; /// -/// 參數驗證器 Attribute +/// 參數驗證器 (泛型版本,可利用泛型型別限制避免誤用) /// -/// -public class ParameterValidatorAttribute : ParameterValidatorAttribute +/// +public class ParameterValidatorAttribute : ParameterValidatorAttribute + where TValidator : IValidator { /// /// Initializes a new instance of the @@ -15,7 +18,7 @@ public class ParameterValidatorAttribute : ParameterValidatorAttribute /// /// 參數名稱 public ParameterValidatorAttribute(string? parameterName = null) - : base(typeof(T), parameterName) + : base(typeof(TValidator), parameterName) { } } \ No newline at end of file