public void Validate_PostalCodeModel_Valid() { var model = new PostalCodeModel { Country = Country.NL, PostalCode = PostalCode.Parse("2629 JD"), }; DataAnnotationsAssert.IsValid(model); }
/// <summary>Converts a string to a postal code, using the specified /// context and culture information. /// </summary> /// <param name="context"> /// An System.ComponentModel.ITypeDescriptorContext that provides a format context. /// </param> /// <param name="culture"> /// The System.Globalization.CultureInfo to use as the current culture. /// </param> /// <param name="value"> /// The System.Object to convert. /// </param> /// <returns> /// An System.Object that represents the converted value. /// </returns> /// <exception cref="NotSupportedException"> /// The conversion cannot be performed. /// </exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var str = value as string; if (value == null || str != null) { return(PostalCode.Parse(str, culture)); } return(base.ConvertFrom(context, culture, value)); }
public void Validate_PostalCodeModelWithInvalidPostalCode_WithError() { using (new CultureInfoScope("nl-BE")) { var model = new PostalCodeModel { Country = Country.BE, PostalCode = PostalCode.Parse("2629 JD"), }; DataAnnotationsAssert.WithErrors(model, ValidationTestMessage.Error("De postcode 2629JD is niet geldig voor België.", "PostalCode", "Country") ); } }
public void Validate_PostalCodeModelWithErrorByService_WithError() { var validator = new AnnotatedModelValidator(new TestServiceProvider { { typeof(AddressService), new AddressService() }, }); var model = new PostalCodeModel { Country = Country.NL, PostalCode = PostalCode.Parse("2629 XP"), }; DataAnnotationsAssert.WithErrors(model, validator, ValidationTestMessage.Error("Postal code does not exist.", "PostalCode") ); }
public bool PostalCodeExists(PostalCode code) => code == PostalCode.Parse("2629 JD");