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.
This commit is contained in:
Louis Seubert 2026-07-12 18:50:06 +02:00
commit f7263c33a8
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD
4 changed files with 75 additions and 3 deletions

View file

@ -0,0 +1,49 @@
// 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();
}
}