From 0a0a61b60544a135a61394953bb5bb9dbbfeb241 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 2 Feb 2021 18:59:41 +0800 Subject: ... --- .../Timeline/Models/Validation/ColorValidator.cs | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 BackEnd/Timeline/Models/Validation/ColorValidator.cs (limited to 'BackEnd/Timeline/Models/Validation') diff --git a/BackEnd/Timeline/Models/Validation/ColorValidator.cs b/BackEnd/Timeline/Models/Validation/ColorValidator.cs new file mode 100644 index 00000000..c5ad833d --- /dev/null +++ b/BackEnd/Timeline/Models/Validation/ColorValidator.cs @@ -0,0 +1,40 @@ +using System; + +namespace Timeline.Models.Validation +{ + public class ColorValidator : Validator + { + protected override (bool, string) DoValidate(string value) + { + if (!value.StartsWith('#')) + { + return (false, "Color must starts with '#'."); + } + + if (value.Length != 7) + { + return (false, "A color string must have 7 chars."); + } + + for (int i = 1; i < 7; i++) + { + var c = value[i]; + if (!((c >= '0' && c <= '9') || (c >= 'a' || c <= 'f') || (c >= 'A' | c <= 'F'))) + { + return (false, $"Char at index {i} is not a hex character."); + } + } + + return (true, GetSuccessMessage()); + } + } + + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] + public class ColorAttribute : ValidateWithAttribute + { + public ColorAttribute() : base(typeof(ColorValidator)) + { + + } + } +} -- cgit v1.2.3