public void Guid_Required_Fails_For_Nullable_Null()
        {
            var result = ValitRules <Model>
                         .Create()
                         .Ensure(m => m.NullValue, _ => _
                                 .Required())
                         .For(_model)
                         .Validate();

            result.Succeeded.ShouldBe(false);
        }
示例#2
0
        public void DateTimeOffset_Required_Returns_Proper_Results_For_Nullable_Value(bool useNullValue, bool expected)
        {
            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => useNullValue? m.NullValue : m.NullableValue, _ => _
                                          .Required())
                                  .For(_model)
                                  .Validate();

            Assert.Equal(result.Succeeded, expected);
        }
示例#3
0
        public void Enumerable_MinItems_Succeeds_If_ExpectedItemsNumber_Is_Less_Than_Number_Of_Items_In_Collection()
        {
            var result = ValitRules <Model>
                         .Create()
                         .Ensure(m => m.Collection, _ => _
                                 .MinItems(1))
                         .For(_model)
                         .Validate();

            result.Succeeded.ShouldBeTrue();
        }
示例#4
0
        public void Int16_IsGreaterThanOrEqualTo_Returns_Proper_Results_For_Not_Nullable_Value_And_Nullable_Value(short?value, bool expected)
        {
            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => m.Value, _ => _
                                          .IsGreaterThanOrEqualTo(value))
                                  .For(_model)
                                  .Validate();

            Assert.Equal(result.Succeeded, expected);
        }
        public void DateTimeOffset_IsAfterUtcNow_For_Nullable_Value_Fails_When_Value_Is_Null()
        {
            var result = ValitRules <Model>
                         .Create()
                         .Ensure(m => m.NullValue, _ => _
                                 .IsAfterUtcNow())
                         .For(_model)
                         .Validate();

            result.Succeeded.ShouldBeFalse();
        }
示例#6
0
        public void Int16_IsEqualTo_Returns_Proper_Results_For_Nullable_Value_And_Not_Nullable_Value(bool useNullValue, short value, bool expected)
        {
            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => useNullValue? m.NullValue : m.NullableValue, _ => _
                                          .IsEqualTo(value))
                                  .For(_model)
                                  .Validate();

            result.Succeeded.ShouldBe(expected);
        }
示例#7
0
        public void Int32_IsNonZero_Returns_Proper_Results_For_Nullable_Value(bool useZeroValue, bool expected)
        {
            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => useZeroValue? m.NullableZeroValue : m.NullableValue, _ => _
                                          .IsNonZero())
                                  .For(_model)
                                  .Validate();

            Assert.Equal(result.Succeeded, expected);
        }
示例#8
0
        public void Int32_IsNonZero_Fails_For_Null_Value()
        {
            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => m.NullValue, _ => _
                                          .IsNonZero())
                                  .For(_model)
                                  .Validate();

            Assert.Equal(result.Succeeded, false);
        }
示例#9
0
        public void UInt32_IsLessThanOrEqualTo_Returns_Proper_Results_For_Not_Nullable_Value_And_Nullable_Value(uint?value, bool expected)
        {
            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => m.Value, _ => _
                                          .IsLessThanOrEqualTo(value))
                                  .For(_model)
                                  .Validate();

            result.Succeeded.ShouldBe(expected);
        }
示例#10
0
        public void String_Email_Fails_For_Invalid_Email(string value)
        {
            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => value, _ => _
                                          .Email())
                                  .For(_model)
                                  .Validate();

            result.Succeeded.ShouldBe(false, value);
        }
        public void Int16_IsGreaterThan_Returns_Proper_Results_For_Nullable_Values(bool useNullValue, short?value, bool expected)
        {
            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => useNullValue? m.NullValue : m.NullableValue, _ => _
                                          .IsGreaterThan(value))
                                  .For(_model)
                                  .Validate();

            Assert.Equal(result.Succeeded, expected);
        }
        public void DateTimeOffset_IsAfterUtcNow_For_Not_Nullable_Value_Succeeds_When_Value_Is_After_DateTimeOffset_Now()
        {
            var result = ValitRules <Model>
                         .Create()
                         .Ensure(m => m.AfterNowValue, _ => _
                                 .IsAfterUtcNow())
                         .For(_model)
                         .Validate();

            result.Succeeded.ShouldBeTrue();
        }
示例#13
0
        public void EmptyMessageProvider_Does_Not_Add_Any_Message()
        {
            var result = ValitRules <Model> .Create()
                         .Ensure(m => m.Value, _ => _
                                 .MaxLength(4)
                                 .WithMessageKey(Key))
                         .For(_model)
                         .Validate();

            result.Succeeded.ShouldBe(false);
            result.ErrorMessages.ShouldBeEmpty();
        }
        public void Validation_With_Complete_Strategy_Succeeds_When_Valid_Collection_Is_Given()
        {
            var rootObject = Model.GetValid();

            var result = ValitRules <Model>
                         .Create()
                         .EnsureFor(m => m.NestedObjectCollection, _modelValitator)
                         .For(rootObject)
                         .Validate();

            result.Succeeded.ShouldBeTrue();
        }
示例#15
0
        public void Property_When_Returns_ProperResult(bool r1, bool r2, bool expected)
        {
            var result = ValitRules <Model> .Create()
                         .Ensure(m => m.NullRefProperty, _ => _
                                 .Required()
                                 .When(m => r1)
                                 .When(m => r2))
                         .For(_model)
                         .Validate();

            result.Succeeded.ShouldBe(expected);
        }
        public void Validation_Succeeds_When_Valid_NestedObject_Is_Given()
        {
            var rootObject = Model.GetValid();

            var result = ValitRules <Model>
                         .Create()
                         .Ensure(m => m.NestedObject, new NestedModelRulesProvider())
                         .For(rootObject)
                         .Validate();

            Assert.True(result.Succeeded);
        }
示例#17
0
        public void Property_WithMessage_Is_Added_For_Not_Valid_Check_If_When_Is_Satisfied()
        {
            var result = ValitRules <Model> .Create()
                         .Ensure(m => m.NullRefProperty, _ => _
                                 .Required()
                                 .When(m => true)
                                 .WithMessage("Arrrgh"))
                         .For(_model)
                         .Validate();

            result.ErrorMessages.ShouldContain("Arrrgh");
        }
示例#18
0
 public IValitResult Validate(AddressDto dto, IValitStrategy strategy = null)
 => ValitRules <AddressDto>
 .Create()
 .WithStrategy(s => s.Complete)
 .Ensure(a => a.City, _ => _
         .Required())
 .Ensure(a => a.Street, _ => _
         .Required())
 .Ensure(a => a.ZipCode, _ => _
         .Required())
 .For(dto)
 .Validate();
示例#19
0
        public void DefaultMessage_Is_Added_If_No_ErrorMessage_Is_Given()
        {
            var result = ValitRules <Model> .Create()
                         .Ensure(m => m.Value, _ => _
                                 .IsNegative())
                         .Ensure(m => m.NullValue, _ => _
                                 .Required())
                         .For(_model)
                         .Validate();

            result.ErrorMessages.Length.ShouldBe(2);
        }
        public void Property_WithErrorCode_Returns_ProperResult()
        {
            var result = ValitRules <Model> .Create()
                         .Ensure(m => m.NullRefProperty, _ => _
                                 .Required()
                                 .WithErrorCode(1)
                                 .WithErrorCode(2))
                         .For(_model)
                         .Validate();

            result.ErrorCodes.Count().ShouldBe(2);
        }
        public void Property_WithErrorCode_Is_Not_Added_For_Not_Valid_Check_If_When_Is_Not_Satisfied()
        {
            var result = ValitRules <Model> .Create()
                         .Ensure(m => m.NullRefProperty, _ => _
                                 .Required()
                                 .When(m => false)
                                 .WithErrorCode(1))
                         .For(_model)
                         .Validate();

            result.ErrorCodes.ShouldNotContain(1);
        }
示例#22
0
 public IEnumerable <IValitRule <NestedModel> > GetRules()
 => ValitRules <NestedModel>
 .Create()
 .Ensure(m => m.NumericValue, _ => _
         .IsGreaterThan(10)
         .WithMessage("One"))
 .Ensure(m => m.StringValue, _ => _
         .Required()
         .WithMessage("Two")
         .Email()
         .WithMessage("Three"))
 .GetAllRules();
示例#23
0
        public void Validation_With_Complete_Strategy_Succeeds_When_Valid_Collection_Is_Given()
        {
            var rootObject = Model.GetValid();

            var result = ValitRules <Model>
                         .Create()
                         .EnsureFor(m => m.NestedObjectCollection, new NestedModelRulesProvider())
                         .For(rootObject)
                         .Validate();

            Assert.True(result.Succeeded);
        }
        public void Validation_Succeeds_When_Valid_NestedObject_Is_Given()
        {
            var rootObject = Model.GetValid();

            var result = ValitRules <Model>
                         .Create()
                         .Ensure(m => m.NestedObject, _modelValitator)
                         .For(rootObject)
                         .Validate();

            result.Succeeded.ShouldBeTrue();
        }
示例#25
0
        public void DateTime_IsAfter_Returns_Proper_Results_For_Not_Nullable_Value_And_Nullable_Value(string stringValue, bool expected)
        {
            DateTime?value = stringValue.AsNullableDateTime();

            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => m.Value, _ => _
                                          .IsAfter(value))
                                  .For(_model)
                                  .Validate();

            Assert.Equal(result.Succeeded, expected);
        }
        public void DateTime_IsBefore_Returns_Proper_Results_For_Nullable_Values(bool useNullValue, string stringValue, bool expected)
        {
            DateTime?value = stringValue.AsNullableDateTime();

            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => useNullValue? m.NullValue : m.NullableValue, _ => _
                                          .IsBefore(value))
                                  .For(_model)
                                  .Validate();

            Assert.Equal(result.Succeeded, expected);
        }
示例#27
0
        public void Property_Tag_Throws_When_Null_Tags_Set_Is_Given()
        {
            var exception = Record.Exception(() => {
                ValitRules <Model>
                .Create()
                .Ensure(m => m.Value1, _ => _
                        .Tag(null))
                .For(_model)
                .Validate();
            });

            exception.ShouldBeOfType(typeof(ValitException));
        }
示例#28
0
        public void TimeSpan_IsGreaterThanOrEqualTo_Returns_Proper_Result_For_Null_Left_Value(string strValue, bool expected)
        {
            TimeSpan value = TimeSpan.Parse(strValue);

            var result = ValitRules <Model>
                         .Create()
                         .Ensure(m => m.NullValue, _ => _
                                 .IsGreaterThanOrEqualTo(value))
                         .For(_model)
                         .Validate();

            result.Succeeded.ShouldBe(expected);
        }
示例#29
0
        public void TimeSpan_IsGreaterThanOrEqualTo_Returns_Proper_Result_For_Nullable_Values(string strValue, bool expected, bool useNull = false)
        {
            TimeSpan?value = useNull ? (TimeSpan?)null : TimeSpan.Parse(strValue);

            var result = ValitRules <Model>
                         .Create()
                         .Ensure(m => m.NullableValue, _ => _
                                 .IsGreaterThanOrEqualTo(value))
                         .For(_model)
                         .Validate();

            result.Succeeded.ShouldBe(expected);
        }
示例#30
0
        public void DateTimeOffset_IsAfter_Returns_Proper_Results_For_Nullable_Value_And_Not_Nullable_Value(bool useNullValue, string stringValue, bool expected)
        {
            var value = stringValue.AsDateTimeOffset();

            IValitResult result = ValitRules <Model>
                                  .Create()
                                  .Ensure(m => useNullValue? m.NullValue : m.NullableValue, _ => _
                                          .IsAfter(value))
                                  .For(_model)
                                  .Validate();

            Assert.Equal(result.Succeeded, expected);
        }