Added validators for voice id & voice name
This commit is contained in:
parent
39c6442126
commit
c082054606
7
Validators/IValidator.cs
Normal file
7
Validators/IValidator.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace HermesSocketServer.Validators
|
||||
{
|
||||
public interface IValidator
|
||||
{
|
||||
bool Check(string? input);
|
||||
}
|
||||
}
|
29
Validators/RegexValidator.cs
Normal file
29
Validators/RegexValidator.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
10
Validators/VoiceIdValidator.cs
Normal file
10
Validators/VoiceIdValidator.cs
Normal file
@ -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) { }
|
||||
}
|
||||
}
|
10
Validators/VoiceNameValidator.cs
Normal file
10
Validators/VoiceNameValidator.cs
Normal file
@ -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) { }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user