diff options
author | crupest <crupest@outlook.com> | 2021-06-02 22:02:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-02 22:02:32 +0800 |
commit | 125c77d974c90b547ec3f89e71ad6420f1668fd3 (patch) | |
tree | 7d42d50306047b53853edbcbc131b95f42aeef16 /BackEnd/Timeline/Models | |
parent | 9060b1e463b5928d7d0e412fbd4cd7178e131bd4 (diff) | |
parent | 0f0b189043b18b951c1aca0174b189bd4ae4cd08 (diff) | |
download | timeline-125c77d974c90b547ec3f89e71ad6420f1668fd3.tar.gz timeline-125c77d974c90b547ec3f89e71ad6420f1668fd3.tar.bz2 timeline-125c77d974c90b547ec3f89e71ad6420f1668fd3.zip |
Merge pull request #587 from crupest/default-color
Add patch default color api.
Diffstat (limited to 'BackEnd/Timeline/Models')
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.
|