Skip to content

Commit 427dfaa

Browse files
committed
adjust validation function
1 parent 450b852 commit 427dfaa

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

pkgs/sdk/server/src/ConfigurationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public sealed class ConfigurationBuilder
5959
/// </summary>
6060
private void SetSdkKeyIfValid(string sdkKey)
6161
{
62-
if (ValidationUtils.ValidateSdkKeyFormat(sdkKey) == null)
62+
if (ValidationUtils.IsValidSdkKeyFormat(sdkKey))
6363
{
6464
_sdkKey = sdkKey;
6565
}

pkgs/shared/common/src/Helpers/ValidationUtils.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,39 @@ namespace LaunchDarkly.Sdk.Helpers
99
public static class ValidationUtils
1010
{
1111
private static readonly Regex ValidCharsRegex = new Regex("^[-a-zA-Z0-9._]+\\z");
12+
private const int MaxSdkKeyLength = 8192;
1213

1314
/// <summary>
14-
/// Validates that a string does not contain invalid characters or exceed the max length of 8192 characters.
15+
/// Validates that a string does not contain invalid characters and is not too long for our systems.
1516
/// </summary>
1617
/// <param name="sdkKey">the SDK key to validate.</param>
1718
/// <returns>Null if the input is valid, otherwise an error string describing the issue.</returns>
18-
public static string ValidateSdkKeyFormat(string sdkKey)
19+
public static bool IsValidSdkKeyFormat(string sdkKey, out string errorMessage)
1920
{
2021

2122
// For offline mode, we allow a null or empty SDK key and it is not invalid.
2223
if (string.IsNullOrEmpty(sdkKey))
2324
{
24-
return null;
25+
return true;
2526
}
2627

27-
if (sdkKey.Length > 8192)
28+
if (sdkKey.Length > MaxSdkKeyLength)
2829
{
29-
return "SDK key cannot be longer than 1024 characters.";
30+
errorMessage = $"SDK key cannot be longer than {MaxSdkKeyLength} characters.";
31+
return false;
3032
}
3133

3234
if (!ValidCharsRegex.IsMatch(sdkKey))
3335
{
34-
return "SDK key contains invalid characters.";
36+
errorMessage = "SDK key contains invalid characters.";
37+
return false;
3538
}
3639

37-
return null;
40+
return true;
3841
}
3942

4043
/// <summary>
41-
/// Validates that a string is non-empty, not too longer for our systems, and only contains
44+
/// Validates that a string is non-empty, not too long for our systems, and only contains
4245
/// alphanumeric characters, hyphens, periods, and underscores.
4346
/// </summary>
4447
/// <param name="s">the string to validate.</param>

0 commit comments

Comments
 (0)