SHA256
forked from ADOFAIEditorForum/ADOFAIRenderer
79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
namespace ADOFAIRenderer.Data;
|
|
|
|
/// <summary>
|
|
/// Base class for double input validation limits.
|
|
/// Multiple limits can be composed on a single input field — all are checked in order.
|
|
/// </summary>
|
|
internal abstract class DoubleLimit
|
|
{
|
|
/// <summary>
|
|
/// Validates the given double value against this limit.
|
|
/// </summary>
|
|
/// <param name="value">The parsed double value to validate.</param>
|
|
/// <param name="errorMessage">
|
|
/// When validation fails, set to a human-readable error message.
|
|
/// When validation succeeds, set to null.
|
|
/// </param>
|
|
/// <returns>true if the value satisfies this limit; false otherwise.</returns>
|
|
public abstract bool Validate(double value, out string errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Requires the double value to be strictly positive (> 0).
|
|
/// </summary>
|
|
internal class PositiveOnlyDoubleLimit : DoubleLimit
|
|
{
|
|
public override bool Validate(double value, out string errorMessage)
|
|
{
|
|
if (value > 0)
|
|
{
|
|
errorMessage = null!;
|
|
return true;
|
|
}
|
|
|
|
errorMessage = "Value must be a positive number greater than 0.";
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Requires the double value to be within an inclusive [Min, Max] range.
|
|
/// Either bound can be null to represent an unbounded side.
|
|
/// </summary>
|
|
internal class RangeDoubleLimit : DoubleLimit
|
|
{
|
|
// ReSharper disable MemberCanBePrivate.Global
|
|
public double? Min { get; }
|
|
public double? Max { get; }
|
|
// ReSharper restore MemberCanBePrivate.Global
|
|
|
|
/// <param name="min">Inclusive lower bound, or null for no lower bound.</param>
|
|
/// <param name="max">Inclusive upper bound, or null for no upper bound.</param>
|
|
public RangeDoubleLimit(double? min, double? max)
|
|
{
|
|
Min = min;
|
|
Max = max;
|
|
}
|
|
|
|
public override bool Validate(double value, out string errorMessage)
|
|
{
|
|
if (value < Min)
|
|
{
|
|
errorMessage = Max.HasValue
|
|
? $"Value must be a number between {Min.Value} and {Max.Value}."
|
|
: $"Value must be a number greater than or equal to {Min.Value}.";
|
|
return false;
|
|
}
|
|
|
|
if (value > Max)
|
|
{
|
|
errorMessage = Min.HasValue
|
|
? $"Value must be a number between {Min.Value} and {Max.Value}."
|
|
: $"Value must be a number less than or equal to {Max.Value}.";
|
|
return false;
|
|
}
|
|
|
|
errorMessage = null!;
|
|
return true;
|
|
}
|
|
} |