Ejemplo n.º 1
0
        public void ModelValidatorNullableTestEnum()
        {
            var validator      = new Validator();
            var modelValidator = ModelValidators.GetValidator <CreatePersonModel>();

            var person = new CreatePersonModel
            {
                Id   = 0,
                Name = "A",
                // ReSharper disable RedundantCast
                NullableEnumField = (TestEnum)(int)0
                                    // ReSharper restore RedundantCast
            };

            modelValidator.Validate(person, validator);

            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());


            var outerModelValidator = ModelValidators.GetValidator <OuterClass>();

            var outer = new OuterClass
            {
                CreatePerson  = person,
                CreatePersons = new CreatePersonModel[0]
            };

            outerModelValidator.Validate(outer, validator);
            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());

            outer = new OuterClass
            {
                CreatePerson  = null,
                CreatePersons = new[]
                {
                    person
                }
            };
            outerModelValidator.Validate(outer, validator);
            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());

            outer = new OuterClass
            {
                CreatePerson  = person,
                CreatePersons = new[]
                {
                    person
                }
            };
            outerModelValidator.Validate(outer, validator);
            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());
        }
Ejemplo n.º 2
0
        public void ModelValidatorCopyTestMaxLength()
        {
            var validator      = new Validator();
            var modelValidator = ModelValidators.GetValidator <CreatePersonModel>();

            modelValidator.Validate(
                new CreatePersonModel
            {
                Name = new string('x', 100)
            },
                validator);

            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());
        }
Ejemplo n.º 3
0
        public void ModelValidatorTestTrimmed()
        {
            var validator      = new Validator();
            var modelValidator = ModelValidators.GetValidator <Person>();

            modelValidator.Validate(
                new Person
            {
                Name = " Test "
            },
                validator);

            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());
        }
Ejemplo n.º 4
0
        public void ModelValidatorCopyTestRecursiveTrimmed()
        {
            var validator      = new Validator();
            var modelValidator = ModelValidators.GetValidator <OuterClass>();

            modelValidator.Validate(
                new OuterClass
            {
                CreatePerson =
                    new CreatePersonModel
                {
                    Name = " Test "
                }
            },
                validator);

            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());
        }
Ejemplo n.º 5
0
        public void ModelValidatorCopyDistinctTest()
        {
            var validator      = new Validator();
            var modelValidator = ModelValidators.GetValidator <OuterClass>();

            modelValidator.Validate(
                new OuterClass
            {
                CreatePersons = new[]
                {
                    new CreatePersonModel
                    {
                        Name = "meh"
                    },
                    new CreatePersonModel
                    {
                        Name = "meh"
                    }
                }
            },
                validator);

            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());
        }
Ejemplo n.º 6
0
        public void ModelValidatorCopyTestRecursiveArrayMaxLength()
        {
            var validator      = new Validator();
            var modelValidator = ModelValidators.GetValidator <OuterClass>();

            modelValidator.Validate(
                new OuterClass
            {
                CreatePersons = new []
                {
                    new CreatePersonModel
                    {
                        Name = "meh"
                    },
                    new CreatePersonModel
                    {
                        Name = new string('x', 100)
                    }
                }
            },
                validator);

            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());
        }
Ejemplo n.º 7
0
        public async Task ModelValidatorTestUnique()
        {
            var validator = new Validator();

            var people = new []
            {
                new CreatePersonModel {
                    Id = 0, Name = "A", IsDeleted = true
                },
                new CreatePersonModel {
                    Id = 0, Name = "A", IsActive = false
                },
                new CreatePersonModel {
                    Id = 0, Name = "A", IsDeleted = true, IsActive = false
                },
                new CreatePersonModel {
                    Id = 0, Name = "A"
                },
                new CreatePersonModel {
                    Id = 1, Name = "B"
                },
                new CreatePersonModel {
                    Id = 2, Name = "C"
                },
                new CreatePersonModel {
                    Id = 3, Name = "D"
                },
                new CreatePersonModel {
                    Id = 4, Name = "E"
                },
                new CreatePersonModel {
                    Id = 5, Name = "F"
                }
            };

            validator.RegisterQueryByEntity(new CreatePeopleRepository(people));

            var person = people.OfActiveState(ActiveState.Active).OfDeletedState(DeletedState.NotDeleted)
                         .Single(x => x.Id == 5);

            await validator.CheckUniqueAsync <CreatePersonModel, CreatePersonModel, string>(
                person.Name,
                // ReSharper disable once RedundantBoolCompare
                x => x.IsDeleted == false && x.IsActive == true && x.Id != person.Id,
                x => x.Name,
                x => x.Name);

            validator.ThrowIfHasErrors();

            person = new CreatePersonModel {
                Id = 6, Name = "F"
            };

            await validator.CheckUniqueAsync <CreatePersonModel, CreatePersonModel, string>(
                person.Name,
                // ReSharper disable once RedundantBoolCompare
                x => x.IsDeleted == false && x.IsActive == true && x.Id != person.Id,
                x => x.Name,
                x => x.Name);

            Console.WriteLine(Assert.ThrowsException <ApiErrorException>(() => validator.ThrowIfHasErrors()).ToString());
        }