| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | |
| | | 6 | | namespace dotnet_etcd.DependencyInjection; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Validates etcd client options. |
| | | 10 | | /// </summary> |
| | | 11 | | internal static class EtcdClientOptionsValidator |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Validates the specified options. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="options">The options to validate.</param> |
| | | 17 | | /// <exception cref="ArgumentNullException">Thrown if options is null.</exception> |
| | | 18 | | /// <exception cref="ArgumentException">Thrown if options is invalid.</exception> |
| | | 19 | | public static void ValidateOptions(EtcdClientOptions options) |
| | 20 | 20 | | { |
| | 20 | 21 | | ArgumentNullException.ThrowIfNull(options); |
| | | 22 | | |
| | 19 | 23 | | if (string.IsNullOrWhiteSpace(options.ConnectionString)) |
| | 2 | 24 | | { |
| | 2 | 25 | | throw new ArgumentException("ConnectionString must be provided", nameof(options)); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | // Validate port is in a valid range |
| | 17 | 29 | | if (options.Port is <= 0 or > 65535) |
| | 3 | 30 | | { |
| | 3 | 31 | | throw new ArgumentException($"Port must be between 1 and 65535, but was {options.Port}", nameof(options)); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | // Validate server name if using static hosts format |
| | 14 | 35 | | if (options.ConnectionString.StartsWith("static://", StringComparison.OrdinalIgnoreCase) && |
| | 14 | 36 | | string.IsNullOrWhiteSpace(options.ServerName)) |
| | 2 | 37 | | { |
| | 2 | 38 | | throw new ArgumentException("ServerName must be provided when using static:// connection string format", |
| | 2 | 39 | | nameof(options)); |
| | | 40 | | } |
| | 12 | 41 | | } |
| | | 42 | | } |