2021-04-21 21:57:19 +00:00
|
|
|
|
using System;
|
2021-08-26 01:43:31 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2021-04-21 21:57:19 +00:00
|
|
|
|
|
|
|
|
|
namespace PluralKit.Core
|
2020-06-29 11:57:48 +00:00
|
|
|
|
{
|
2021-08-26 01:43:31 +00:00
|
|
|
|
public abstract class PatchObject
|
|
|
|
|
{
|
|
|
|
|
public abstract UpdateQueryBuilder Apply(UpdateQueryBuilder b);
|
|
|
|
|
|
|
|
|
|
public void AssertIsValid() {}
|
|
|
|
|
|
|
|
|
|
protected bool AssertValid(string input, string name, int maxLength, Func<string, bool>? validate = null)
|
|
|
|
|
{
|
|
|
|
|
if (input.Length > maxLength)
|
|
|
|
|
throw new FieldTooLongError(name, maxLength, input.Length);
|
|
|
|
|
if (validate != null && !validate(input))
|
|
|
|
|
throw new ValidationError(name);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-04-21 21:57:19 +00:00
|
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
|
protected bool AssertValid(string input, string name, string pattern)
|
|
|
|
|
{
|
|
|
|
|
if (!Regex.IsMatch(input, pattern))
|
|
|
|
|
throw new ValidationError(name);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ValidationError: Exception
|
2021-04-21 21:57:19 +00:00
|
|
|
|
{
|
2021-08-26 01:43:31 +00:00
|
|
|
|
public ValidationError(string message): base(message) { }
|
2021-04-21 21:57:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
|
public class FieldTooLongError: ValidationError
|
2020-06-29 11:57:48 +00:00
|
|
|
|
{
|
2021-08-26 01:43:31 +00:00
|
|
|
|
public string Name;
|
|
|
|
|
public int MaxLength;
|
|
|
|
|
public int ActualLength;
|
2021-04-21 21:57:19 +00:00
|
|
|
|
|
2021-08-26 01:43:31 +00:00
|
|
|
|
public FieldTooLongError(string name, int maxLength, int actualLength):
|
|
|
|
|
base($"{name} too long ({actualLength} > {maxLength})")
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
MaxLength = maxLength;
|
|
|
|
|
ActualLength = actualLength;
|
|
|
|
|
}
|
2020-06-29 11:57:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|