From c082054606f4eeb5eb28d76b4d6d4d070b6f4c84 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 17 Oct 2024 19:06:02 +0000 Subject: [PATCH] Added validators for voice id & voice name --- Validators/IValidator.cs | 7 +++++++ Validators/RegexValidator.cs | 29 +++++++++++++++++++++++++++++ Validators/VoiceIdValidator.cs | 10 ++++++++++ Validators/VoiceNameValidator.cs | 10 ++++++++++ 4 files changed, 56 insertions(+) create mode 100644 Validators/IValidator.cs create mode 100644 Validators/RegexValidator.cs create mode 100644 Validators/VoiceIdValidator.cs create mode 100644 Validators/VoiceNameValidator.cs diff --git a/Validators/IValidator.cs b/Validators/IValidator.cs new file mode 100644 index 0000000..ba58485 --- /dev/null +++ b/Validators/IValidator.cs @@ -0,0 +1,7 @@ +namespace HermesSocketServer.Validators +{ + public interface IValidator + { + bool Check(string? input); + } +} \ No newline at end of file diff --git a/Validators/RegexValidator.cs b/Validators/RegexValidator.cs new file mode 100644 index 0000000..7fe4113 --- /dev/null +++ b/Validators/RegexValidator.cs @@ -0,0 +1,29 @@ +using System.Text.RegularExpressions; + +namespace HermesSocketServer.Validators +{ + public class RegexValidator : IValidator + { + private readonly Regex _regex; + private readonly int _minimum; + private readonly int _maximum; + + public RegexValidator(string regex, RegexOptions options, int minimum, int maximum) { + _regex = new Regex(regex, options | RegexOptions.Compiled); + _minimum = minimum; + _maximum = maximum; + } + + public bool Check(string? input) + { + if (input == null) + throw new ArgumentNullException(nameof(input)); + if (input.Length < _minimum) + throw new ArgumentException("Too short. Must be of length 8 or greater.", nameof(input)); + if (input.Length > _maximum) + throw new ArgumentException("Too long. Must be of length 24 or less.", nameof(input)); + + return _regex.IsMatch(input); + } + } +} \ No newline at end of file diff --git a/Validators/VoiceIdValidator.cs b/Validators/VoiceIdValidator.cs new file mode 100644 index 0000000..0f5a733 --- /dev/null +++ b/Validators/VoiceIdValidator.cs @@ -0,0 +1,10 @@ +using System.Text.RegularExpressions; + +namespace HermesSocketServer.Validators +{ + public class VoiceIdValidator : RegexValidator + { + public VoiceIdValidator() + : base("^[a-z0-9]{25}$", RegexOptions.IgnoreCase, 25, 25) { } + } +} \ No newline at end of file diff --git a/Validators/VoiceNameValidator.cs b/Validators/VoiceNameValidator.cs new file mode 100644 index 0000000..5c02923 --- /dev/null +++ b/Validators/VoiceNameValidator.cs @@ -0,0 +1,10 @@ +using System.Text.RegularExpressions; + +namespace HermesSocketServer.Validators +{ + public class VoiceNameValidator : RegexValidator + { + public VoiceNameValidator() + : base("^[a-z0-9_\\-]{2,32}$", RegexOptions.IgnoreCase, 2, 32) { } + } +} \ No newline at end of file