-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathRangeValidator.cs
More file actions
30 lines (26 loc) · 1.19 KB
/
RangeValidator.cs
File metadata and controls
30 lines (26 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.CommandLine.Parsing;
namespace System.CommandLine.Validation;
/// <summary>
/// Validates that a value is within the specified bounds.
/// </summary>
public class RangeValidator : ValueValidator, IValueValidator
{
public RangeValidator() : base(nameof(ValueConditions.Range), typeof(ValueConditions.Range))
{ }
/// <inheritdoc/>
public override void Validate(object? value, CliValueSymbol valueSymbol,
CliValueResult? valueResult, ValueCondition valueCondition, ValidationContext validationContext)
{
if (valueCondition is IValueValidator valueValidator)
{
valueValidator.Validate(value, valueSymbol, valueResult, valueCondition, validationContext);
return;
}
if (valueCondition.MustHaveValidator)
{
validationContext.AddError(new CliDiagnostic(new("", "", "Range validator missing for {valueSymbol}", severity: CliDiagnosticSeverity.Error, null), [valueSymbol.Name], cliSymbolResult: valueResult));
}
}
}