aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Models/Validation/StringSetValidator.cs
blob: 363ece10fedfa1d853ca5c8d7c8387c8f05aa85b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Linq;

namespace Timeline.Models.Validation
{
    public class StringSetValidator : Validator<string>
    {
        public StringSetValidator(params string[] set)
        {
            Set = set;
        }

#pragma warning disable CA1819 // Properties should not return arrays
        public string[] Set { get; set; }
#pragma warning restore CA1819 // Properties should not return arrays

        protected override (bool, string) DoValidate(string value)
        {
            var contains = Set.Contains(value, StringComparer.OrdinalIgnoreCase);
            if (!contains)
            {
                return (false, "Not a valid value.");
            }
            else
            {
                return (true, GetSuccessMessage());
            }
        }
    }

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
    AllowMultiple = false)]
    public class ValidationSetAttribute : ValidateWithAttribute
    {
        public ValidationSetAttribute(params string[] set) : base(new StringSetValidator(set))
        {

        }
    }
}