Almost all applications work with data that come from the interaction with humans or other applications. These data, however, may not necessarily meet the requirements of the accepting applications. Data must be validated.
What is validation?
Data entered must pass a set of validation rules in order to be recognized as valid and allowed for further processing.
As an example, lets take a class that has three members: name:String, created:Date and total:int. Our application requires that name is set (not null) and has at least three characters; created is also required and must be a date representing time before now; and total must be a non-negative integer.
There are two common approaches to data validation: fail-fast validation and complete validation.
Fail-fast validation
How it works
If any of the validation rules fails, validation is stopped and data is pronounced invalid and rejected for further processing.
Output
Boolean result that indicates the validity of input data: true for valid, false for invalid.
Pros
It is generally faster than complete validation as first failure terminates the execution of consecutive validation rules. Does not have the over of failure cause reporting.
Cons
Does not provide enough information about the cause of failure.
When to use it
If a simple result: true or false is enough; detailed information about the cause of failure is not required. May be suitable for cases when the source of data cannot correct the data (usually a system without human input).
Example
boolean isValid(String name, Date created, int total)
Data is passed in and boolean result is returned.
Complete validation
How it works
Failure of a validation rule does not stop the validation process. Data is marked as invalid and rejected for further processing after completing whole validation process.
Output
Boolean result that indicates the validity of input data: true for valid, false for invalid. Some form of error collection that contains the information about the causes of validation failure.
For examples see ActionMessages class from Struts framework, Errors class from Spring framework or ErrorCollection class in Atlassian JIRA.
Pros
Provides information about the cause or causes of validation failure. This information can provide the necessary feedback for correcting the input data.
Cons
Slower than fail-fast validation as extra information about causes of validation failure are reported and full set of validation rules is executed independently on the validation result.
When to use it
When a complete set of failure causes is required. The causes of failure may provide hints to the user entering the data about how to correct the data.
Example
void validate(String name, Date created, int total, ErrorCollection errorCollection)
Data is passed in along with the error collection. Method does not have to return anything (void) as invalid data is indicated by the presence of errors in the error collection.
Conclusion
If the complex validation rule set can be broken down into separate validations per input field, these can be used in order to enhance the user experience (via JavaScript or AJAX) – they can provide a real-time feedback for the data being entered.
Also consider that some cases can involve several other input values in order to make a decision about validity of input data. Such case can for example be a single date value consisting on the values from three input fields (don't do this, it's not a really good way of entering dates)
or conditionally required fields, such as the text area in the next picture is only required to be filled in if "other" is selected.
Both types of validation serve their purpose. Which one you decide to use depends mostly on how much information about data being validated you really need in order to make a decision or to correct it in case of failure.




