Creating Custom ISelfValidators

This tutorial will show you how to create custom ISelfValidators by implementing validation for a MonoBehaviour.

ISelfValidator can be implemented directly in the type you want to validate. This is the fastest and easiest way to validate all occurrences of an object and also gives you direct access to private members.

Let's create our test behaviour and explain what we're doing.

using UnityEngine;
using Sirenix.OdinInspector;

public class SomeMonoBehaviour : MonoBehaviour, ISelfValidator
{
    public GameObject Prefab;

    public void Validate(SelfValidationResult result)
    {
        if (Prefab == null)
        {
            result.AddError("Prefab must not be null!");
        }
    }
}
using Sirenix.OdinInspector;

The ISelfValidator interface is in the Sirenix.OdinInspector namespace, so we need to include it. This is the only validator that is contained outside of Sirenix.OdinInspector.Editor.Validation. Having it in Sirenix.OdinInspector makes sure that we can mix it with our runtime objects without getting compile errors.

Unlike the other validator types, ISelfValidator does not need to be registered.

public void Validate(SelfValidationResult result)
{
    if (Prefab == null)
    {
        result.AddError("Prefab must not be null!");
    }
}

This is where the actual validation logic is executed. We check if the prefab is null and if it is, we add an error to the validation results.

The result object is your main way of interacting with the validation system. Whenever your validator finds an issue you can add a warning / error to it using

  • .AddWarning()
  • .AddError()

Alternatively, you can also use the .Add() method, which takes a ValidatorSeverity argument. This is useful for validation rules (see Validators vs Validation Rules) as it allows you to configure the severity of this validator inside of the editor.

You can also add Buttons, Fixes, Metadata, and Context Menu Items by calling these methods after you have added warning / error.

  • .WithFix() (see Creating Custom Fixes)
  • .WithButton()
  • .WithMetaData()
  • .WithContextClick()
  • .WithSceneGUI()
  • .WithSelectionObject()
  • .EnableRichText()

If you want to draw something in the scene as the result of a warning / error you can do so by using the .WithSceneGUI() method.
All of these method calls can be chained since they use a builder pattern.

That's pretty much all you need to create a custom ISelfValidator.
The validation logic can of course be a lot more sophisticated, but the general workflow is always the same.

Validators vs Validation Rules

You already know how to create custom validators and are interested in the difference between Validators and Validation Rules? Then this tutorial is right for you.

Validators vs Validation Rules

Custom Fixes

You want to add a button to your validator that executes a custom fix? Then this tutorial is right for you.

Creating Custom Fixes

Other Validator Types

Are you interested in the other validator types? Check out this overview to see which one is right for your use case.

Validator Types Overview