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));
        }
        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)));
        }
Esempio n. 3
0
        public ServerPackage AddServer(ServerPackageAdd server)
        {
            modelValidator.GetErrors("");
            var validator = modelValidator.GetHelper <ServerPackageAdd>();

            if (!context.Packages.Any(p => p.Id == server.PackageId && p.CreatedById == UserId))
            {
                modelValidator.AddError(ServerPackageAdd.InvalidPackage);
                return(null);
            }
            else if (context.ServerPackages.Any(c => c.PackageId == server.PackageId &&
                                                c.PackageName == server.PackageName))
            {
                validator.AddError(c => c.PackageName, ServerPackageAdd.DuplicateSource);
                return(null);
            }
            else
            {
                return(AddServer(server, UserId));
            }
        }