Class AttributeValidator
Provides runtime validation of fields decorated with Scylla's validation attributes, enforcing Scylla's fail-fast philosophy by surfacing configuration errors at startup before any gameplay logic executes.
Inherited Members
Namespace: Scylla.Core.Attributes
Assembly: ScyllaCore.dll
Syntax
public static class AttributeValidator
Remarks
The three supported validation attributes are:
-
NotNullAttribute - asserts that a field reference is
not
null, including Unity's pseudo-null (a destroyed UnityEngine.Object). - NotEmptyAttribute - asserts that a string is not null, empty, or whitespace-only, or that a collection contains at least one element.
-
ValidateInputAttribute - delegates the check to a
named instance method on the owning object that returns
bool.
Call ValidateObject(object) in Awake or Start to validate
all decorated fields on a MonoBehaviour or any other object. Both public and
non-public (private, protected) fields are discovered through reflection.
A failed validation throws ValidationException, which stops
initialization immediately.
When a non-throwing result is preferred - for example, inside an Editor tool or a conditional check - use TryValidateObject(object, out string) instead.
Multiple validation attributes may be stacked on a single field (e.g., both NotNullAttribute and ValidateInputAttribute). All attributes on a given field are checked in declaration order before moving to the next field, but fields are iterated in the order returned by GetFields(BindingFlags), which is not guaranteed to match source order. Validation stops at the first failure.
public class GameManager : MonoBehaviour
{
[NotNull]
[SerializeField] private Camera _mainCamera;
[NotEmpty]
[SerializeField] private string _gameName;
private void Awake()
{
/* Throws ValidationException if _mainCamera is null or _gameName is empty */
AttributeValidator.ValidateObject(this);
}
}
Methods
TryValidateObject(object, out string)
Validates all fields on obj that are decorated with Scylla
validation attributes, returning a bool result instead of throwing
on failure.
Declaration
public static bool TryValidateObject(object obj, out string errorMessage)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object whose fields should be validated. Must not be |
| string | errorMessage | When this method returns |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
This method catches ValidationException internally and converts
it into the boolean + out-parameter pattern. It does not catch
ArgumentNullException (from a null obj)
or any other non-validation exception that may originate from
ValidateInputAttribute validator methods.
Use this overload in contexts where throwing is undesirable, such as Editor utilities, conditional startup checks, or situations where the caller intends to log the problem and continue rather than aborting.
ValidateObject(object)
Validates all fields on obj that are decorated with one or more
Scylla validation attributes (NotNullAttribute,
NotEmptyAttribute, ValidateInputAttribute).
Declaration
public static void ValidateObject(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object whose fields should be validated. May be any reference type, including
MonoBehaviour instances and plain C# objects. Must not be |
Remarks
Fields are discovered via reflection using
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
so both public and private/protected fields are inspected. Static fields are
excluded.
Decoration order matters within a single field: NotNullAttribute is checked first, then NotEmptyAttribute, then ValidateInputAttribute. Fields without any validation attribute are silently skipped.
The recommended call site is Awake() on a MonoBehaviour - before any
other initialization - so that a missing reference aborts startup with a clear,
actionable message rather than producing a NullReferenceException later.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown immediately if |
| ValidationException | Thrown as soon as the first field fails validation. The exception's FieldName identifies the offending field, and Message contains either the custom error message supplied in the attribute or a default message generated from the field name. Subsequent fields are not checked after the first failure. |