Esempio n. 1
0
        public void NoRequiredFieldsNoValuesSupplied()
        {
            List <ValidationResult> validationResults = new List <ValidationResult>();
            var result = RecursiveValidator.TryValidateObject(new BodySimple(), validationResults, true);

            result.ShouldBeTrue();
            validationResults.ShouldBeEmpty();
        }
        public bool IsValid(object instance, out IEnumerable<ValidationEngineMessage> results)
        {
            var validator = new RecursiveValidator(true, _container);
            var valid = validator.TryValidateObject(instance);

            results = valid
                ? new List<ValidationEngineMessage>()
                : _mapper.Map(validator.Results);
            return valid;
        }
        public void Validate(object instance)
        {
            var validator = new RecursiveValidator(true, _container);
            var valid = validator.TryValidateObject(instance);

            if (!valid)
            {
                throw new ValidationEngineException(
                    _mapper.Map(validator.Results));
            }
        }
Esempio n. 4
0
        public void RequiredFieldsAllValuesSupplied()
        {
            BodyWithRequiredFields body = new BodyWithRequiredFields()
            {
                Param1 = "value1",
                Param2 = "value2"
            };

            List <ValidationResult> validationResults = new List <ValidationResult>();
            var result = RecursiveValidator.TryValidateObject(body, validationResults, true);

            result.ShouldBeTrue();
            validationResults.ShouldBeEmpty();
        }
Esempio n. 5
0
        public void RequiredFieldsNotSuppliedOrEmpty(string param1)
        {
            BodyWithRequiredFields body = new BodyWithRequiredFields()
            {
                Param1 = param1,
                Param2 = "value2"
            };

            List <ValidationResult> validationResults = new List <ValidationResult>();
            var result = RecursiveValidator.TryValidateObject(body, validationResults, true);

            result.ShouldBeFalse();
            validationResults.ShouldContain(x => x.ErrorMessage.Contains("Param1 is required"));
        }
Esempio n. 6
0
        public void ShouldValidateReadOnlyProperties()
        {
            ClassWithRequiredReadonlyProperty body = new ClassWithRequiredReadonlyProperty()
            {
                Child = new ClassWithRequiredReadonlyProperty()
            };

            List <ValidationResult> validationResults = new List <ValidationResult>();
            var result = RecursiveValidator.TryValidateObject(body, validationResults, true);

            result.ShouldBeFalse();
            validationResults.ShouldContain(x => x.MemberNames.Contains("ReadOnlyProperty") && x.ErrorMessage.Contains("ReadOnlyProperty field is required"));
            validationResults.ShouldContain(x => x.MemberNames.Contains("Child.ReadOnlyProperty") && x.ErrorMessage.Contains("ReadOnlyProperty field is required"));
        }
Esempio n. 7
0
        public void NestedChildFieldMissingRequiredValue()
        {
            BodyWithNestedRequiredUrlField body = new BodyWithNestedRequiredUrlField()
            {
                UrlParam = new BodyWithUrlField()
                {
                    Param1 = null
                }
            };

            List <ValidationResult> validationResults = new List <ValidationResult>();
            var result = RecursiveValidator.TryValidateObject(body, validationResults, true);

            result.ShouldBeFalse();
            validationResults.ShouldContain(x => x.MemberNames.Contains("UrlParam.Param1") && x.ErrorMessage.Contains("Param1 is required"));
        }
        public void ValidatesObjectRecursively()
        {
            var root = new RootClass();
            var validationContext = new ValidationContext(root);
            var validationResults = new List <ValidationResult>();

            // Validate recursively
            Assert.False(RecursiveValidator.TryValidateObject(root, validationContext, validationResults));
            Assert.Equal(6, validationResults.Count);
            Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == nameof(RootClass.SomeString));
            Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.SingleChild)}.{nameof(ChildClass.SomeOtherString)}");
            Assert.Contains(validationResults,
                            r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.SingleChild)}.{nameof(ChildClass.NestedItems)}[0].{nameof(ChildItem.SomeOtherOtherString)}");
            Assert.Contains(validationResults,
                            r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.SingleChild)}.{nameof(ChildClass.NestedItems)}[1].{nameof(ChildItem.SomeOtherOtherString)}");
            Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.Items)}[0].{nameof(ChildItem.SomeOtherOtherString)}");
            Assert.Contains(validationResults, r => r.MemberNames.FirstOrDefault() == $"{nameof(RootClass.Items)}[1].{nameof(ChildItem.SomeOtherOtherString)}");
        }
Esempio n. 9
0
        public void NestedCollectionFieldMissingRequiredValue()
        {
            BodyWithNestedRequiredUrlField body = new BodyWithNestedRequiredUrlField()
            {
                UrlParam = new BodyWithUrlField()
                {
                    Param1 = "https://google.com"
                },
                Items = new BodyWithRequiredFields[]
                {
                    new BodyWithRequiredFields()
                    {
                        Param1 = null,
                        Param2 = "blah"
                    }
                }
            };

            List <ValidationResult> validationResults = new List <ValidationResult>();
            var result = RecursiveValidator.TryValidateObject(body, validationResults, true);

            result.ShouldBeFalse();
            validationResults.ShouldContain(x => x.MemberNames.Contains("Items[0].Param1") && x.ErrorMessage.Contains("Param1 is required"));
        }
Esempio n. 10
0
        public void ThrowsArgumentNullException()
        {
            List <ValidationResult> validationResults = new List <ValidationResult>();
            var exception = ShouldThrowExtensions.ShouldThrow <ArgumentNullException>(() => RecursiveValidator.TryValidateObject(null, validationResults, true));

            exception.ParamName.ShouldBe("instance");
        }
 public bool IsValid(object instance)
 {
     var validator = new RecursiveValidator(true, _container);
     return validator.TryValidateObject(instance);
 }