Class NotEmptyAttribute
Marks a string or collection field as requiring a non-empty value, enforced at runtime by ValidateObject(object).
Inherited Members
Namespace: Scylla.Core.Attributes
Assembly: ScyllaCore.dll
Syntax
[AttributeUsage(AttributeTargets.Field)]
public sealed class NotEmptyAttribute : Attribute
Remarks
Emptiness is evaluated differently depending on the field type:
-
String - the value must not be
null, the empty string (""), or consist entirely of whitespace characters. Internally uses IsNullOrWhiteSpace(string). -
ICollection (arrays,
List<T>, etc.) - the collection must not benulland must contain at least one element. Checked via Count. -
IEnumerable - when the field implements
IEnumerablebut notICollection, the validator advances the enumerator by one step to confirm that at least one element exists. -
Null reference - regardless of type, a
nullvalue is always considered empty and will fail validation.
This attribute is applied to fields only (Field) and does not provide any Editor Inspector visualization. For visual feedback in the Unity Inspector, combine it with RequiredAttribute. For null-only checks without emptiness semantics, use NotNullAttribute instead.
Validation is performed by calling ValidateObject(object)
on the owning object - typically in Awake or Start. A failed check
throws ValidationException with the field name and the configured
error message, stopping execution immediately in line with Scylla's fail-fast
philosophy.
public class Player : MonoBehaviour
{
[NotEmpty("Player name cannot be empty!")]
[SerializeField] private string _playerName;
[NotEmpty]
[SerializeField] private List<Item> _startingItems;
private void Awake()
{
/* Throws ValidationException if _playerName is null/empty or _startingItems is empty */
AttributeValidator.ValidateObject(this);
}
}
Constructors
NotEmptyAttribute()
Initializes a new instance of NotEmptyAttribute that uses the default generated error message when validation fails.
Declaration
public NotEmptyAttribute()
Remarks
When the decorated field fails its emptiness check, AttributeValidator
produces the message "Field '{fieldName}' cannot be empty." automatically,
where fieldName is the C# name of the annotated field as returned by
reflection.
NotEmptyAttribute(string)
Initializes a new instance of NotEmptyAttribute with a custom error message that is used verbatim when validation fails.
Declaration
public NotEmptyAttribute(string errorMessage)
Parameters
| Type | Name | Description |
|---|---|---|
| string | errorMessage | The human-readable message to include in the thrown ValidationException
when the field value is empty. Should clearly state which field failed and what value
is expected. Pass |
Remarks
The supplied errorMessage is stored verbatim and forwarded to
ValidationException without modification. There is no validation of
the message string itself; passing null here is equivalent to using the
parameterless constructor.
Properties
ErrorMessage
Gets the custom error message displayed when validation fails for the decorated field.
Declaration
public string ErrorMessage { get; }
Property Value
| Type | Description |
|---|---|
| string | A non-null string containing the developer-supplied error message, or |