Skip to content

Commit

Permalink
Update readme & add unit test condition
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Mar 6, 2022
1 parent be3506c commit 6d9eaf5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
22 changes: 1 addition & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,11 @@ Widget 'MiniValidation' is valid!
### Web app (.NET 6)
```csharp
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using MiniValidation;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.MapGet("/", () => "Hello World");

app.MapGet("/widgets", () =>
Expand All @@ -93,23 +85,11 @@ app.MapGet("/widgets", () =>
app.MapGet("/widgets/{name}", (string name) =>
new Widget { Name = name });

// Example calling MiniValidator.TryValidate
app.MapPost("/widgets", (Widget widget) =>
!MiniValidator.TryValidate(widget, out var errors)
? Results.BadRequest(errors)
? Results.ValidationProblem(errors)
: Results.Created($"/widgets/{widget.Name}", widget));

// Example using Validated<T> paramater binder
app.MapPost("/widgets-validated", (Validated<Widget> input) =>
{
var (widget, isValid, errors) = input;
return !isValid || widget == null
? input.DefaultBindingResultStatusCode.HasValue
? Results.StatusCode(input.DefaultBindingResultStatusCode.Value)
: Results.BadRequest(errors)
: Results.Created($"/widgets/{widget.Name}", widget);
});

app.Run();

class Widget
Expand Down
4 changes: 4 additions & 0 deletions tests/MiniValidation.UnitTests/TryValidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public static IEnumerable<object?[]> PrimitiveValues
new object[] { true },
new object[] { new DateTime(2021, 01, 01) },
new object[] { new DateTimeOffset(2021, 01, 01, 0, 0, 0, TimeSpan.FromHours(1)) },
#if NET6_0_OR_GREATER
new object[] { new DateOnly(2021, 01, 01) },
new object[] { new TimeOnly(0, 0) },
#endif
new object[] { StringComparison.OrdinalIgnoreCase },
new object?[] { new int?(1) },
};
Expand Down

0 comments on commit 6d9eaf5

Please sign in to comment.