Example #1
0
        public void SkipIndexer()
        {
            var builder = new ValidatorBuilder <object>();
            var count   = 0;

            builder.AddEmptyConstraint(true);
            builder.AddKnownType <ClassWithIndexer>();
            builder.AddPropertyConstraintFactory((property, _) =>
            {
                Assert.Empty(property.GetIndexParameters());

                count++;

                return(null);
            });

            var validator = builder.Build();
            var context   = new TestValidationContext();

            validator.Validate(new ClassWithIndexer(), context.Context);

            Assert.Equal(1, count);
            Assert.Empty(context.Errors);
            Assert.Empty(context.UnknownTypes);
        }
Example #2
0
        public void ValidateCancelTest()
#if NO_CANCELLATION_TOKEN
        {
            var context = new TestValidationContext();

            Assert.False(context.Context.IsCancelled);

            // Must use a type with multiple types as cancellation is checked at the start of processing each type
            var validator = ValidatorBuilder.Create()
                            .AddKnownType <TestClass1>()
                            .AddPropertyFilter <TestClass2>()
                            .AddConstraint(_ => new DelegateConstraint(() =>
            {
                context.Context.Cancel();
            }))
                            .Build();

            var instance = new TestClass1
            {
                Instance = new TestClass2(),
            };

            Assert.Throws <OperationCanceledException>(() => validator.Validate(instance, context.Context));
            Assert.True(context.Context.IsCancelled);
        }
Example #3
0
        public void TypeConstraintDerived(bool input)
        {
            var message      = Guid.NewGuid().ToString();
            var builder      = new ValidatorBuilder <object>();
            var constraintId = Guid.NewGuid().ToString();

            builder.AddTypeConstraint <SimpleBoolean>((c, ctx) =>
            {
                if (!c.IsTrue)
                {
                    ctx.RaiseError(message);
                }
            }, constraintId);

            var validator = builder.Build();

            var context = new TestValidationContext();

            validator.Validate(new SimpleBooleanDerived {
                IsTrue = input
            }, context.Context);

            if (input)
            {
                Assert.Empty(context.Errors);
            }
            else
            {
                var error = Assert.Single(context.Errors);

                Assert.Equal(constraintId, error.Id);
                Assert.Equal(message, error.Message);
            }
        }
Example #4
0
        public void EnumerablePropertiesWithCycle()
        {
            // Arrange
            var message = Guid.NewGuid().ToString();
            var key     = Guid.NewGuid().ToString();
            var builder = new ValidatorBuilder <object>();

            builder.AddTypeConstraint <CyclicEnumerable>((i, ctx) =>
            {
                if (i.Name is null)
                {
                    ctx.RaiseError(message);
                }
            }, key);

            var validator = builder.Build();

            var context = new TestValidationContext();
            var item    = new CyclicEnumerable {
                Name = "0"
            };

            item.Others = new[]
            {
                new CyclicEnumerable {
                    Name = "1", Others = new[] { new CyclicEnumerable {
                                                     Name = "2"
                                                 } }
                },
                new CyclicEnumerable {
                    Name = null, Others = new[] { item }
                },
                new CyclicEnumerable {
                    Name = "3"
                },
            };

            // Act
            validator.Validate(item, context.Context);

            // Assert
            var error = Assert.Single(context.Errors);

            Assert.Equal(message, error.Message);
            Assert.Equal(key, error.Id);

            Assert.Collection(context.Items,
                              i => Assert.Equal(item, i),
                              i => Assert.Equal(item.Others, i),
                              i => Assert.Equal(item.Others[0], i),
                              i => Assert.Equal(item.Others[1], i),
                              i => Assert.Equal(item.Others[2], i),
                              i => Assert.Equal(item.Others[0].Others, i),
                              i => Assert.Equal(item.Others[0].Others[0], i),
                              i => Assert.Equal(item.Others[1].Others, i));
        }
Example #5
0
        public async Task AsyncValidation()
        {
            const int ParallelCount = 2;

            var list    = new HashSet <int>();
            var cts     = new CancellationTokenSource();
            var builder = new ValidatorBuilder <object>();

            builder.AddKnownType <RecursiveClass>();
            builder.AddPropertyFilter <RecursiveClass>();
            builder.AddConstraint(_ => new DelegateConstraint(instanceValue =>
            {
                lock (list)
                {
                    if (Task.CurrentId.HasValue)
                    {
                        list.Add(Task.CurrentId.Value);
                    }
                }

                return(true);
            }));
            var validator = builder.Build();

            var instance = new RecursiveClass
            {
                Entry1 = new RecursiveClass
                {
                    Entry1 = new RecursiveClass
                    {
                    },
                    Entry2 = new RecursiveClass
                    {
                    },
                },
                Entry2 = new RecursiveClass
                {
                    Entry1 = new RecursiveClass
                    {
                    },
                    Entry2 = new RecursiveClass
                    {
                    },
                },
            };

            var context = new TestValidationContext();

            context.Context.MaxDegreeOfParallelism = ParallelCount;
            await validator.ValidateAsync(instance, context.Context).ConfigureAwait(false);

            Assert.Equal(ParallelCount, list.Count);
        }
        public void TwoInstances()
        {
            var instance = new TestClass
            {
                Value = new TestClass(),
            };
            var context = new TestValidationContext();

            GetValidator().Validate(instance, context.Context);

            Assert.Empty(context.Errors);
            Assert.Equal(2, context.Items.Count);
        }
        public void SingleInstance()
        {
            var instance = new TestClass
            {
            };

            var context = new TestValidationContext();

            GetValidator().Validate(instance, context.Context);

            Assert.Empty(context.Errors);
            Assert.Single(context.Items);
        }
Example #8
0
        public void EnumerableProperties()
        {
            // Arrange
            var message = Guid.NewGuid().ToString();
            var key     = Guid.NewGuid().ToString();
            var builder = new ValidatorBuilder <object>();

            builder.AddKnownType <TestWithEnumerableProperty1>();
            builder.AddTypeConstraint <StringItem>((i, ctx) =>
            {
                if (i.Item is null)
                {
                    ctx.RaiseError(message);
                }
            }, key);

            var validator = builder.Build();

            var context = new TestValidationContext();
            var item    = new TestWithEnumerableProperty1
            {
                Items = new[]
                {
                    new StringItem {
                        Item = "1"
                    },
                    new StringItem {
                        Item = null
                    },
                    new StringItem {
                        Item = "3"
                    },
                },
            };

            // Act
            validator.Validate(item, context.Context);

            // Assert
            var error = Assert.Single(context.Errors);

            Assert.Equal(message, error.Message);
            Assert.Equal(key, error.Id);

            Assert.Collection(context.Items,
                              i => Assert.Equal(item, i),
                              i => Assert.Equal(item.Items, i),
                              i => Assert.Equal(item.Items[0], i),
                              i => Assert.Equal(item.Items[1], i),
                              i => Assert.Equal(item.Items[2], i));
        }
        public void MultipleInstanceWithCycle()
        {
            var instance1 = new TestClass();
            var instance  = new TestClass
            {
                Value = instance1,
            };

            instance1.Value = instance;

            var context = new TestValidationContext();

            GetValidator().Validate(instance, context.Context);

            Assert.Empty(context.Errors);
            Assert.Equal(2, context.Items.Count);
        }
Example #10
0
        public void NoPattern()
        {
            var builder = new ValidatorBuilder <object>();

            builder.AddRegexAttributeConstraint <RegexAttribute>(r => r.Pattern);
            builder.AddKnownType <TestNoPattern>();
            builder.AddEmptyConstraint();

            var validator = builder.Build();
            var context   = new TestValidationContext();

            var item = new TestNoPattern {
                Test = "hello"
            };

            validator.Validate(item, context.Context);

            Assert.Empty(context.Errors);
            Assert.Single(context.Items);
        }
Example #11
0
        public void SamePatternIsCached()
        {
            var builder = new ValidatorBuilder <object>();

            builder.AddRegexAttributeConstraint <RegexAttribute>(r => r.Pattern);
            builder.AddKnownType <DuplicatePattern>();

            var validator = builder.Build();
            var instance  = new DuplicatePattern();
            var context   = new TestValidationContext();

            validator.Validate(instance, context.Context);

            Assert.Equal(2, context.Errors.Count);

            var error1 = Assert.IsType <Regex>(context.Errors[0].Context);
            var error2 = Assert.IsType <Regex>(context.Errors[1].Context);

            Assert.Same(error1, error2);
        }
        public void RecursiveDescentBySubtype()
        {
            var instance1 = new TestClass();
            var instance  = new TestClass
            {
                Value = instance1,
            };

            instance1.Value = instance;

            var builder = new ValidatorBuilder <object>();

            builder.AddKnownType <TestClass>();
            builder.AddPropertyFilter <TestClassBase>();
            builder.AddEmptyConstraint();
            var validator = builder.Build();
            var context   = new TestValidationContext();

            validator.Validate(instance, context.Context);

            Assert.Empty(context.Errors);
            Assert.Equal(2, context.Items.Count);
        }
        public void SimpleProperty()
        {
            const string Value   = "hello!";
            var          count   = 0;
            var          builder = new ValidatorBuilder <object>();

            builder
            .ForType <Test1>()
            .AddProperty(t => t.Instance);

            var test = new Test1
            {
                Instance = new Test2
                {
                    Member = Value,
                },
            };

            builder.AddConstraint(_ => new DelegateConstraint(value =>
            {
                if (value is string)
                {
                    Assert.Equal(Value, value);
                    count++;
                }

                return(true);
            }));

            var validator = builder.Build();
            var context   = new TestValidationContext();

            validator.Validate(test, context.Context);

            Assert.Equal(1, count);
        }
Example #14
0
        public void StaticProperty()
        {
            var builder = new ValidatorBuilder <object>();
            var count   = 0;

            builder.AddEmptyConstraint(true);
            builder.AddKnownType <StaticPropertyBoolean>();
            builder.AddPropertyConstraintFactory((property, _) =>
            {
                count++;
                Assert.NotEqual(typeof(StaticPropertyBoolean).GetProperty(nameof(StaticPropertyBoolean.Test)), property);

                return(null);
            });

            var validator = builder.Build();
            var context   = new TestValidationContext();

            validator.Validate(new StaticPropertyBoolean(), context.Context);

            Assert.Equal(1, count);
            Assert.Empty(context.Errors);
            Assert.Empty(context.UnknownTypes);
        }