validateSnapshot static method
Validates a snapshot before restoring it.
This method checks if a snapshot is valid and compatible with the current application state. It validates the snapshot schema, checks if all required state contributors are registered, and verifies that the snapshot data matches the expected format.
Example:
final validation = await StateSnapshot.validateSnapshot(jsonString);
if (validation.isValid) {
await StateSnapshot.restoreFromJson(jsonString);
} else {
print('Validation errors: ${validation.errors}');
}
Parameters:
jsonString: A JSON string containing snapshot data
Returns: A SnapshotValidationResult containing validation status and any errors.
Throws:
- StateError if checkpoint is not enabled
Implementation
static Future<SnapshotValidationResult> validateSnapshot(
String jsonString,
) async {
ensureEnabled();
try {
// Parse JSON string
final json = jsonDecode(jsonString) as Map<String, dynamic>;
// Deserialize snapshot
final snapshot = SnapshotSerializer.instance.fromJson(json);
// Validate snapshot
return SnapshotManager.instance.validateSnapshot(snapshot);
} catch (e) {
return SnapshotValidationResult(
isValid: false,
errors: ['Failed to parse snapshot: $e'],
);
}
}