public override bool TryGetValue(CommandParameterContext context, out object result) { if (context.Value == null) { result = _value; return(true); } result = null; return(false); }
public override ValidationResult Validate(CommandParameterContext context) { return(PossibleValues.Contains(context.Value as string) ? ValidationResult.Success() : ValidationResult.Error( string.Format( "Value '{0}' for {1} is not valid. Valid values are: {2}", context.Value, context.Parameter.PropertyName, string.Join(", ", PossibleValues) ) )); }
public override ValidationResult Validate(CommandParameterContext context) { if (context.Value is int integer) { if (integer > 0) { return(ValidationResult.Success()); } return(ValidationResult.Error($"Number is not greater than 0 ({context.Parameter.PropertyName}).")); } throw new InvalidOperationException($"Parameter is not a number ({context.Parameter.PropertyName})."); }
public static void ValidateParameter(CommandParameter parameter, CommandValueLookup settings, ITypeResolver resolver) { var assignedValue = settings.GetValue(parameter); foreach (var validator in parameter.Validators) { var context = new CommandParameterContext(parameter, resolver, assignedValue); var validationResult = validator.Validate(context); if (!validationResult.Successful) { // If there is an error message specified in the parameter validator attribute, // then use that one, otherwise use the validation result. var result = string.IsNullOrWhiteSpace(validator.ErrorMessage) ? validationResult : ValidationResult.Error(validator.ErrorMessage); throw CommandRuntimeException.ValidationFailed(result); } } }
/// <summary> /// Validates the parameter value. /// </summary> /// <param name="context">The parameter context.</param> /// <returns>The validation result.</returns> public abstract ValidationResult Validate(CommandParameterContext context);
public static CommandValueLookup GetParameterValues(CommandTree?tree, ITypeResolver resolver) { var lookup = new CommandValueLookup(); var binder = new CommandValueBinder(lookup); CommandValidator.ValidateRequiredParameters(tree); while (tree != null) { // Process unmapped parameters. foreach (var parameter in tree.Unmapped) { // Got a value provider? if (parameter.ValueProvider != null) { var context = new CommandParameterContext(parameter, resolver, null); if (parameter.ValueProvider.TryGetValue(context, out var result)) { result = ConvertValue(resolver, lookup, binder, parameter, result); lookup.SetValue(parameter, result); CommandValidator.ValidateParameter(parameter, lookup, resolver); continue; } } if (parameter.IsFlagValue()) { // Set the flag value to an empty, not set instance. var instance = Activator.CreateInstance(parameter.ParameterType); lookup.SetValue(parameter, instance); } else { // Is this an option with a default value? if (parameter.DefaultValue != null) { var value = parameter.DefaultValue?.Value; value = ConvertValue(resolver, lookup, binder, parameter, value); binder.Bind(parameter, resolver, value); CommandValidator.ValidateParameter(parameter, lookup, resolver); } else if (Nullable.GetUnderlyingType(parameter.ParameterType) != null || !parameter.ParameterType.IsValueType) { lookup.SetValue(parameter, null); } } } // Process mapped parameters. foreach (var mapped in tree.Mapped) { if (mapped.Parameter.WantRawValue) { // Just try to assign the raw value. binder.Bind(mapped.Parameter, resolver, mapped.Value); } else { if (mapped.Parameter.IsFlagValue() && mapped.Value == null) { if (mapped.Parameter is CommandOption option && option.DefaultValue != null) { // Set the default value. binder.Bind(mapped.Parameter, resolver, option.DefaultValue?.Value); } else { // Set the flag but not the value. binder.Bind(mapped.Parameter, resolver, null); } }
/// <summary> /// Gets a value for the parameter. /// </summary> /// <param name="context">The parameter context.</param> /// <param name="result">The resulting value.</param> /// <returns><c>true</c> if a value was provided; otherwise, <c>false</c>.</returns> public abstract bool TryGetValue(CommandParameterContext context, out object?result);