isValidRecap static method

bool isValidRecap(
  1. Map<String, dynamic> recap
)

Implementation

static bool isValidRecap(Map<String, dynamic> recap) {
  final att = recap['att'] as Map<String, dynamic>?;
  if (att == null) {
    throw Errors.getInternalError(
      Errors.MISSING_OR_INVALID,
      context: 'Invalid ReCap. No `att` property found',
    ).toSignError();
  }
  //
  final resources = att.keys;
  if (resources.isEmpty) {
    throw Errors.getInternalError(
      Errors.MISSING_OR_INVALID,
      context: 'Invalid ReCap. No resources found in `att` property',
    ).toSignError();
  }
  //
  for (var resource in resources) {
    final abilities = att[resource];
    if (abilities is! Map) {
      throw Errors.getInternalError(
        Errors.MISSING_OR_INVALID,
        context: 'Invalid ReCap. Resource must be an object: $resource',
      ).toSignError();
    }
    final resourceAbilities = (abilities as Map<String, dynamic>).keys;
    if (resourceAbilities.isEmpty) {
      throw Errors.getInternalError(
        Errors.MISSING_OR_INVALID,
        context: 'Invalid ReCap. Resource object is empty: $resource',
      ).toSignError();
    }
    //
    for (var ability in resourceAbilities) {
      final limits = abilities[ability];
      if (limits is! List) {
        throw Errors.getInternalError(
          Errors.MISSING_OR_INVALID,
          context: 'Invalid ReCap. Ability limits $ability must be an array '
              'of objects, found: $limits',
        ).toSignError();
      }
      if ((limits).isEmpty) {
        throw Errors.getInternalError(
          Errors.MISSING_OR_INVALID,
          context: 'Invalid ReCap. Value of $ability is empty array, must be '
              'an array with objects',
        ).toSignError();
      }
      //
      for (var limit in limits) {
        if (limit is! Map) {
          throw Errors.getInternalError(
            Errors.MISSING_OR_INVALID,
            context:
                'Invalid ReCap. Ability limits ($ability) must be an array '
                'of objects, found: $limit',
          ).toSignError();
        }
      }
    }
  }

  return true;
}