request/src/request.validation.tests/ValidationSeverityTests.cs
Louis Seubert f7263c33a8
fix(validation): make Severity meaningful in Validation.IsValid
Validation.IsValid previously counted every problem regardless of severity,
leaving Severity inert. Now IsValid only fails on Error problems, so Warning
and Info no longer invalidate the result, and IsValidFor(Severity) lets
callers set the failing-severity threshold.
2026-07-12 21:25:21 +02:00

49 lines
1.7 KiB
C#

// Copyright (c) The Geekeey Authors
// SPDX-License-Identifier: EUPL-1.2
namespace Geekeey.Request.Validation.Tests;
internal sealed class ValidationSeverityTests
{
[Test]
public async Task I_can_have_warning_problems_not_invalidate_by_default()
{
var validator = new PropertyValidator<Person, string?>(person
=> person.Name, rule => rule.Must(value => !string.IsNullOrWhiteSpace(value), "Name is required.")
.WithSeverity(Severity.Warning));
var result = validator.Validate(new Person { Name = "" });
await Assert.That(result.Problems).Count().IsEqualTo(1);
await Assert.That(result.IsValid).IsTrue();
await Assert.That(result.IsValidFor(Severity.Warning)).IsFalse();
await Assert.That(result.IsValidFor(Severity.Info)).IsFalse();
}
[Test]
public async Task I_can_have_info_problems_only_invalidate_at_info_threshold()
{
var validator = new PropertyValidator<Person, string?>(person
=> person.Name, rule => rule.Must(value => !string.IsNullOrWhiteSpace(value), "Name is required.")
.WithSeverity(Severity.Info));
var result = validator.Validate(new Person { Name = "" });
await Assert.That(result.Problems).Count().IsEqualTo(1);
await Assert.That(result.IsValid).IsTrue();
await Assert.That(result.IsValidFor(Severity.Warning)).IsTrue();
await Assert.That(result.IsValidFor(Severity.Info)).IsFalse();
}
[Test]
public async Task I_can_have_error_problems_invalidate_by_default()
{
var validator = new PropertyValidator<Person, string?>(person
=> person.Name, rule => rule.Must(value => !string.IsNullOrWhiteSpace(value), "Name is required."));
var result = validator.Validate(new Person { Name = "" });
await Assert.That(result.IsValid).IsFalse();
await Assert.That(result.IsValidFor(Severity.Error)).IsFalse();
}
}