Running a Validation Profile from code

The first step in running a validation profile is getting or creating the validation profile we want to run. We can eiter instantiate validation profiles and configure them manually, or load them from the asset database. In the example below, we do both, and then use a ValidationCollectionProfile to combine them.

IValidationProfile assetValidator = new AssetValidationProfile()
{
    AssetPaths = new string[] { "Assets/SomeFolder" },
    SearchFilters = new string[] { "t:Material" }
};

IValidationProfile myValidator = AssetDatabase.LoadAssetAtPath<MyValidationProfileAsset>(path);

IValidationProfile profileToRun = new ValidationCollectionProfile() 
{
    Profiles = new List<IValidationProfile>() { assetValidator, myValidator };
};

We can then open the profile in the Validation Profile Window.

ValidationProfileManagerWindow.OpenProjectValidatorWithProfile(profileToRun, scanProfileImmediately: true);

It is also easy to manually run a validation profile, if you for instance wanted to run the validation on a server and send back the results, or as part of your custom build process or testing suite:

ValidationRunner runner = new ValidationRunner();

foreach (ValidationProfileResult vpResult in profileToRun.Validate(runner))
{
    Debug.Log($"Progress: {(int)(vpResult.Progress * 100)}% | Scanning Asset: {vpResult.Name}");

    foreach (ValidationResult result in vpResult.Results)
    {
        if (result.ResultType == ValidationResultType.IgnoreResult)
            continue;

        var log = result.Message;
        log += "\n   Validator:" + result.Setup.Validator.GetType().Name;
        log += "\n   Path:" + result.Path;
        log += "\n   Source:" + vpResult.Source;

        if (result.ResultType == ValidationResultType.Error)
        {
            Debug.LogError(log);
        }
        else
        {
            Debug.LogWarning(log);
        }
    }
}

If you have any broadly applicable use cases for running custom profiles that you feel should be included in Odin Project Validator, we would love to hear from you.