aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Models
diff options
context:
space:
mode:
Diffstat (limited to 'BackEnd/Timeline/Models')
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePatchRequest.cs2
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs2
-rw-r--r--BackEnd/Timeline/Models/Validation/ColorValidator.cs34
-rw-r--r--BackEnd/Timeline/Models/Validation/Validator.cs2
4 files changed, 37 insertions, 3 deletions
diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePatchRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePatchRequest.cs
index 9accb6fc..35667af2 100644
--- a/BackEnd/Timeline/Models/Http/HttpTimelinePatchRequest.cs
+++ b/BackEnd/Timeline/Models/Http/HttpTimelinePatchRequest.cs
@@ -31,7 +31,7 @@ namespace Timeline.Models.Http
/// <summary>
/// New color. Null for not change.
/// </summary>
- [Color]
+ [Color(PermitDefault = true, PermitEmpty = true)]
public string? Color { get; set; }
}
}
diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs
index 2c6edf66..cb576a74 100644
--- a/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs
+++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs
@@ -13,7 +13,7 @@ namespace Timeline.Models.Http
/// <summary>
/// Change the color. Null for not change.
/// </summary>
- [Color]
+ [Color(PermitEmpty = true, PermitDefault = true)]
public string? Color { get; set; }
}
}
diff --git a/BackEnd/Timeline/Models/Validation/ColorValidator.cs b/BackEnd/Timeline/Models/Validation/ColorValidator.cs
index c5ad833d..4f7accc5 100644
--- a/BackEnd/Timeline/Models/Validation/ColorValidator.cs
+++ b/BackEnd/Timeline/Models/Validation/ColorValidator.cs
@@ -4,8 +4,22 @@ namespace Timeline.Models.Validation
{
public class ColorValidator : Validator<string>
{
+ public bool PermitEmpty { get; set; } = false;
+ public bool PermitDefault { get; set; } = false;
+ public string DefaultValue { get; set; } = "default";
+
protected override (bool, string) DoValidate(string value)
{
+ if (PermitEmpty && value.Length == 0)
+ {
+ return (true, GetSuccessMessage());
+ }
+
+ if (PermitDefault && value == DefaultValue)
+ {
+ return (true, GetSuccessMessage());
+ }
+
if (!value.StartsWith('#'))
{
return (false, "Color must starts with '#'.");
@@ -32,9 +46,29 @@ namespace Timeline.Models.Validation
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class ColorAttribute : ValidateWithAttribute
{
+ private ColorValidator Validator => (ColorValidator)_validator;
+
public ColorAttribute() : base(typeof(ColorValidator))
{
}
+
+ public bool PermitEmpty
+ {
+ get => Validator.PermitEmpty;
+ set => Validator.PermitEmpty = value;
+ }
+
+ public bool PermitDefault
+ {
+ get => Validator.PermitDefault;
+ set => Validator.PermitDefault = value;
+ }
+
+ public string DefaultValue
+ {
+ get => Validator.DefaultValue;
+ set => Validator.DefaultValue = value;
+ }
}
}
diff --git a/BackEnd/Timeline/Models/Validation/Validator.cs b/BackEnd/Timeline/Models/Validation/Validator.cs
index d334960e..0e1f7445 100644
--- a/BackEnd/Timeline/Models/Validation/Validator.cs
+++ b/BackEnd/Timeline/Models/Validation/Validator.cs
@@ -77,7 +77,7 @@ namespace Timeline.Models.Validation
AllowMultiple = false)]
public class ValidateWithAttribute : ValidationAttribute
{
- private readonly IValidator _validator;
+ protected readonly IValidator _validator;
/// <summary>
/// Create with a given validator.