From 74810d42ea08d9782bd83a02d06eaca9c2ecd9a5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 May 2026 18:53:07 -0500 Subject: add StringAttribute to cover all string related validation --- src/slskd/Common/Validation/StringAttribute.cs | 92 ++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/slskd/Common/Validation/StringAttribute.cs (limited to 'src/slskd/Common/Validation/StringAttribute.cs') diff --git a/src/slskd/Common/Validation/StringAttribute.cs b/src/slskd/Common/Validation/StringAttribute.cs new file mode 100644 index 00000000..2fcf4283 --- /dev/null +++ b/src/slskd/Common/Validation/StringAttribute.cs @@ -0,0 +1,92 @@ +// +// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ +// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ +// █__ --█ █__ --█ ◄█ - █ +// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ +// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ +// │ Copyright (c) JP Dillingham. +// │ +// │ This program is free software: you can redistribute it and/or modify +// │ it under the terms of the GNU Affero General Public License as published +// │ by the Free Software Foundation, version 3. +// │ +// │ This program is distributed in the hope that it will be useful, +// │ but WITHOUT ANY WARRANTY; without even the implied warranty of +// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// │ GNU Affero General Public License for more details. +// │ +// │ You should have received a copy of the GNU Affero General Public License +// │ along with this program. If not, see https://www.gnu.org/licenses/. +// │ +// │ This program is distributed with Additional Terms pursuant to Section 7 +// │ of the AGPLv3. See the LICENSE file in the root directory of this +// │ project for the complete terms and conditions. +// │ +// │ https://slskd.org +// │ +// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ +// │ SPDX-FileCopyrightText: JP Dillingham +// │ SPDX-License-Identifier: AGPL-3.0-only +// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ +// + +namespace slskd.Validation +{ + using System.ComponentModel.DataAnnotations; + using System.Linq; + using System.Text.RegularExpressions; + + /// + /// Validates that the value is not null or whitespace. + /// + public class StringAttribute : ValidationAttribute + { + 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 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}'"))}"); + } + + 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; + } + } +} -- cgit v1.2.3 From 0346199c4ed5edb0d502f2e3f5972aafeea6c923 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 May 2026 19:01:50 -0500 Subject: fix test --- src/slskd/Common/Validation/StringAttribute.cs | 13 ++++++++++--- .../Common/Validation/StringAttributeTests.cs | 7 +++++++ 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'src/slskd/Common/Validation/StringAttribute.cs') diff --git a/src/slskd/Common/Validation/StringAttribute.cs b/src/slskd/Common/Validation/StringAttribute.cs index 2fcf4283..bb323ba2 100644 --- a/src/slskd/Common/Validation/StringAttribute.cs +++ b/src/slskd/Common/Validation/StringAttribute.cs @@ -53,9 +53,16 @@ namespace slskd.Validation { var val = (string)value; - if (!AllowNull && val is null) + if (val is null) { - return new ValidationResult($"The {validationContext.DisplayName} field must not be null"); + if (!AllowNull) + { + return new ValidationResult($"The {validationContext.DisplayName} field must not be null"); + } + else + { + return ValidationResult.Success; + } } if (!AllowEmpty && val is "") @@ -64,7 +71,7 @@ namespace slskd.Validation } // check this after empty to avoid overlap - if (!AllowWhiteSpace && string.IsNullOrWhiteSpace(val)) + if (!AllowWhiteSpace && val.All(c => char.IsWhiteSpace(c))) { return new ValidationResult($"The {validationContext.DisplayName} field must not contain only whitespace"); } diff --git a/tests/slskd.Tests.Unit/Common/Validation/StringAttributeTests.cs b/tests/slskd.Tests.Unit/Common/Validation/StringAttributeTests.cs index dfa964f7..71e6ce8b 100644 --- a/tests/slskd.Tests.Unit/Common/Validation/StringAttributeTests.cs +++ b/tests/slskd.Tests.Unit/Common/Validation/StringAttributeTests.cs @@ -221,6 +221,13 @@ public class StringAttributeTests private static (bool IsValid, string ErrorMessage) Validate(string value, Action configure = null) => StringAttributeTests.Validate(value, configure); + [Fact] + public void Null_Passes_When_AllowNull_Is_True() + { + var (isValid, _) = Validate(null); + Assert.True(isValid); + } + [Fact] public void Null_Fails_When_AllowNull_Is_False() { -- cgit v1.2.3