2021-08-26 01:43:31 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2021-04-21 21:57:19 +00:00
|
|
|
|
2021-10-13 12:37:34 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
using SqlKata;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public abstract class PatchObject
|
2020-06-29 11:57:48 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public List<ValidationError> Errors = new();
|
|
|
|
public abstract Query Apply(Query q);
|
|
|
|
|
|
|
|
public void AssertIsValid() { }
|
|
|
|
|
|
|
|
protected void AssertValid(string input, string name, int maxLength, Func<string, bool>? validate = null)
|
|
|
|
{
|
|
|
|
if (input.Length > maxLength)
|
|
|
|
Errors.Add(new FieldTooLongError(name, maxLength, input.Length));
|
|
|
|
if (validate != null && !validate(input))
|
|
|
|
Errors.Add(new ValidationError(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void AssertValid(string input, string name, string pattern)
|
2021-08-26 01:43:31 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!Regex.IsMatch(input, pattern))
|
|
|
|
Errors.Add(new ValidationError(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
public PrivacyLevel ParsePrivacy(JObject o, string propertyName)
|
|
|
|
{
|
|
|
|
var input = o.Value<string>(propertyName);
|
|
|
|
|
|
|
|
if (input == null) return PrivacyLevel.Public;
|
|
|
|
if (input == "") return PrivacyLevel.Private;
|
|
|
|
if (input == "private") return PrivacyLevel.Private;
|
|
|
|
if (input == "public") return PrivacyLevel.Public;
|
|
|
|
|
|
|
|
Errors.Add(new ValidationError(propertyName));
|
|
|
|
// unused, but the compiler will complain if this isn't here
|
|
|
|
return PrivacyLevel.Private;
|
2020-06-29 11:57:48 +00:00
|
|
|
}
|
|
|
|
}
|