dvDocsDecode function

DVDocsDocument dvDocsDecode(
  1. String payload
)

The document payload holds, or a FormatException naming what is wrong with it.

Checked rather than cast: a payload that is not a document is a stale index.html left in the directory the site is served from, and the failure that says so is worth more than a cast error three frames deep.

Implementation

DVDocsDocument dvDocsDecode(String payload) {
  final Object? decoded = jsonDecode(payload);
  if (decoded is! Map<Object?, Object?>) {
    throw FormatException(
      'the documentation payload is not a document',
      payload,
      0,
    );
  }
  final Map<String, Object?> json = decoded.cast<String, Object?>();
  for (final String key in const <String>[
    'application',
    'graphVersion',
    'navigation',
    'pages',
    'findings',
  ]) {
    if (!json.containsKey(key)) {
      throw FormatException(
        'the documentation payload has no $key',
        payload,
        0,
      );
    }
  }
  return DVDocsDocument.fromJson(json);
}