Interface ISyncValidator<T>
- Namespace
- SteamNetworkLib.Sync
- Assembly
- SteamNetworkLib.dll
Interface for validating sync var values before they are synchronized.
public interface ISyncValidator<T>
Type Parameters
TThe type of value to validate.
Remarks
Validation Flow: Validators are invoked before a value is synchronized. If validation fails, the sync operation is blocked and an error is reported.
Error Handling: Validation errors can either throw SyncValidationException or be reported via the OnSyncError/OnSyncError event, depending on the ThrowOnValidationError setting.
Use Cases:
- Enforcing numeric ranges (e.g., health must be 0-100)
- Validating string formats (e.g., usernames must be alphanumeric)
- Checking enum values are within valid ranges
- Complex business logic validation
Built-in Validators:
- PredicateValidator<T> - Custom validation using a lambda
- RangeValidator<T> - Numeric range validation
- CompositeValidator<T> - Combine multiple validators
// Create a validator that ensures health stays within 0-100
var healthValidator = new RangeValidator<int>(0, 100);
// Create a custom validator using predicate
var nameValidator = new PredicateValidator<string>(
name => !string.IsNullOrEmpty(name) && name.Length <= 20,
"Name must be 1-20 characters"
);
// Use with a sync var
var health = client.CreateHostSyncVar("Health", 100, validator: healthValidator);
Methods
GetErrorMessage(T)
Gets a human-readable error message describing why validation failed.
string? GetErrorMessage(T value)
Parameters
valueTThe invalid value that failed validation.
Returns
- string
An error message describing why the value is invalid, or null if no specific message is available.
Remarks
This message is used in SyncValidationException and logged when validation fails.
Include the invalid value and expected constraints in the message for easier debugging.
IsValid(T)
Validates a value before it is synchronized.
bool IsValid(T value)
Parameters
valueTThe value to validate.
Returns
- bool
True if the value is valid and should be synchronized; false otherwise.
Remarks
This method is called synchronously before each sync operation.
Keep validation logic fast and non-blocking as it runs on the main thread.