public void Should_read_regex_annotation()
        {
            // Given, When
            var subject = new DataAnnotationsValidator(typeof(TestModel));

            // Then
            subject.Description.Rules.ShouldHave(r => r.RuleType == "Regex" && r.MemberNames.Contains("Age"));
        }
        public void Should_read_required_annotation()
        {
            // Given, When
            var subject = new DataAnnotationsValidator(typeof(TestModel));

            // Then
            subject.Description.Rules.ShouldHave(r => r.RuleType == "NotNull" && r.MemberNames.Contains("FirstName"));
            subject.Description.Rules.ShouldHave(r => r.RuleType == "NotEmpty" && r.MemberNames.Contains("FirstName"));
        }
        public void Description_should_be_correct()
        {
            // Given, When
            var subject = new DataAnnotationsValidator(typeof(TestModel));

            // Then
            subject.Description.ShouldNotBeNull();
            subject.Description.Rules.ShouldHaveCount(10);
        }
        /// <summary>
        /// Creates a data annotations <see cref="IModelValidator"/> instance for the given type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>An <see cref="IModelValidator"/> instance. If no data annotation rules were found for the specified <paramref name="type"/> then <see langword="null"/> is returned.</returns>
        public IModelValidator Create(Type type)
        {
            var validator =
                new DataAnnotationsValidator(type);

            return(validator.Description.Rules.Any()
                ? validator
                : null);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a data annotations <see cref="IModelValidator"/> instance for the given type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>An <see cref="IModelValidator"/> instance. If no data annotation rules were found for the specified <paramref name="type"/> then <see langword="null"/> is returned.</returns>
        public IModelValidator Create(Type type) 
        {
            var validator = 
                new DataAnnotationsValidator(type, this.factory, this.validatableObjectAdapter);

            return validator.Description.Rules.Any() ?
                validator :
                null;
        }
        /// <summary>
        /// Creates a data annotations <see cref="IModelValidator"/> instance for the given type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>An <see cref="IModelValidator"/> instance. If no data annotation rules were found for the specified <paramref name="type"/> then <see langword="null"/> is returned.</returns>
        public IModelValidator Create(Type type)
        {
            var validator =
                new DataAnnotationsValidator(type);

            return validator.Description.Rules.Any()
                ? validator
                : null;
        }
        public void Should_invoke_validation()
        {
            // Given
            var subject = new DataAnnotationsValidator(typeof(TestModel));
            var instance = new TestModel { Age = "yeah" };

            // When
            var result = subject.Validate(instance);

            // Then
            result.IsValid.ShouldBeFalse();
            result.Errors.ShouldHaveCount(3);
        }
        public DataAnnotationsValidatorFixture()
        {
            this.propertyValidator1 =
                A.Fake<IPropertyValidator>();

            this.propertyValidator2 =
                A.Fake<IPropertyValidator>();

            this.validatableObjectAdapter =
                A.Fake<IValidatableObjectAdapter>();

            this.validatorFactory =
                A.Fake<IPropertyValidatorFactory>();

            A.CallTo(() => this.validatorFactory.GetValidators(typeof(ModelUnderTest)))
               .Returns(new[] { this.propertyValidator1, this.propertyValidator2 });

            this.validator =
                new DataAnnotationsValidator(typeof(ModelUnderTest), this.validatorFactory, this.validatableObjectAdapter);
        }
        public void Should_read_string_length_annotation()
        {
            // Given, When
            var subject = new DataAnnotationsValidator(typeof(TestModel));

            // Then
            subject.Description.Rules.ShouldHave(r => r.RuleType == "StringLength" && r.MemberNames.Contains("FirstName"));
        }
        public void Should_read_self_annotation()
        {
            // Given, When
            var subject = new DataAnnotationsValidator(typeof(TestModel));

            // Then
            subject.Description.Rules.ShouldHave(r => r.RuleType == "Self" && r.MemberNames == null);
        }
        public void Should_use_custom_validator()
        {
            // Given
            DataAnnotationsValidator.RegisterAdapter(typeof(OopsValidationAttribute), (a, d) => new OopsAdapter(a));

            // When
            var subject = new DataAnnotationsValidator(typeof(TestModel));

            // Then
            subject.Description.Rules.ShouldHave(r => r.RuleType == "Oops" && r.MemberNames == null);
        }