Dynamic Validator

Dynamic Validator

Hello Again,

In this tutorial I am using dependency injection to add rules for any class and use these rules later in validation process.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace DynamicValidator
{
    class Program
    {
        static void Main(string[] args)
        {
            Emp e1 = new Emp();

            e1.id = 123;
            bool vv = e1.validate();
            Console.WriteLine("Validate: " + vv.ToString());

            e1.name = "Ahmed";
            vv = e1.validate();
            Console.WriteLine("Validate: " + vv.ToString());

            e1.email = "[email protected]";
            vv = e1.validate();
            Console.WriteLine("Validate: " + vv.ToString());
            Console.ReadKey();

        }
    }
    public interface RuleType
    {
        bool Validate(object v);
    }
    public class RegexRule: RuleType 
    {
        private string _RegexString;
        public RegexRule(string r) { _RegexString = r; }
        public bool Validate(object v)
        {
            if (v == null) return false;
            Regex regex = new Regex(_RegexString);
            Match match = regex.Match(v.ToString());
            if (match.Success) return true;
            else return false;
        }
    }
    public class Required : RuleType
    {
        private bool _required;
        public Required(bool r) { _required = r; }
        public bool Validate(object v)
        {
            if (!_required) return true;
            if (v != null && (String.IsNullOrEmpty(v.ToString()) || String.IsNullOrWhiteSpace(v.ToString()))) return false;
            else return true;
        }
    }
    public static class RulesMapper
    {
        public static Dictionary<string, List<RuleType>> rule_map = new Dictionary<string, List<RuleType>>();
        public static bool validate(string type , object obj)
        {
            foreach (RuleType r in rule_map[type])
                if (!r.Validate(obj)) return false;
            return true;
        }
        public static void AddRule(string type , List<RuleType> rules)
        {
            List<RuleType> tmp = new List<RuleType>();
            if (!rule_map.TryGetValue(type, out tmp)) rule_map.Add(type, rules);
            else tmp.AddRange(rules);
        }
    }
    public class Emp
    {
        public int id;
        public string name;
        public string email;

        public Emp()
        {
            List<RuleType> _RuleList0 = new List<RuleType>();
            _RuleList0.Add(new Required(true));
            RulesMapper.AddRule(typeof(Emp) + ":" + nameof(id), _RuleList0);

            List<RuleType> _RuleList1 = new List<RuleType>();
            _RuleList1.Add(new Required(true));
            RulesMapper.AddRule(typeof(Emp) + ":" + nameof(name), _RuleList1);

            List<RuleType> _RuleList2 = new List<RuleType>();
            _RuleList2.Add(new RegexRule(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"));
            _RuleList2.Add(new Required(true));
            RulesMapper.AddRule(typeof(Emp) + ":" + nameof(email), _RuleList2);
        }

        public bool validate()
        {
            if (RulesMapper.validate(typeof(Emp) + ":" + nameof(id), id) &&
                RulesMapper.validate(typeof(Emp) + ":" + nameof(name), name) &&
                RulesMapper.validate(typeof(Emp) + ":" + nameof(email), email) ) return true;
            return false;
        }
        
    }

}

The Output

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.