36 lines
1.1 KiB
Dart
36 lines
1.1 KiB
Dart
import 'package:relationship_saver/core/network/backend_exception.dart';
|
|
import 'package:relationship_saver/integrations/backend/models/backend_models.dart';
|
|
|
|
/// Validation rules for outgoing sync envelopes.
|
|
class SyncEnvelopeValidator {
|
|
SyncEnvelopeValidator._();
|
|
|
|
/// Validates a list of changes before a push call.
|
|
static void validateAll(List<ChangeEnvelope> changes) {
|
|
for (final ChangeEnvelope change in changes) {
|
|
validate(change);
|
|
}
|
|
}
|
|
|
|
/// Validates one change envelope.
|
|
static void validate(ChangeEnvelope change) {
|
|
if (change.schemaVersion <= 0) {
|
|
throw ValidationException(
|
|
'schemaVersion must be > 0 for ${change.clientMutationId}',
|
|
);
|
|
}
|
|
|
|
if (change.op == ChangeOperation.delete && change.payload != null) {
|
|
throw ValidationException(
|
|
'payload must be null for delete operation (${change.clientMutationId})',
|
|
);
|
|
}
|
|
|
|
if (change.op == ChangeOperation.upsert && change.payload == null) {
|
|
throw ValidationException(
|
|
'payload is required for upsert operation (${change.clientMutationId})',
|
|
);
|
|
}
|
|
}
|
|
}
|