aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-16 18:53:07 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-16 18:53:07 -0500
commit74810d42ea08d9782bd83a02d06eaca9c2ecd9a5 (patch)
tree7d7f4e6f3a612e01e02f4ed761107ce3778590d6
parent772df82d0faf98f00fc8cc2ee59051e386eaa998 (diff)
downloadslskd-74810d42ea08d9782bd83a02d06eaca9c2ecd9a5.tar.gz
add StringAttribute to cover all string related validation
-rw-r--r--src/slskd/Common/Validation/StringAttribute.cs (renamed from src/slskd/Common/Validation/DisallowedCharactersAttribute.cs)75
-rw-r--r--tests/slskd.Tests.Unit/Common/Validation/DisallowedCharactersAttributeTests.cs254
-rw-r--r--tests/slskd.Tests.Unit/Common/Validation/StringAttributeTests.cs517
3 files changed, 565 insertions, 281 deletions
diff --git a/src/slskd/Common/Validation/DisallowedCharactersAttribute.cs b/src/slskd/Common/Validation/StringAttribute.cs
index 90ed3d81..2fcf4283 100644
--- a/src/slskd/Common/Validation/DisallowedCharactersAttribute.cs
+++ b/src/slskd/Common/Validation/StringAttribute.cs
@@ -1,4 +1,4 @@
-// <copyright file="DisallowedCharactersAttribute.cs" company="JP Dillingham">
+// <copyright file="StringAttribute.cs" company="JP Dillingham">
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
@@ -30,42 +30,63 @@
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
// </copyright>
-namespace slskd.Validation;
-
-using System.ComponentModel.DataAnnotations;
-using System.Linq;
-
-/// <summary>
-/// Validates that the value does not contain any of the <see cref="Disallowed"/> characters.
-/// </summary>
-public class DisallowedCharactersAttribute : ValidationAttribute
+namespace slskd.Validation
{
- public DisallowedCharactersAttribute(params char[] disallowed)
- {
- Disallowed = disallowed;
- }
+ using System.ComponentModel.DataAnnotations;
+ using System.Linq;
+ using System.Text.RegularExpressions;
/// <summary>
- /// Gets or sets a value indicating whether character matching is case-insensitive. Defaults to <see langword="true"/>.
+ /// Validates that the value is not null or whitespace.
/// </summary>
- public bool IgnoreCase { get; set; } = true;
-
- private char[] Disallowed { get; }
-
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
+ public class StringAttribute : ValidationAttribute
{
- if (value is not null and string str)
+ public bool AllowNull { get; set; } = true;
+ public bool AllowEmpty { get; set; } = true;
+ public bool AllowWhiteSpace { get; set; } = true;
+ public char[] DisallowedCharacters { get; set; } = [];
+ public int MinimumLength { get; set; } = 0;
+ public int MaximumLength { get; set; } = int.MaxValue;
+ public Regex Pattern { get; set; } = null;
+
+ protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
- var invalid = IgnoreCase
- ? str.Where(c => Disallowed.Any(d => char.ToUpperInvariant(c) == char.ToUpperInvariant(d)))
- : str.Where(c => Disallowed.Contains(c));
+ var val = (string)value;
+
+ if (!AllowNull && val is null)
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field must not be null");
+ }
+
+ if (!AllowEmpty && val is "")
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field must contain at least one character");
+ }
+
+ // check this after empty to avoid overlap
+ if (!AllowWhiteSpace && string.IsNullOrWhiteSpace(val))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field must not contain only whitespace");
+ }
+
+ if (val.Length < MinimumLength || val.Length > MaximumLength)
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field must be between {MinimumLength} and {MaximumLength} characters");
+ }
+
+ var invalid = val.Where(c => DisallowedCharacters.Any(d => char.ToUpperInvariant(c) == char.ToUpperInvariant(d)));
if (invalid.Any())
{
return new ValidationResult($"The {validationContext.DisplayName} field contains one or more disallowed characters: {string.Join(", ", invalid.Select(c => $"'{c}'"))}");
}
- }
- return ValidationResult.Success;
+ if (Pattern is not null && Pattern.IsMatch(val))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field must match the regular expression {Pattern.ToJson()}");
+ }
+
+ return ValidationResult.Success;
+ }
}
-} \ No newline at end of file
+}
diff --git a/tests/slskd.Tests.Unit/Common/Validation/DisallowedCharactersAttributeTests.cs b/tests/slskd.Tests.Unit/Common/Validation/DisallowedCharactersAttributeTests.cs
deleted file mode 100644
index 4654fd25..00000000
--- a/tests/slskd.Tests.Unit/Common/Validation/DisallowedCharactersAttributeTests.cs
+++ /dev/null
@@ -1,254 +0,0 @@
-using System.ComponentModel.DataAnnotations;
-using slskd.Validation;
-using Xunit;
-
-namespace slskd.Tests.Unit.Common.Validation;
-
-public class DisallowedCharactersAttributeTests
-{
- private static (bool IsValid, string ErrorMessage) Validate(string value, char[] disallowed, bool ignoreCase = true)
- {
- var attribute = new DisallowedCharactersAttribute(disallowed) { IgnoreCase = ignoreCase };
- var context = new ValidationContext(new object()) { DisplayName = "Field", MemberName = "Field" };
- var result = attribute.GetValidationResult(value, context);
- return (result == null, result?.ErrorMessage);
- }
-
- // null and empty pass — [Required] handles presence; this attribute only checks content
- [Fact]
- public void Null_Passes()
- {
- var (isValid, _) = Validate(null, ['!']);
- Assert.True(isValid);
- }
-
- [Fact]
- public void EmptyString_Passes()
- {
- var (isValid, _) = Validate(string.Empty, ['!']);
- Assert.True(isValid);
- }
-
- public class Passes
- {
- private static (bool IsValid, string ErrorMessage) Validate(string value, char[] disallowed, bool ignoreCase = true)
- => DisallowedCharactersAttributeTests.Validate(value, disallowed, ignoreCase);
-
- // Punctuation has no case variants — IgnoreCase must not affect the result either way
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void NoDisallowedCharsPresent_Passes_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("hello", ['!', '@'], ignoreCase);
- Assert.True(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void EmptyDisallowedSet_Passes_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("hello!@#", [], ignoreCase);
- Assert.True(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void Unicode_AllowedChars_Pass_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("пользователь/音楽", ['!', '@'], ignoreCase);
- Assert.True(isValid);
- }
-
- [Fact]
- public void NonString_Passes()
- {
- var attribute = new DisallowedCharactersAttribute('!');
- var context = new ValidationContext(new object()) { DisplayName = "Field", MemberName = "Field" };
- var result = attribute.GetValidationResult(42, context);
- Assert.Equal(ValidationResult.Success, result);
- }
-
- // IgnoreCase = false only: a disallowed letter in one case does not block the other case
- [Fact]
- public void CaseSensitive_LowerCaseDisallowed_DoesNotBlock_UpperCase()
- {
- var (isValid, _) = Validate("HELLO", ['h', 'e', 'l', 'o'], ignoreCase: false);
- Assert.True(isValid);
- }
-
- [Fact]
- public void CaseSensitive_UpperCaseDisallowed_DoesNotBlock_LowerCase()
- {
- var (isValid, _) = Validate("hello", ['H', 'E', 'L', 'O'], ignoreCase: false);
- Assert.True(isValid);
- }
- }
-
- public class Fails
- {
- private static (bool IsValid, string ErrorMessage) Validate(string value, char[] disallowed, bool ignoreCase = true)
- => DisallowedCharactersAttributeTests.Validate(value, disallowed, ignoreCase);
-
- // Punctuation has no case variants — IgnoreCase must not affect the result either way
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void SingleDisallowedChar_Fails_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("hello!", ['!'], ignoreCase);
- Assert.False(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void DisallowedCharAtStart_Fails_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("!hello", ['!'], ignoreCase);
- Assert.False(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void DisallowedCharAtEnd_Fails_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("hello!", ['!'], ignoreCase);
- Assert.False(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void DisallowedCharInMiddle_Fails_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("hel!lo", ['!'], ignoreCase);
- Assert.False(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void OneOfMultipleDisallowedCharsPresent_Fails_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("hello!", ['!', '@', '#'], ignoreCase);
- Assert.False(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void MultipleDisallowedCharsAllPresent_Fails_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("h!e@l#lo", ['!', '@', '#'], ignoreCase);
- Assert.False(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void DisallowedCharRepeated_Fails_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("he!lo!", ['!'], ignoreCase);
- Assert.False(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void AllCharsDisallowed_Fails_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("!@#", ['!', '@', '#'], ignoreCase);
- Assert.False(isValid);
- }
-
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void Unicode_DisallowedChar_Fails_Regardless_Of_IgnoreCase(bool ignoreCase)
- {
- var (isValid, _) = Validate("пользователь", ['е'], ignoreCase);
- Assert.False(isValid);
- }
-
- // IgnoreCase = true only: a disallowed letter blocks its other-case equivalent
- [Fact]
- public void IgnoreCase_LowerCaseDisallowed_Blocks_UpperCase()
- {
- var (isValid, _) = Validate("HELLO", ['h', 'e', 'l', 'o'], ignoreCase: true);
- Assert.False(isValid);
- }
-
- [Fact]
- public void IgnoreCase_UpperCaseDisallowed_Blocks_LowerCase()
- {
- var (isValid, _) = Validate("hello", ['H', 'E', 'L', 'O'], ignoreCase: true);
- Assert.False(isValid);
- }
-
- [Fact]
- public void CaseSensitive_ExactMatch_Fails()
- {
- var (isValid, _) = Validate("hello", ['h'], ignoreCase: false);
- Assert.False(isValid);
- }
- }
-
- public class ErrorMessage_Contains
- {
- private static (bool IsValid, string ErrorMessage) Validate(string value, char[] disallowed, bool ignoreCase = true)
- => DisallowedCharactersAttributeTests.Validate(value, disallowed, ignoreCase);
-
- [Fact]
- public void DisplayName()
- {
- var (_, errorMessage) = Validate("hello!", ['!']);
- Assert.Contains("Field", errorMessage);
- }
-
- [Fact]
- public void TheDisallowedChar()
- {
- var (_, errorMessage) = Validate("hello!", ['!']);
- Assert.Contains("'!'", errorMessage);
- }
-
- [Fact]
- public void AllPresentDisallowedChars()
- {
- var (_, errorMessage) = Validate("h!@l", ['!', '@']);
- Assert.Contains("'!'", errorMessage);
- Assert.Contains("'@'", errorMessage);
- }
-
- [Fact]
- public void EachOccurrenceOfRepeatedChar()
- {
- // "he!lo!" has two '!' — both occurrences are listed
- var (_, errorMessage) = Validate("he!lo!", ['!']);
- Assert.Equal(2, errorMessage.Split("'!'").Length - 1);
- }
-
- [Fact]
- public void OnlyCharsActuallyPresent_NotAllDisallowedChars()
- {
- // '@' is disallowed but not in the value — must not appear in the message
- var (_, errorMessage) = Validate("hello!", ['!', '@']);
- Assert.Contains("'!'", errorMessage);
- Assert.DoesNotContain("'@'", errorMessage);
- }
-
- [Fact]
- public void IgnoreCase_ShowsActualChar_NotDisallowedChar()
- {
- // 'a' is disallowed (lowercase); value contains 'A' (uppercase).
- // The message reports what the user supplied ('A'), not the disallowed definition ('a').
- var (_, errorMessage) = Validate("A", ['a'], ignoreCase: true);
- Assert.Contains("'A'", errorMessage);
- Assert.DoesNotContain("'a'", errorMessage);
- }
- }
-}
diff --git a/tests/slskd.Tests.Unit/Common/Validation/StringAttributeTests.cs b/tests/slskd.Tests.Unit/Common/Validation/StringAttributeTests.cs
new file mode 100644
index 00000000..dfa964f7
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/Validation/StringAttributeTests.cs
@@ -0,0 +1,517 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+using System.Text.RegularExpressions;
+using slskd.Validation;
+using Xunit;
+
+namespace slskd.Tests.Unit.Common.Validation;
+
+public class StringAttributeTests
+{
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ {
+ var attr = new StringAttribute();
+ configure?.Invoke(attr);
+ var context = new ValidationContext(new object()) { DisplayName = "Field", MemberName = "Field" };
+ var result = attr.GetValidationResult(value, context);
+ return (result == null, result?.ErrorMessage);
+ }
+
+ // empty string passes all default checks — [Required] handles presence
+ [Fact]
+ public void EmptyString_Passes()
+ {
+ var (isValid, _) = Validate(string.Empty);
+ Assert.True(isValid);
+ }
+
+ public class DisallowedCharacters
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, char[] disallowed)
+ => StringAttributeTests.Validate(value, a => a.DisallowedCharacters = disallowed);
+
+ public class Passes
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, char[] disallowed)
+ => DisallowedCharacters.Validate(value, disallowed);
+
+ [Fact]
+ public void EmptyString_Passes()
+ {
+ var (isValid, _) = Validate(string.Empty, ['!']);
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void NoDisallowedCharsPresent_Passes()
+ {
+ var (isValid, _) = Validate("hello", ['!', '@']);
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void EmptyDisallowedSet_Passes()
+ {
+ var (isValid, _) = Validate("hello!@#", []);
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void Unicode_AllowedChars_Pass()
+ {
+ var (isValid, _) = Validate("пользователь/音楽", ['!', '@']);
+ Assert.True(isValid);
+ }
+
+ // Case-insensitive matching only folds the declared disallowed chars, not all letters;
+ // a disallowed lower-case letter does not block an entirely different upper-case letter.
+ [Fact]
+ public void LowerCaseDisallowed_DoesNotBlock_Different_UpperCaseLetter()
+ {
+ var (isValid, _) = Validate("B", ['a']);
+ Assert.True(isValid);
+ }
+ }
+
+ public class Fails
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, char[] disallowed)
+ => DisallowedCharacters.Validate(value, disallowed);
+
+ [Fact]
+ public void SingleDisallowedChar_Fails()
+ {
+ var (isValid, _) = Validate("hello!", ['!']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void DisallowedCharAtStart_Fails()
+ {
+ var (isValid, _) = Validate("!hello", ['!']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void DisallowedCharAtEnd_Fails()
+ {
+ var (isValid, _) = Validate("hello!", ['!']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void DisallowedCharInMiddle_Fails()
+ {
+ var (isValid, _) = Validate("hel!lo", ['!']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void OneOfMultipleDisallowedCharsPresent_Fails()
+ {
+ var (isValid, _) = Validate("hello!", ['!', '@', '#']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void MultipleDisallowedCharsAllPresent_Fails()
+ {
+ var (isValid, _) = Validate("h!e@l#lo", ['!', '@', '#']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void DisallowedCharRepeated_Fails()
+ {
+ var (isValid, _) = Validate("he!lo!", ['!']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void AllCharsDisallowed_Fails()
+ {
+ var (isValid, _) = Validate("!@#", ['!', '@', '#']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void Unicode_DisallowedChar_Fails()
+ {
+ var (isValid, _) = Validate("пользователь", ['е']);
+ Assert.False(isValid);
+ }
+
+ // character matching is always case-insensitive
+ [Fact]
+ public void LowerCaseDisallowed_Blocks_UpperCase()
+ {
+ var (isValid, _) = Validate("HELLO", ['h', 'e', 'l', 'o']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void UpperCaseDisallowed_Blocks_LowerCase()
+ {
+ var (isValid, _) = Validate("hello", ['H', 'E', 'L', 'O']);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void ExactMatch_Fails()
+ {
+ var (isValid, _) = Validate("hello", ['h']);
+ Assert.False(isValid);
+ }
+ }
+
+ public class ErrorMessage_Contains
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, char[] disallowed)
+ => DisallowedCharacters.Validate(value, disallowed);
+
+ [Fact]
+ public void TheDisallowedChar()
+ {
+ var (_, errorMessage) = Validate("hello!", ['!']);
+ Assert.Contains("'!'", errorMessage);
+ }
+
+ [Fact]
+ public void AllPresentDisallowedChars()
+ {
+ var (_, errorMessage) = Validate("h!@l", ['!', '@']);
+ Assert.Contains("'!'", errorMessage);
+ Assert.Contains("'@'", errorMessage);
+ }
+
+ [Fact]
+ public void EachOccurrenceOfRepeatedChar()
+ {
+ // "he!lo!" has two '!' — both occurrences are listed
+ var (_, errorMessage) = Validate("he!lo!", ['!']);
+ Assert.Equal(2, errorMessage.Split("'!'").Length - 1);
+ }
+
+ [Fact]
+ public void OnlyCharsActuallyPresent_NotAllDisallowedChars()
+ {
+ // '@' is disallowed but not in the value — must not appear in the message
+ var (_, errorMessage) = Validate("hello!", ['!', '@']);
+ Assert.Contains("'!'", errorMessage);
+ Assert.DoesNotContain("'@'", errorMessage);
+ }
+
+ [Fact]
+ public void ShowsActualChar_NotDisallowedChar()
+ {
+ // 'a' is disallowed (lowercase); value contains 'A' (uppercase).
+ // The message reports what the user supplied ('A'), not the disallowed definition ('a').
+ var (_, errorMessage) = Validate("A", ['a']);
+ Assert.Contains("'A'", errorMessage);
+ Assert.DoesNotContain("'a'", errorMessage);
+ }
+ }
+ }
+
+ // -----------------------------------------------------------------------
+ // AllowNull
+ // -----------------------------------------------------------------------
+ public class AllowNull
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => StringAttributeTests.Validate(value, configure);
+
+ [Fact]
+ public void Null_Fails_When_AllowNull_Is_False()
+ {
+ var (isValid, _) = Validate(null, a => a.AllowNull = false);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void NonNull_Passes_When_AllowNull_Is_False()
+ {
+ var (isValid, _) = Validate("hello", a => a.AllowNull = false);
+ Assert.True(isValid);
+ }
+
+ public class ErrorMessage_Contains
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => AllowNull.Validate(value, configure);
+
+ [Fact]
+ public void Null_Keyword()
+ {
+ var (_, message) = Validate(null, a => a.AllowNull = false);
+ Assert.Contains("null", message);
+ }
+ }
+ }
+
+ // -----------------------------------------------------------------------
+ // AllowEmpty
+ // -----------------------------------------------------------------------
+ public class AllowEmpty
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => StringAttributeTests.Validate(value, configure);
+
+ [Fact]
+ public void Empty_Passes_When_AllowEmpty_Is_True()
+ {
+ var (isValid, _) = Validate(string.Empty);
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void Empty_Fails_When_AllowEmpty_Is_False()
+ {
+ var (isValid, _) = Validate(string.Empty, a => a.AllowEmpty = false);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void NonEmpty_Passes_When_AllowEmpty_Is_False()
+ {
+ var (isValid, _) = Validate("hello", a => a.AllowEmpty = false);
+ Assert.True(isValid);
+ }
+
+ public class ErrorMessage_Contains
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => AllowEmpty.Validate(value, configure);
+
+ [Fact]
+ public void Character_Keyword()
+ {
+ var (_, message) = Validate(string.Empty, a => a.AllowEmpty = false);
+ Assert.Contains("character", message);
+ }
+ }
+ }
+
+ // -----------------------------------------------------------------------
+ // AllowWhiteSpace
+ // -----------------------------------------------------------------------
+ public class AllowWhiteSpace
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => StringAttributeTests.Validate(value, configure);
+
+ [Fact]
+ public void WhiteSpace_Passes_When_AllowWhiteSpace_Is_True()
+ {
+ var (isValid, _) = Validate(" ");
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void WhiteSpace_Fails_When_AllowWhiteSpace_Is_False()
+ {
+ var (isValid, _) = Validate(" ", a => a.AllowWhiteSpace = false);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void NonWhiteSpace_Passes_When_AllowWhiteSpace_Is_False()
+ {
+ var (isValid, _) = Validate("hello", a => a.AllowWhiteSpace = false);
+ Assert.True(isValid);
+ }
+
+ // IsNullOrWhiteSpace("") is true, so empty string also fails the whitespace check
+ // when AllowEmpty=true (default) and AllowWhiteSpace=false
+ [Fact]
+ public void EmptyString_Fails_When_AllowWhiteSpace_Is_False_And_AllowEmpty_Is_True()
+ {
+ var (isValid, _) = Validate(string.Empty, a => a.AllowWhiteSpace = false);
+ Assert.False(isValid);
+ }
+
+ public class ErrorMessage_Contains
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => AllowWhiteSpace.Validate(value, configure);
+
+ [Fact]
+ public void Whitespace_Keyword()
+ {
+ var (_, message) = Validate(" ", a => a.AllowWhiteSpace = false);
+ Assert.Contains("whitespace", message);
+ }
+ }
+ }
+
+ // -----------------------------------------------------------------------
+ // Length (MinimumLength / MaximumLength)
+ // -----------------------------------------------------------------------
+ public class Length
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => StringAttributeTests.Validate(value, configure);
+
+ [Fact]
+ public void Within_Range_Passes()
+ {
+ var (isValid, _) = Validate("hello", a => { a.MinimumLength = 3; a.MaximumLength = 10; });
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void ExactlyAtMinimumLength_Passes()
+ {
+ var (isValid, _) = Validate("hi", a => a.MinimumLength = 2);
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void ExactlyAtMaximumLength_Passes()
+ {
+ var (isValid, _) = Validate("hello", a => a.MaximumLength = 5);
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void BelowMinimumLength_Fails()
+ {
+ var (isValid, _) = Validate("hi", a => a.MinimumLength = 5);
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void ExceedsMaximumLength_Fails()
+ {
+ var (isValid, _) = Validate("hello world", a => a.MaximumLength = 5);
+ Assert.False(isValid);
+ }
+
+ public class ErrorMessage_Contains
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => Length.Validate(value, configure);
+
+ [Fact]
+ public void MinimumLength_Value()
+ {
+ var (_, message) = Validate("hi", a => { a.MinimumLength = 5; a.MaximumLength = 20; });
+ Assert.Contains("5", message);
+ }
+
+ [Fact]
+ public void MaximumLength_Value()
+ {
+ var (_, message) = Validate("hi", a => { a.MinimumLength = 5; a.MaximumLength = 20; });
+ Assert.Contains("20", message);
+ }
+ }
+ }
+
+ // -----------------------------------------------------------------------
+ // Pattern
+ // -----------------------------------------------------------------------
+ // The Pattern check fails validation when the value *matches* the regex —
+ // Pattern acts as a "disallowed pattern" rather than a "required pattern".
+ public class Pattern
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => StringAttributeTests.Validate(value, configure);
+
+ [Fact]
+ public void NullPattern_Passes()
+ {
+ // Pattern defaults to null; any value passes the pattern check
+ var (isValid, _) = Validate("hello");
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void ValueMatchingPattern_Fails()
+ {
+ var (isValid, _) = Validate("hello1", a => a.Pattern = new Regex("[0-9]"));
+ Assert.False(isValid);
+ }
+
+ [Fact]
+ public void ValueNotMatchingPattern_Passes()
+ {
+ var (isValid, _) = Validate("hello", a => a.Pattern = new Regex("[0-9]"));
+ Assert.True(isValid);
+ }
+
+ public class ErrorMessage_Contains
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => Pattern.Validate(value, configure);
+
+ [Fact]
+ public void RegularExpression_Keyword()
+ {
+ var (_, message) = Validate("hello1", a => a.Pattern = new Regex("[0-9]"));
+ Assert.Contains("regular expression", message);
+ }
+ }
+ }
+
+ // -----------------------------------------------------------------------
+ // Check ordering
+ // -----------------------------------------------------------------------
+ // Validation checks run in declaration order; when a value violates multiple
+ // conditions the error from the first check in code is the one returned.
+ public class CheckOrdering
+ {
+ private static (bool IsValid, string ErrorMessage) Validate(string value, Action<StringAttribute> configure = null)
+ => StringAttributeTests.Validate(value, configure);
+
+ // AllowNull is checked before AllowEmpty:
+ // null with both disabled returns the null error, not the empty error.
+ [Fact]
+ public void Null_ReturnsNullError_Not_EmptyError()
+ {
+ var (_, message) = Validate(null, a => { a.AllowNull = false; a.AllowEmpty = false; });
+ Assert.Contains("null", message);
+ Assert.DoesNotContain("character", message);
+ }
+
+ // AllowEmpty is checked before AllowWhiteSpace:
+ // "" with both disabled returns the empty error, not the whitespace error.
+ [Fact]
+ public void Empty_ReturnsEmptyError_Not_WhitespaceError()
+ {
+ var (_, message) = Validate(string.Empty, a => { a.AllowEmpty = false; a.AllowWhiteSpace = false; });
+ Assert.Contains("character", message);
+ Assert.DoesNotContain("whitespace", message);
+ }
+
+ // AllowWhiteSpace is checked before the length check:
+ // a whitespace string that is also below MinimumLength returns the whitespace error.
+ [Fact]
+ public void Whitespace_ReturnsWhitespaceError_Not_LengthError()
+ {
+ var (_, message) = Validate(" ", a => { a.AllowWhiteSpace = false; a.MinimumLength = 10; });
+ Assert.Contains("whitespace", message);
+ Assert.DoesNotContain("between", message);
+ }
+
+ // Length is checked before DisallowedCharacters:
+ // a too-short string that also contains a disallowed character returns the length error.
+ [Fact]
+ public void TooShort_ReturnsLengthError_Not_DisallowedCharsError()
+ {
+ var (_, message) = Validate("hi!", a => { a.MinimumLength = 10; a.DisallowedCharacters = ['!']; });
+ Assert.Contains("between", message);
+ Assert.DoesNotContain("disallowed", message);
+ }
+
+ // DisallowedCharacters is checked before Pattern:
+ // a string with a disallowed character that also matches the pattern returns the disallowed-chars error.
+ [Fact]
+ public void DisallowedChar_ReturnsDisallowedError_Not_PatternError()
+ {
+ var (_, message) = Validate("hello!1", a => { a.DisallowedCharacters = ['!']; a.Pattern = new Regex("[0-9]"); });
+ Assert.Contains("disallowed", message);
+ Assert.DoesNotContain("expression", message);
+ }
+ }
+}