validatePatchbayTerminalPayload function

List<PatchbayResponseValidationIssue> validatePatchbayTerminalPayload(
  1. PatchbayResponseSchema schema,
  2. Object? payload
)

Validates the terminal event carried by a completed job-wait payload.

Implementation

List<PatchbayResponseValidationIssue> validatePatchbayTerminalPayload(
  PatchbayResponseSchema schema,
  Object? payload,
) {
  const String eventsPath = r'$.payload.events';
  if (payload is! Map<Object?, Object?>) {
    return const <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: r'$.payload',
        reason: 'wrongType',
        expected: 'object',
      ),
    ];
  }
  if (!payload.containsKey('events')) {
    return const <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: eventsPath,
        reason: 'missingField',
        expected: 'terminalEvent',
      ),
    ];
  }
  final Object? rawEvents = payload['events'];
  if (rawEvents == null) {
    return const <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: eventsPath,
        reason: 'unexpectedNull',
        expected: 'array',
      ),
    ];
  }
  if (rawEvents is! List<Object?>) {
    return const <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: eventsPath,
        reason: 'wrongType',
        expected: 'array',
      ),
    ];
  }
  if (rawEvents.isEmpty) {
    return const <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: eventsPath,
        reason: 'missingField',
        expected: 'terminalEvent',
      ),
    ];
  }
  final String eventPath = '$eventsPath[${rawEvents.length - 1}]';
  final Object? rawEvent = rawEvents.last;
  if (rawEvent is! Map<Object?, Object?>) {
    return <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: eventPath,
        reason: 'wrongType',
        expected: 'object',
      ),
    ];
  }
  if (!rawEvent.containsKey('phase')) {
    return <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: '$eventPath.phase',
        reason: 'missingField',
        expected: 'string',
      ),
    ];
  }
  final Object? rawPhase = rawEvent['phase'];
  if (rawPhase == null) {
    return <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: '$eventPath.phase',
        reason: 'unexpectedNull',
        expected: 'string',
      ),
    ];
  }
  if (rawPhase is! String) {
    return <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: '$eventPath.phase',
        reason: 'wrongType',
        expected: 'string',
      ),
    ];
  }
  final PatchbayResponseValueSchema? terminalSchema = schema.terminal[rawPhase];
  if (terminalSchema == null) {
    final List<String> expected = schema.terminal.keys.toList()..sort();
    return <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: '$eventPath.phase',
        reason: 'unknownVariant',
        expected: expected.join('|'),
      ),
    ];
  }
  if (!rawEvent.containsKey('payload')) {
    return <PatchbayResponseValidationIssue>[
      PatchbayResponseValidationIssue(
        field: '$eventPath.payload',
        reason: 'missingField',
        expected: 'object',
      ),
    ];
  }
  return validatePatchbayResponsePayload(
    terminalSchema,
    rawEvent['payload'],
    path: '$eventPath.payload',
  );
}