feat: OptionsValidatorBase<TOptions>
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.Configuration.Tests;
|
||||
|
||||
public sealed class OptionsValidatorBaseTests
|
||||
{
|
||||
private sealed class SampleOptions
|
||||
{
|
||||
public int Port { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
private sealed class SampleValidator : OptionsValidatorBase<SampleOptions>
|
||||
{
|
||||
protected override void Validate(ValidationBuilder v, SampleOptions o)
|
||||
{
|
||||
v.Port(o.Port, "Sample:Port");
|
||||
v.Required(o.Name, "Sample:Name");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Success_when_clean()
|
||||
{
|
||||
var r = new SampleValidator().Validate(null, new SampleOptions { Port = 8080, Name = "ok" });
|
||||
Assert.True(r.Succeeded);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fails_and_reports_all_failures()
|
||||
{
|
||||
var r = new SampleValidator().Validate(null, new SampleOptions { Port = 0, Name = "" });
|
||||
Assert.True(r.Failed);
|
||||
Assert.Equal(2, r.Failures!.Count());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user