Exemple #1
0
 public Package Add(PackageAdd package)
 {
     if (context.Packages.Any(p => p.Name == package.Name))
     {
         var validator = modelValidator.GetHelper <PackageAdd>();
         validator.AddError(p => p.Name, PackageAdd.DuplicateName);
         return(null);
     }
     return(Add(package, loginManager.UserId));
 }
        public void NoModel()
        {
            var validator = new ModelValidator();
            var helper    = validator.GetHelper <Person>();

            helper.AddError(p => p.Name, "Name is too long");
            helper.AddError(p => p.Age, "You are getting too old for this");
            validator.AddError(nameof(Person.Age), "this one is personal");

            Assert.Equal(3, validator.GetErrors().SelectMany(e => e.Value).Count());
            Assert.Equal(2, validator.GetErrors().Count());
            Assert.Equal(2, validator.GetErrors(nameof(Person.Age)).Count());
            Assert.Single(validator.GetErrors(nameof(Person.Name)));
            Assert.Single(helper.GetErrors(p => p.Name));
        }
Exemple #3
0
        public void Validate(Person person)
        {
            var helper = modelValidator.GetHelper <Person>();

            if (person.FirstName == "nameless")
            {
                helper.AddError(p => p.FirstName, "You need a first name.");
            }
            if (person.LastName == "nameless")
            {
                helper.AddError(p => p.LastName, "Man needs a last name.");
            }
            if (person.Age > 35)
            {
                helper.AddError(p => p.Age, "You are getting too old for this s...erver");
            }
        }
        public void WithModel()
        {
            var model     = new Person();
            var validator = new ModelValidator();
            var helper    = validator.GetHelper(model);

            helper.AddError(p => p.Name, "Name is too long");
            helper.AddError(p => p.Age, "You are getting too old for this");
            validator.AddError(nameof(Person.Age), "this one is personal");

            Assert.Single(validator.GetErrors());
            Assert.Equal(2, validator.GetErrors(model).Count());
            Assert.Single(validator.GetErrors(model, nameof(Person.Age)));
            Assert.Single(helper.GetErrors(p => p.Age));
            Assert.Single(helper.GetErrors(p => p.Name));
            Assert.Null(helper.GetErrors(p => p.Hobby));
            Assert.Single(validator.GetErrors(model, nameof(Person.Name)));
        }