Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeAttributeでの排他的な範囲指定に対応 #825

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Beutl.Core/Validation/RangeDataAnnotationValidater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,27 @@ public RangeDataAnnotationValidater(RangeAttribute attribute)

public override bool TryCoerce(ValidationContext context, ref TNumber value)
{
value = TNumber.Clamp(value, Minimum, Maximum);
return true;
switch ((Attribute?.MaximumIsExclusive, Attribute?.MinimumIsExclusive))
{
case (true, true):
return false;

case (true, false):
value = TNumber.Max(value, Minimum);
return true;

case (false, true):
value = TNumber.Min(value, Maximum);
return true;

case (false, false):
value = TNumber.Clamp(value, Minimum, Maximum);
value = TNumber.Min(value, Maximum);
return true;

default:
return false;
}
}

public override string? Validate(ValidationContext context, TNumber value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public sealed class TupleRangeDataAnnotationValidater<TTuple, TNumber> : RangeVa
{
public TupleRangeDataAnnotationValidater(RangeAttribute attribute)
{
if (attribute.MaximumIsExclusive || attribute.MinimumIsExclusive)
throw new NotSupportedException("'MaximumIsExclusive' or 'MinimumIsExclusive' cannot be set to True.");

Attribute = attribute;
if (Attribute.OperandType == typeof(TTuple)
&& Attribute.Maximum is string maximumStr
Expand Down