validateSnapshot method

SnapshotValidationResult validateSnapshot(
  1. Snapshot snapshot
)

Validates a snapshot before restoring it.

This method checks:

  • Schema version compatibility
  • Whether required state contributors are registered
  • Whether snapshot keys exist in the registry

Parameters:

  • snapshot: The snapshot to validate

Returns: A SnapshotValidationResult containing validation status and any errors/warnings.

Implementation

SnapshotValidationResult validateSnapshot(Snapshot snapshot) {
  final registry = SnapshotRegistry.instance;
  final registeredKeys = registry.getAllKeys();
  final errors = <String>[];
  final warnings = <String>[];

  // Check if any state contributors are registered
  if (registeredKeys.isEmpty) {
    errors.add(
      'No state contributors registered. Register at least one state '
      'contributor before restoring a snapshot.',
    );
    return SnapshotValidationResult(
      isValid: false,
      errors: errors,
      warnings: warnings,
    );
  }

  // Check schema version
  if (snapshot.schemaVersion != schemaVersion) {
    errors.add(
      'Schema version mismatch: snapshot has version ${snapshot.schemaVersion}, '
      'but current version is $schemaVersion.',
    );
  }

  // Check for unregistered keys in snapshot
  final snapshotKeys = snapshot.states.keys.toList();
  for (final key in snapshotKeys) {
    if (!registry.isRegistered(key)) {
      warnings.add(
        'Snapshot contains key "$key" which is not registered. '
        'This key will be skipped during restore.',
      );
    }
  }

  // Check for registered keys not in snapshot
  for (final key in registeredKeys) {
    if (!snapshot.states.containsKey(key)) {
      warnings.add(
        'Registered key "$key" is not present in snapshot. '
        'This key will not be restored.',
      );
    }
  }

  return SnapshotValidationResult(
    isValid: errors.isEmpty,
    errors: errors,
    warnings: warnings,
  );
}