Validator Types Overview

There are currently 5 different validator types, each with a unique role.

ValueValidator

ValueValidators target all values of the specified type, regardless of where they are used. They are great if you want to validate non-unity objects, such as strings, structs, or classes. For example, they can be used to validate all strings in your project and make sure they don't contain forbidden words.

When should I consider using a ValueValidator?

  • I'm trying to validate all occurrences of a specific type in my project.
  • I don't want to have to add attributes to every value I want to validate.

If you wan't to learn how to create a custom ValueValidator, check out the following tutorial. Creating Custom ValueValidators

RootObjectValidator

RootObjectValidators are great for validating Unity objects such as ScriptableObjects, Materials and Components, as you only get a warning / error message for the object itself which we call the root. Unlike the ValueValidator referencing a root object inside another object will not throw an additional warning / error.

When should I consider using a RootObjectValidator?

  • I'm trying to validate all occurrences of a specific type in my project, but only if it is the root object.
  • I don't want to have to add attributes to every value I want to validate.
  • I'm validating an asset or a component.

If you wan't to learn how to create a custom RootObjectValidator, check out the following tutorial. Creating Custom RootObjectValidators

AttributeValidator

AttributeValidators are great for validating objects based on if they have a specific attribute applied to them or not. This also allows you to take the arguments passed into the attribute into consideration when you validate your object. A good example for an attribute validator is the Range attribute, which takes min and max parameters that could be used inside the validator to check if the values are inside the range.

When should I consider using an AttributeValidator?

  • I want to only validate types that have a specific attribute applied to them.
  • I want to pass arguments to the validator using an attribute.

If you wan't to learn how to create a custom AttributeValidator, check out the following tutorial. Creating Custom AttributeValidators

SceneValidator

SceneValidators can be used to validate scenes and scene objects. Let's assume you have a specific scene object that should only exist once, using a SceneValidator can ensure that you are notified if you accidentally add more than one of them to the hierarchy.

When should I consider using a SceneValidator?

  • I want to validate a scene's hierarchy / objects.

If you wan't to learn how to create a custom SceneValidator, check out the following tutorial. Creating Custom SceneValidators

ISelfValidator

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.

When should I consider using an ISelfValidator?

  • I want an easy and fast way to validate an object.
  • I want to have the object and the validation logic in the same place.
  • I want direct access to private members.

If you wan't to learn how to create a custom ISelfValidator, check out the following tutorial. Creating Custom ISelfValidators