Class ValidateInputAttribute
Marks a field for custom validation by delegating to a named instance method on the same object, enforced at runtime by ValidateObject(object).
Inherited Members
Namespace: Scylla.Core.Attributes
Assembly: ScyllaCore.dll
Syntax
[AttributeUsage(AttributeTargets.Field)]
public sealed class ValidateInputAttribute : Attribute
Remarks
Use this attribute when the built-in NotNullAttribute and NotEmptyAttribute constraints are insufficient - for example, to validate numeric ranges, cross-field dependencies, or any domain-specific invariant that requires bespoke logic.
The validation method must satisfy all of the following requirements:
-
Defined as an instance method on the same class that declares the
annotated field (or a base class thereof, discovered through
reflection with
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic). - Accepts exactly one parameter whose type is compatible with the field's type. The field value is passed directly; no type coercion is performed.
-
Returns bool -
truemeans the value is valid;falsecauses a ValidationException to be thrown.
If any of these conditions are violated, AttributeValidator throws a descriptive ValidationException rather than silently skipping the check.
It is strongly recommended to reference the method name via nameof() rather
than a hard-coded string literal. This ensures compile-time safety and IDE
rename support, guarding against silent failures at runtime when a method is
renamed without updating the attribute argument.
If the validator method itself throws an exception during invocation, AttributeValidator wraps it in a ValidationException and re-throws, preserving the inner message for diagnosis.
public class GameSettings : MonoBehaviour
{
[ValidateInput(nameof(ValidatePlayerCount), "Player count must be between 1 and 4.")]
[SerializeField] private int _playerCount = 1;
[ValidateInput(nameof(ValidateVolume))]
[SerializeField] private float _volume = 1f;
private void Awake()
{
/* Throws ValidationException if _playerCount or _volume fail their checks */
AttributeValidator.ValidateObject(this);
}
private bool ValidatePlayerCount(int count)
{
return count >= 1 && count <= 4;
}
private bool ValidateVolume(float volume)
{
return volume >= 0f && volume <= 1f;
}
}
Constructors
ValidateInputAttribute(string)
Initializes a new instance of ValidateInputAttribute with the name of the custom validation method and a default auto-generated error message.
Declaration
public ValidateInputAttribute(string validatorMethod)
Parameters
| Type | Name | Description |
|---|---|---|
| string | validatorMethod | The name of the instance method on the owning class that performs the validation.
Must not be |
Remarks
When the decorated field fails validation, the thrown ValidationException
will carry the auto-generated message "Field '{fieldName}' failed validation.".
To supply a context-specific message use the two-parameter overload.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown immediately at attribute construction if |
ValidateInputAttribute(string, string)
Initializes a new instance of ValidateInputAttribute with the name of the custom validation method and a developer-supplied error message.
Declaration
public ValidateInputAttribute(string validatorMethod, string errorMessage)
Parameters
| Type | Name | Description |
|---|---|---|
| string | validatorMethod | The name of the instance method on the owning class that performs the validation.
Must not be |
| string | errorMessage | The human-readable message to include in the thrown ValidationException
when the validator method returns |
Remarks
The supplied errorMessage is stored verbatim and forwarded to
ValidationException without modification. There is no validation of
the message string itself.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown immediately at attribute construction if |
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 |
ValidatorMethod
Gets the name of the instance method on the owning class that performs the custom validation logic for the decorated field.
Declaration
public string ValidatorMethod { get; }
Property Value
| Type | Description |
|---|---|
| string | The method name as a string, exactly as it must appear through reflection.
Always non-null and non-empty - the constructors enforce this invariant.
The method must have the signature |