Пример #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());
        }
Пример #2
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());
        }