|
3 | 3 |
|
4 | 4 | using System.Collections.ObjectModel;
|
5 | 5 | using System.Diagnostics;
|
| 6 | +using System.Globalization; |
6 | 7 | using System.Runtime.CompilerServices;
|
7 | 8 | using System.Threading.Channels;
|
8 | 9 | using Microsoft.Extensions.Logging;
|
@@ -234,25 +235,77 @@ private async Task<bool> RunValidationAsync(Interaction interactionState, Intera
|
234 | 235 | // State could be null if the user dismissed the inputs dialog. There is nothing to validate in this situation.
|
235 | 236 | if (result.State is IReadOnlyList<InteractionInput> inputs)
|
236 | 237 | {
|
237 |
| - var options = (InputsDialogInteractionOptions)interactionState.Options; |
| 238 | + foreach (var input in inputs) |
| 239 | + { |
| 240 | + input.ValidationErrors.Clear(); |
| 241 | + } |
| 242 | + |
| 243 | + var context = new InputsDialogValidationContext |
| 244 | + { |
| 245 | + CancellationToken = cancellationToken, |
| 246 | + ServiceProvider = _serviceProvider, |
| 247 | + Inputs = inputsInfo.Inputs |
| 248 | + }; |
238 | 249 |
|
239 |
| - if (options.ValidationCallback is { } validationCallback) |
| 250 | + foreach (var input in inputs) |
240 | 251 | {
|
241 |
| - foreach (var input in inputs) |
| 252 | + var value = input.Value = input.Value?.Trim(); |
| 253 | + |
| 254 | + if (string.IsNullOrEmpty(value)) |
242 | 255 | {
|
243 |
| - input.ValidationErrors.Clear(); |
| 256 | + if (input.Required) |
| 257 | + { |
| 258 | + context.AddValidationError(input, "Value is required."); |
| 259 | + } |
244 | 260 | }
|
245 |
| - |
246 |
| - var context = new InputsDialogValidationContext |
| 261 | + else |
247 | 262 | {
|
248 |
| - CancellationToken = cancellationToken, |
249 |
| - ServiceProvider = _serviceProvider, |
250 |
| - Inputs = inputsInfo.Inputs |
251 |
| - }; |
252 |
| - await validationCallback(context).ConfigureAwait(false); |
| 263 | + switch (input.InputType) |
| 264 | + { |
| 265 | + case InputType.Text: |
| 266 | + case InputType.SecretText: |
| 267 | + var maxLength = InteractionHelpers.GetMaxLength(input.MaxLength); |
| 268 | + |
| 269 | + if (value.Length > maxLength) |
| 270 | + { |
| 271 | + context.AddValidationError(input, $"Value length exceeds {maxLength} characters."); |
| 272 | + } |
| 273 | + break; |
| 274 | + case InputType.Choice: |
| 275 | + if (!input.Options?.Any(o => o.Key == value) ?? true) |
| 276 | + { |
| 277 | + context.AddValidationError(input, "Value must be one of the provided options."); |
| 278 | + } |
| 279 | + break; |
| 280 | + case InputType.Boolean: |
| 281 | + if (!bool.TryParse(value, out _)) |
| 282 | + { |
| 283 | + context.AddValidationError(input, "Value must be a valid boolean."); |
| 284 | + } |
| 285 | + break; |
| 286 | + case InputType.Number: |
| 287 | + if (!int.TryParse(value, CultureInfo.InvariantCulture, out _)) |
| 288 | + { |
| 289 | + context.AddValidationError(input, "Value must be a valid number."); |
| 290 | + } |
| 291 | + break; |
| 292 | + default: |
| 293 | + break; |
| 294 | + } |
| 295 | + } |
| 296 | + } |
253 | 297 |
|
254 |
| - return !context.HasErrors; |
| 298 | + // Only run validation callback if there are no data validation errors. |
| 299 | + if (!context.HasErrors) |
| 300 | + { |
| 301 | + var options = (InputsDialogInteractionOptions)interactionState.Options; |
| 302 | + if (options.ValidationCallback is { } validationCallback) |
| 303 | + { |
| 304 | + await validationCallback(context).ConfigureAwait(false); |
| 305 | + } |
255 | 306 | }
|
| 307 | + |
| 308 | + return !context.HasErrors; |
256 | 309 | }
|
257 | 310 | }
|
258 | 311 |
|
|
0 commit comments