From bd9dacf0566ca542ecab913acbc244f474c6a752 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 1 Jun 2021 23:22:47 +0800 Subject: feat: Add ways to reset timeline or post color. --- .../Timeline/Models/Validation/ColorValidator.cs | 34 ++++++++++++++++++++++ BackEnd/Timeline/Models/Validation/Validator.cs | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) (limited to 'BackEnd/Timeline/Models/Validation') 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 { + 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; /// /// Create with a given validator. -- cgit v1.2.3