From 843da00b55c2296422596ec0539afbcadb3de3b5 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 10 Mar 2020 16:01:09 +0800 Subject: ... --- Timeline/Models/Timeline.cs | 2 ++ .../Validation/GeneralTimelineNameValidator.cs | 33 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 Timeline/Models/Validation/GeneralTimelineNameValidator.cs (limited to 'Timeline/Models') diff --git a/Timeline/Models/Timeline.cs b/Timeline/Models/Timeline.cs index e2ff525e..6d4c924d 100644 --- a/Timeline/Models/Timeline.cs +++ b/Timeline/Models/Timeline.cs @@ -55,7 +55,9 @@ namespace Timeline.Models public DateTime LastUpdated { get; set; } = default!; } +#pragma warning disable CA1724 // Type names should not match namespaces public class Timeline +#pragma warning restore CA1724 // Type names should not match namespaces { public string Name { get; set; } = default!; public string Description { get; set; } = default!; diff --git a/Timeline/Models/Validation/GeneralTimelineNameValidator.cs b/Timeline/Models/Validation/GeneralTimelineNameValidator.cs new file mode 100644 index 00000000..e1c96fbd --- /dev/null +++ b/Timeline/Models/Validation/GeneralTimelineNameValidator.cs @@ -0,0 +1,33 @@ +using System; + +namespace Timeline.Models.Validation +{ + public class GeneralTimelineNameValidator : Validator + { + private readonly UsernameValidator _usernameValidator = new UsernameValidator(); + private readonly TimelineNameValidator _timelineNameValidator = new TimelineNameValidator(); + + protected override (bool, string) DoValidate(string value) + { + if (value.StartsWith('@')) + { + return _usernameValidator.Validate(value.Substring(1)); + } + else + { + return _timelineNameValidator.Validate(value); + } + } + } + + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, + AllowMultiple = false)] + public class GeneralTimelineNameAttribute : ValidateWithAttribute + { + public GeneralTimelineNameAttribute() + : base(typeof(GeneralTimelineNameValidator)) + { + + } + } +} -- cgit v1.2.3