public void ReturnSnakeCaseForTwoModelStateErrorsWhenSnakeCaseIsSet() { //--Arrange var attribute = new RidgidValidateModelAttribute(); var actionContext = new HttpActionContext(); actionContext.ModelState["Field"] = new ModelState { Errors = { ModelStateCustomErrorMessage.Create(1, "ErrorMessage1"), ModelStateCustomErrorMessage.Create(2, "ErrorMessage2") } }; FormatResponseMessage.SetSnakeCaseSetting(true); //--Act attribute.OnActionExecuting(actionContext); var response = actionContext.Response; //--Assert var contentAsString = ContentAsString(actionContext, response); contentAsString.ShouldBe( "{\"errors\":[{\"error_id\":1,\"debug_error_message\":\"ErrorMessage1\"},{\"error_id\":2,\"debug_error_message\":\"ErrorMessage2\"}]}"); }
public override string FormatErrorMessage(string fieldName) { var errorMessage = CustomErrorMessage ?? CreateErrorMessage(fieldName); return(ModelStateCustomErrorMessage.Create(ErrorId, errorMessage)); }
public void InvalidateCorrectlyWithCustomErrorMessage() { //--Arrange var model = new ModelWithRequiredFieldWithCustomErrorMessage(); var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); result[0].ErrorMessage .ShouldBe(ModelStateCustomErrorMessage.Create(1, "CustomMessage")); }
public void UseSnakeCaseForFieldNameWhenSet() { //--Arrange var model = new ModelWithRequiredFieldWithoutCustomErrorMessage(); var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(true); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); const string defaultErrorMsg = "The 'multiple_worded_field' field is required."; result[0].ErrorMessage .ShouldBe(ModelStateCustomErrorMessage.Create(1, defaultErrorMsg)); }
public void InvalidateCorrectlyWhenMaxAndMinEqualWhenSnakeCase() { //--Arrange var model = new ModelWithStringLengthMinAndMaxEqualField { MultipleWordedField = "1234" }; var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(true); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); result[0].ErrorMessage.ShouldBe(ModelStateCustomErrorMessage.Create(1, "The 'multiple_worded_field' field must be '2' characters long.")); }
public void InvalidateCorrectlyWithCustomErrorMessage() { //--Arrange var model = new ModelWithStringLengthFieldWithCustomErrorMessage { Field = "invalid" }; var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(false); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); result[0].ErrorMessage .ShouldBe(ModelStateCustomErrorMessage.Create(1, "CustomMessage")); }
public void ReturnCamelCaseForOneModelStateErrorByDefault() { //--Arrange var attribute = new RidgidValidateModelAttribute(); var actionContext = new HttpActionContext(); actionContext.ModelState["Field"] = new ModelState { Errors = { ModelStateCustomErrorMessage.Create(1, "ErrorMessage") } }; FormatResponseMessage.SetSnakeCaseSetting(false); //--Act attribute.OnActionExecuting(actionContext); var response = actionContext.Response; //--Assert var contentAsString = ContentAsString(actionContext, response); contentAsString.ShouldBe("{\"errors\":[{\"errorId\":1,\"debugErrorMessage\":\"ErrorMessage\"}]}"); }
public void UseSnakeCaseForFieldNameWhenSet() { //--Arrange var model = new ModelWithStringLengthField { MultipleWordedField = "1" }; var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(true); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); const string defaultErrorMsg = "The 'multiple_worded_field' field must be between '2' and '3' characters long."; result[0].ErrorMessage.ShouldBe(ModelStateCustomErrorMessage.Create(1, defaultErrorMsg)); }
public void InvalidateCorrectlyWithoutCustomErrorMessage() { //--Arrange var model = new ModelWithDateTimeFieldWithoutCustomErrorMessage { CreatedDate = "invalidDate" }; var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(false); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); const string defaultErrorMsg = "The 'CreatedDate' must be in ISO-8601 format, (i.e. yyyy-mm-ddThh:mm:ss.ffffff)."; result[0].ErrorMessage .ShouldBe(ModelStateCustomErrorMessage.Create(1, defaultErrorMsg)); }
public void InvalidateCorrectlyWithoutCustomErrorMessage() { //--Arrange var model = new ModelWithMinLengthFieldWithoutCustomErrorMessage { MultipleWordedField = "1" }; var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(false); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); const string defaultErrorMsg = "The 'MultipleWordedField' field must be less than '2' characters long."; result[0].ErrorMessage .ShouldBe(ModelStateCustomErrorMessage.Create(1, defaultErrorMsg)); }
public void ConvertFieldNameToSnakeCaseIfSetInAppConfig() { //--Arrange var model = new ModelWithEmailAddressFieldWithoutCustomErrorMessage() { MultipleWordedField = "invalidemail" }; var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(true); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); const string defaultErrorMsg = "The 'multiple_worded_field' field is an invalid email address."; result[0].ErrorMessage .ShouldBe(ModelStateCustomErrorMessage.Create(1, defaultErrorMsg)); }
public void SetFieldnameToSnakeCaseWhenSetInAppConfig() { //--Arrange var model = new ModelWithMaxLengthFieldWithoutCustomErrorMessage { MultipleWordedField = "123" }; var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(true); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); const string defaultErrorMsg = "The 'multiple_worded_field' field cannot be greater than '2' characters long."; result[0].ErrorMessage .ShouldBe(ModelStateCustomErrorMessage.Create(1, defaultErrorMsg)); }
public void InvalidateCorrectlyWithoutCustomErrorMessage() { //--Arrange var model = new ModelWithRegularExpressionFieldWithoutCustomErrorMessage { MultipleWordedField = "invalidregex" }; var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(false); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); const string defaultErrorMsg = "The 'MultipleWordedField' field must match the regular expression: 'a|b'."; result[0].ErrorMessage .ShouldBe(ModelStateCustomErrorMessage.Create(1, defaultErrorMsg)); }
public void UseSnakeCaseInDefaultErrorMessageWhenSet() { //--Arrange var model = new ModelWithPositiveIntFieldWithoutCustomErrorMessage { CustomId = -1 }; var validationContext = new ValidationContext(model, null, null); var result = new List <ValidationResult>(); FormatResponseMessage.SetSnakeCaseSetting(true); //--Act var valid = Validator.TryValidateObject(model, validationContext, result, true); //--Assert valid.ShouldBeFalse(); result.Count.ShouldBe(1); string defaultErrorMsg = $"The 'custom_id' field must be an integer value between '0' and '{int.MaxValue}'."; result[0].ErrorMessage .ShouldBe(ModelStateCustomErrorMessage.Create(1, defaultErrorMsg)); }