Skip to content

Commit

Permalink
Fixed minimum and maximum validation not working (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Jul 24, 2021
1 parent 9c5d53a commit a73995e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Sharprompt/Forms/ListForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ internal class ListForm<T> : FormBase<IEnumerable<T>>
{
public ListForm(ListOptions<T> options)
{
if (options.Minimum < 0)
{
throw new ArgumentOutOfRangeException(nameof(options.Minimum), $"The minimum ({options.Minimum}) is not valid");
}

if (options.Maximum < options.Minimum)
{
throw new ArgumentException($"The maximum ({options.Maximum}) is not valid when minimum is set to ({options.Minimum})", nameof(options.Maximum));
}

_options = options;

_inputItems.AddRange(options.DefaultValues ?? Enumerable.Empty<T>());
Expand Down
10 changes: 8 additions & 2 deletions Sharprompt/Forms/MultiSelectForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ internal class MultiSelectForm<T> : FormBase<IEnumerable<T>>
public MultiSelectForm(MultiSelectOptions<T> options)
: base(false)
{
// throw early when invalid options are passed
if (options.Minimum < 0)
{
throw new ArgumentOutOfRangeException(nameof(options.Minimum), $"The minimum ({options.Minimum}) is not valid");
Expand Down Expand Up @@ -62,7 +61,14 @@ protected override bool TryGetResult(out IEnumerable<T> result)
}
else
{
_selectedItems.Add(currentItem);
if (_selectedItems.Count >= _options.Maximum)
{
Renderer.SetValidationResult(new ValidationResult($"A maximum selection of {_options.Maximum} items is required"));
}
else
{
_selectedItems.Add(currentItem);
}
}

break;
Expand Down
4 changes: 1 addition & 3 deletions Sharprompt/Internal/EastAsianWidth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ private static bool IsFullWidth(int codePoint)
continue;
}

var end = range.Start + range.Count;

if (codePoint > end)
if (codePoint > range.Start + range.Count)
{
left = middle + 1;

Expand Down
2 changes: 1 addition & 1 deletion Sharprompt/MultiSelectOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MultiSelectOptions<T>

public int? PageSize { get; set; }

public int Minimum { get; set; } = 0;
public int Minimum { get; set; } = 1;

public int Maximum { get; set; } = int.MaxValue;

Expand Down
4 changes: 4 additions & 0 deletions Sharprompt/Prompt.Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public static IEnumerable<T> MultiSelect<T>(string message, int? pageSize = null
Items = items,
DefaultValues = defaultValues?.Select(x => (EnumValue<T>)x),
PageSize = pageSize,
Minimum = minimum,
Maximum = maximum,
TextSelector = x => x.DisplayName
};

Expand All @@ -192,6 +194,8 @@ public static IEnumerable<T> MultiSelect<T>(string message, IEnumerable<T> items
Items = items,
DefaultValues = defaultValues,
PageSize = pageSize,
Minimum = minimum,
Maximum = maximum,
TextSelector = x => x.ToString()
};

Expand Down

0 comments on commit a73995e

Please sign in to comment.