Esempio n. 1
0
        public async Task ValidClassIsValidWithCustomRulesForAsync()
        {
            var simple = new SimpleClass {
                Number = 2, Text = "A value"
            };
            var complex = new ComplexClass {
                Id = Guid.NewGuid(), Child = simple
            };
            var validator = new ComplexClassValidator(new SimpleClassValidator()) as Core.Validators.IValidator <ComplexClass>;
            var result    = await validator?.ValidateAsync(complex, ComplexClassValidator.RuleSetName);

            validator.Should().NotBeNull("because the base validator should impleent the base IValidator<T> contract");
            result.Should().NotBeNull("because a set should be returned even for valid objects");
            result.Should().HaveCount(0, "because a valid object should have no errors");
        }
Esempio n. 2
0
        public async Task CustomRuleIsNotRunWhenNotRequestedForAsync()
        {
            var simple = new SimpleClass {
                Number = 999, Text = null
            };
            var complex = new ComplexClass {
                Id = Guid.Empty, Child = simple
            };
            var validator = new ComplexClassValidator(new SimpleClassValidator()) as Core.Validators.IValidator <ComplexClass>;
            var result    = await validator?.ValidateAsync(complex);

            validator.Should().NotBeNull("because the base validator should impleent the base IValidator<T> contract");
            result.Should().NotBeNull("because a set should be returned even for valid objects");
            result.Should().HaveCount(1, "because child should not have been validated");
            result.Count(error => error.Code == ErrorCode.InvalidValue.ToString()).Should().Be(1, "because the id property was the only property with a bad value");
        }
Esempio n. 3
0
        public async Task InValidClassIsVDetectedWithCustomRulesForAsync()
        {
            var simple = new SimpleClass {
                Number = 999, Text = null
            };
            var complex = new ComplexClass {
                Id = Guid.Empty, Child = simple
            };
            var validator = new ComplexClassValidator(new SimpleClassValidator()) as Core.Validators.IValidator;
            var result    = await validator?.ValidateAsync(complex, ComplexClassValidator.RuleSetName);

            validator.Should().NotBeNull("because the base validator should impleent the base IValidator<T> contract");
            result.Should().NotBeNull("because a set should be returned even for valid objects");
            result.Should().HaveCount(3, "because the Id and both child properties were invalid");
            result.Count(error => error.Code == ErrorCode.InvalidValue.ToString()).Should().Be(1, "because the id property was the only property with a bad value");
            result.Count(error => error.Code == ErrorCode.NumberIsOutOfRange.ToString()).Should().Be(1, "because the number property was the only property with a range error");
            result.Count(error => error.Code == ErrorCode.ValueIsRequired.ToString()).Should().Be(1, "because the text property was the only property missing a value");
        }