static void Main(string[] args)
        {
            var fname = Prompt("First Name: ");
            var lname = Prompt("Last Name: ");
            var email = Prompt("Email: ");

            var person = new Person
            {
                FirstName = fname,
                LastName  = lname,
                Email     = email
            };

            var result = new PersonValidator().Validate(person);

            if (result.IsValid)
            {
                Console.WriteLine("Ok");
            }
            else
            {
                foreach (var error in result.Errors)
                {
                    Console.WriteLine(error.ErrorMessage);
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            var person = new Person()
            {
                Name     = "A",
                Surname  = "",
                Email    = "[email protected]",
                BankCard = "4286531213451234",
                Phone    = "324242",
                Age      = 10,
                Passport = "AZ1234567"
            };

            var personValidator = new PersonValidator();
            var result          = personValidator.Validate(person);

            Console.WriteLine($"Is Valid: {result.IsValid}");
            if (!result.IsValid)
            {
                foreach (var res in result.Errors)
                {
                    Console.WriteLine(res.ErrorMessage);
                }
            }

            //var context = new ValidationContext(person);

            //var results = new List<ValidationResult>();
            //var isValid = Validator.TryValidateObject(person, context, results, true);
            //// Validator.ValidateObject(person, context, true);
            //if (!isValid)
            //{
            //    foreach (var result in results)
            //    {
            //        Console.WriteLine(result.ErrorMessage);
            //    }
            //}

            // Reflection
            //var type = typeof(Person);
            //foreach (var propInfo in type.GetProperties())
            //{
            //    Console.WriteLine(propInfo.Name);
            //    foreach (var attribute in propInfo.CustomAttributes)
            //    {
            //        Console.WriteLine("  -  " + attribute);
            //    }
            //}
        }