How can I make .net core web api model validation return all validation errors including required field and numeric errors? - validation

My client is asking if all validation errors can be displayed at once.
String properties are marked with [JsonRequired]. If more than one string property is set to null in the request, validation error messages are returned in the response for each of the null properties. I have a few non-nullable decimal properties, also marked with [JsonRequired]. If decimal (or any numeric, primitive, etc.) properties are set to null in the request, obviously a conversion error is returned.
The JSON value could not be converted to System.Decimal.
However, the required property errors from earlier aren't displayed anymore.
What I have tried is setting the type to decimal? and implementing IValidatableObject on my model class. Then I can check if the value is null and return a ValidationResult with the error description. Then I have the same issue here because the validation "stops" at the decimal? property.

Related

How to map an input validation error to a specific error code in Spring

I am having a case in which I would like to do some input validation on the #RequestParams of an endpoint.
I know about Validators and Custom Validators, and my current strategy implies creating a wrapper object around the RequestParams, a custom validator and apply class level the annotation that triggers the custom validation.
My problem is that the custom validation is implementing ConstraintValidator, which means that the validator will either return true or false, and an error will be created by Spring with some text (I also know that I can change this text). My desire, however, is to create a custom payload back to the client. An example could be
class MyError {
int code;
String message;
}
The way to return this object is through a #ControllerAdvice Error handler, which understands that a ConstraintValidationException should return my custom payload format. However, I need to return different codes and messages for different reasons on the input validation failed. For example:
A Field is empty -> code XXX
A Field is formatted incorrectly -> code YYY
As far as I know, there is little customization possible on the exception that is reachable from my #ControllerAdvice, I can get a list of errors that happened but I cannot easily determine what happened. (Technically I can, but it would have to be based on the message string, which is pretty weak).
Is there a way to provide extra data to the Exception so I can distinguish from the #ControllerAdvice what happened and create my custom error response accordingly?
Am I approaching it the wrong way?
You can intercept the BindException (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/validation/BindException.html) with the #ExceptionHandler. This contains detailed information about all validation errors. For example, with e.getFieldErrors() you can access all errors associated with fields.
For example, for a field
#MyConstraint
#Length(min = 3)
private String field;
that failed validation you get the following information in the exception:
Field error in object data on field field: rejected value [XY]; codes [Length.data.field,Length.field,Length.java.lang.String,Length].
Field error in object data on field field: rejected value [XY]; codes [MyConstraint.data.field,MyConstraint.field,MyConstraint.java.lang.String,MyConstraint].
From this you can see that it failed the #Length constraint and the custom #MyConstraint constraint.

Spring MVC- Joda datetime error message for invalid date

I have a joda datetime attribute in my request object and I have used #DateTimeFormat for the date formatting.
But when I enter an invalid date(say day as 55), it displays the whole message
Failed to convert property value of type java.lang.String to required type org.joda.time.DateTime for property request.myDate; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "55/12/2014" from type java.lang.String to type org.joda.time.DateTime; nested exception is org.joda.time.IllegalFieldValueException: Cannot parse "55/12/2014": Value 55 for dayOfMonth must be in the range [1,31]
How can I change it to a custom message as the present message is generated by Spring.
In another SO question
Spring mvc Joda Datetime converter fail if invalid date
it is asked to make entry in message.properties.
But I'm not able to understand how will it pick the message from property file as I am not doing any error.rejectValue type of thing, So how will the message be identified.
The approach you linked to in your question should work. But the message key you should use is for instance typeMismatch.org.joda.time.DateTime.
Even though you are not manually rejecting the value anywhere Spring will automatically know where to look for the message based on the rules described in the JavaDoc of DefaultMessageCodesResolver.
In the situation you describe Spring will look for following codes specifically:
typeMismatch.request.myDate - Conversion message for attribute named "myDate" on an object named "request"
typeMismatch.myDate - Conversion message for attribute named myDate
typeMismatch.org.joda.time.DateTime - Conversion message for DateTime type
typeMismatch - General conversion error message
So you can define some general typeMismatch message like Incorrect format and then define more specific messages as needed. The more specific error codes have higher priority than those below it. So if you have both typeMismatch and typeMismatch.org.joda.time.DateTime messages defined, the latter will be used.

Get value of editbox of type date/time

I try to get the value of an editbox of type Date/Time. If I test it with
getComponent("dateField").value
or
getComponent("dateField").getSubmittedValue();
and print the output to the console. It always returns "null" if the field is empty or
the field does not contain a valide date. Because of this I can't differ between invalide input and empty input.
Is there a way to get the information if the field is empty?
It depends on the refresh phase you're testing.
getValue() will always return blank, because only content that can be converted to the underlying data type will be passed to it. Even if you disable validation, converter checks still run, because serious errors will occur if you try to put "this is not a date" into a Date/Time.
getSubmittedValue() will always be null if you're checking in Invoke Application or Render Response phases. That's because during the Update Model Values phase, the submittedValue property is passed to the value property and the submittedValue property nulled.
If you're checking in a validator, the text value entered by the user has not yet been checked against validation rules (validation) or that it can be converted to the right data type (conversion), so getValue() will return the value stored last time round and getSubmittedValue() will give the string value (e.g. "this is not a date").
So the answer is you should be able tell whether the field is empty in a validator, but bear in mind custom validators only run if you also have a required validator.

Spring MVC: Set property to null when bind fails

Is it possible to configure Spring to set the target property to null when binding fails?
For example, my bean has a date property which can have a pre-existing value when the form is displayed. If the user enters an invalid date, I want the property to be set to null; currently it retains its previous value.
I still want the binding error of course, so I can display an error message.
I'm hoping there's a general configuration solution for this (ie. I don't want to just write code to deal with each field manually!).
Background: Retaining the old value causes odd behaviour in subsequent custom cross-field validation. Setting the property values to null would tell this validation not to execute.

MVC3, turn off field validation for a field not in a ViewModel

I have a Form in an MVC3 project. One of my input fields should accept HTML. Unfortunately I cannot have a ViewModel which this value maps to. The Field is autogenerated and read in automatically. I am getting the following error.
A potentially dangerous Request.Form value was detected from the client
Since there is no viewmodel, I cannot apply the [AllowHTML] attribute. Does anyone know a workaround that does not involve disabling validation for the entire page?
Thank You
Additional Information:
I can access the unvalidated value by doing the following:
using System.Web.WebPages;
using System.Web.Helpers;
.....Inside Controller....
string value = Request.Unvalidated("input-40");
The problem now is that the Request.Params collection throws an exception. I would like to access all the other values and have them be validated...just not that one. Is there a way for me to validate the other fields either explicitly or access a validated collection.
The following would be fine
string value = System.Web.Something.ValidateInput(Request.Unvalidated("input-41"));
Unfortunately I don't know where/if this method exists
You can try the ValidateInput(false) attribute:
[ValidateInput(false)]
public ActionResult YourAction(FormCollection yourCollection)
{
// your stuff
}
Use ValidateInput attribute for your action method. Seems to be unsafe but should work, cannot test it now.

Resources