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); }
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); }
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(); }
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(); }
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); }
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); }
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); }
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); }
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(); }
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(); }
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); }
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"); }
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();
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); }
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();
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(); }
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); }
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)); }
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); }
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); }
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); }