-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
da32d12
commit a6f6c78
Showing
2 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using FluentValidation; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace SampleNSwagWebApi.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
public class FilesController : Controller | ||
{ | ||
[HttpPost("[action]")] | ||
public IActionResult UploadFile([FromForm] UploadFileRequest request) | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
|
||
return Ok(); | ||
} | ||
} | ||
|
||
#region Model | ||
|
||
public class UploadFileRequest | ||
{ | ||
[FromForm(Name = "File")] | ||
public IFormFile File { get; set; } | ||
} | ||
|
||
#endregion | ||
|
||
#region Validation | ||
|
||
public class UploadFileRequestValidator : AbstractValidator<UploadFileRequest> | ||
{ | ||
public UploadFileRequestValidator() | ||
{ | ||
RuleFor(x => x.File) | ||
.Cascade(CascadeMode.Stop) | ||
.NotEmpty(); | ||
} | ||
} | ||
|
||
#endregion | ||
} |
46 changes: 46 additions & 0 deletions
46
test/MicroElements.Swashbuckle.FluentValidation.Tests/IFormFileTests.cs
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,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using FluentValidation; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using Xunit; | ||
|
||
namespace MicroElements.Swashbuckle.FluentValidation.Tests | ||
{ | ||
public class IFormFileTests : UnitTestBase | ||
{ | ||
public class UploadFileRequest | ||
{ | ||
[FromForm(Name = "File")] | ||
public IFormFile File { get; set; } | ||
} | ||
|
||
public class UploadFileRequestValidator : AbstractValidator<UploadFileRequest> | ||
{ | ||
public UploadFileRequestValidator() | ||
{ | ||
RuleFor(c => c.File) | ||
.Cascade(CascadeMode.Stop) | ||
.NotEmpty(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void all_rules_should_be_applied() | ||
{ | ||
var schemaRepository = new SchemaRepository(); | ||
var referenceSchema = SchemaGenerator(new UploadFileRequestValidator()).GenerateSchema(typeof(UploadFileRequest), schemaRepository); | ||
|
||
var schema = schemaRepository.Schemas[referenceSchema.Reference.Id]; | ||
var fileProperty = schema.Properties[nameof(UploadFileRequest.File)]; | ||
fileProperty.Type.Should().Be("string"); | ||
fileProperty.Format.Should().Be("binary"); | ||
fileProperty.Nullable.Should().Be(false); | ||
} | ||
} | ||
} |