Introduction
Data Validation is a plugin to inform developers when assets are in an invalid state at edit and build time. This means that errors can be caught when developers are making changes and during a project’s build pipeline. This allows programmers to write simpler code and, more importantly, prevent bugs and crashes from showing up in front of players.
Example
The following is a simplified example of an actor that contains a pointer to a data asset that the actor will use and be referenced at runtime.


Since the DataAsset pointer isn’t guaranteed to be valid, it is essential to check it before it is accessed both in the IncreaseSpeed and BeginPlay functions.
Consider how this code could be simplified if the pointer to the data asset was guaranteed to always be valid. Here is how the same class looks with Data Validation.


By guaranteeing that the pointer to the internal Data Asset is valid at build time, the code is now future proofed against null pointer exceptions and has been simplified in the following ways:
The Data Asset can be returned by reference, signaling to other developers that they are guaranteed a valid, non-null object
Accessing the Data Asset doesn’t require validity checking and assertions
There is only the code path where the Data Asset is valid and there is no need for error logging or error handling
One thing to note is that care must be taken not to lose an invalid data validation result from a parent class. Don’t ever return EDataValidationResult::Valid from the subclass implementation of IsDataValid, but instead capture the result from the parent class and change it to EDataValidationResult::Invalid when needed. This will ensure that all invalid cases from parent classes are caught reported appropriately.
Data Validation in Action
Now that there is an asset type that can be created that implements data validation, an instance can be created in the editor to demonstrate what happens when an asset is in an invalid state.
Data Validation Errors in Editor
The most common place Data Validation errors are going to show up is in editor when developers are creating new or editing existing assets.
Given an invalid Blueprint instance of ADataValidationAsset, data validation errors will present in the following three ways:




Data Validation Errors via Commandlet
Unreal Engine provides a commandlet to do a Data Validation pass on all of the assets in a project. This is especially helpful when:
Adding new validation paths which may introduce validation errors to existing assets
Running in an automation pipeline to verify the integrity of a build during a Continuous Integration (CI) process.
Assuming an installed version of Unreal Engine the Data Validation Commandlet can be run via a command line prompt, e.g. Powershell, the following way:
Conclusion
It is an engineering responsibility to guarantee the code is performant, free of bugs or crashes, and easy to understand. Data Validation is one tool that can help in those efforts.
This is a brief introduction to Data Validation. Check out the documentation here to see the other ways that it can be implemented, e.g. creating a validator that derives from UEditorValidatorBase.
Example code can be found here
Great article, love the detail!