public void CreateOrder(Order order) { if (!ModelState.IsValid) { var error = new StringBuilder(); foreach (var e in ModelState.Errors) { error.Append(string.Format("Validation error on {0}:{1}\n", e.MemberName, e.Message)); } throw new FaultException(error.ToString()); } }
public void Test_complex_validation() { // Arrange var order = new Order { OrderId = 1, FirstOrderDetail = new OrderDetail { OrderId = 1, Price = 100, ProductName = "P123456" }, Details = new List<OrderDetail> { new OrderDetail { OrderId = 1, Price = 100, ProductName = "P123456" }, new OrderDetail { OrderId = 1, Price = 200, ProductName = "P2" } } }; ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(() => order, order.GetType()); // Action var validator = ModelValidator.GetModelValidator(metadata); var validationResults = validator.Validate(null).ToList(); // Assert Assert.AreEqual(2, validationResults.Count()); Assert.AreEqual("Order.Details[0].ProductName", validationResults[0].MemberName); Assert.AreEqual("The field ProductName must be a string with a maximum length of 5.", validationResults[0].Message); Assert.AreEqual("Order.FirstOrderDetail.ProductName", validationResults[1].MemberName); Assert.AreEqual("The field ProductName must be a string with a maximum length of 5.", validationResults[1].Message); }
public void CreateOrder(Order order) { ColorConsole.WriteLine(ConsoleColor.Green, "CreateOrder was called succesfully."); }