public void ListRequiredIfPropertyValueIsNull() { ListRequiredIfTestObject obj = new ListRequiredIfTestObject(); obj.StringListIfValue = null; ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "ReqListIfPropertyIsNull"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.ReqListIfPropertyIsNull, ctx, validationResults).Should().BeFalse(); }
public void StringListNotRequiredNullList() { // nulllist ok because the property value doesn't match ListRequiredIfTestObject obj = new ListRequiredIfTestObject(); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "StringList"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.StringList, ctx, validationResults).Should().BeTrue(); }
public void StringListNotRequiredEmptyList() { // empty list ok because the property value doesn't match to what is needed for the list to be required ListRequiredIfTestObject obj = new ListRequiredIfTestObject(); obj.StringList = new List <string>(); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "StringList"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.StringList, ctx, validationResults).Should().BeTrue(); }
public void StringListRequiredNullList() { // list required because property value matches to value that indicates the list is needed ListRequiredIfTestObject obj = new ListRequiredIfTestObject(); obj.StringListIfValue = "ptv"; ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "StringList"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.StringList, ctx, validationResults).Should().BeFalse(); }
public void StringListRequiredEmptyList() { // empty list is not valid when the property value matches the value given in the attribute ListRequiredIfTestObject obj = new ListRequiredIfTestObject(); obj.StringListIfValue = "ptv"; obj.StringList = new List <string>(); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "StringList"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.StringList, ctx, validationResults).Should().BeFalse(); }
public void RequiredListCannotHaveEmptyOrNullValues(string extraValue) { ListRequiredIfTestObject obj = new ListRequiredIfTestObject(); obj.StringListIfValue = "ptv"; obj.StringList = new List <string>() { "some", "value", extraValue }; ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "StringList"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.StringList, ctx, validationResults).Should().BeFalse(); }
public void ShouldThrowInvalidPropertyNameDefined() { ListRequiredIfTestObject obj = new ListRequiredIfTestObject(); obj.DemoItems = new List <SomeDemoObject>(); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "DemoItems"; List <ValidationResult> validationResults = new List <ValidationResult>(); Action act = () => Validator.TryValidateProperty(obj.DemoItems, ctx, validationResults); // the attribute should indicate that the desired property was not found and not just silently return success // case: some property has value hsl and in that case the list should contain values // now if there is a typo in the property name, no values are required in the list by the validation act.ShouldThrowExactly <InvalidOperationException>().WithMessage("Property 'TypoInPropertyName' not found."); }
public void NoValidationContextShouldNotCauseInternalNullReferenceException() { // the current implementation crashes if the public IsValid(object) is called // anyways the attribute can't really be used in this way as it dependant on another property on the object // but it shouldn't crash, it should check that it has the validationcontext before using it ListRequiredIfAttribute attr = new ListRequiredIfAttribute("StringListIfValue", "ptv"); ListRequiredIfTestObject obj = new ListRequiredIfTestObject(); obj.StringListIfValue = "ptv"; obj.StringList = new List <string>() { "vrk" }; Action act = () => attr.IsValid(obj); act.ShouldThrowExactly <InvalidOperationException>("validationContext cannot be null."); }
public void ShouldThrowInvalidPropertyTypeUsage() { // Comment: should throw an exception that indicates that the property is used on a wrong property type // instead just saying that the value is not valid var obj = new ListRequiredIfTestObject(); obj.SomeDictionary.Add("ptv", "vrk"); var ctx = new ValidationContext(obj) { MemberName = "SomeDictionary" }; var validationResults = new List <ValidationResult>(); Action act = () => Validator.TryValidateProperty(obj.SomeDictionary, ctx, validationResults); // currently throws validation exception because dictionary doesn't implement ilist // the implementation should throw exception other than validation exception and indicate what is wrong act.ShouldThrowExactly <InvalidOperationException>("Attribute used on a property that doesn't implement IList."); }
public void PropertyNameStringListContainsNullAndEmptyString() { // the RequiredListIfPropertyValueList is required because value 'vrk' is in the PropertyValueList ListRequiredIfTestObject obj = new ListRequiredIfTestObject(); obj.PropertyValueList.Add("ptv"); obj.PropertyValueList.Add("hsl"); obj.PropertyValueList.Add(null); // should be fine to have a null value in the list obj.PropertyValueList.Add(string.Empty); // should be fine to have an empty string obj.PropertyValueList.Add("vrk"); obj.RequiredListIfPropertyValueList.Add("demo value"); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "RequiredListIfPropertyValueList"; List <ValidationResult> validationResults = new List <ValidationResult>(); // should be true because we have one value in the RequiredListIfPropertyValueList -list Validator.TryValidateProperty(obj.RequiredListIfPropertyValueList, ctx, validationResults).Should().BeTrue(); }