SubmissionRequirement.fromJson constructor

SubmissionRequirement.fromJson(
  1. dynamic requirementJson
)

Implementation

SubmissionRequirement.fromJson(dynamic requirementJson) {
  var requirement = credentialToMap(requirementJson);
  if (requirement.containsKey('rule')) {
    var tmpRule = requirement['rule'];
    if (tmpRule == 'all') {
      rule = SubmissionRequirementRule.all;
    } else if (tmpRule == 'pick') {
      rule = SubmissionRequirementRule.pick;
      if (requirement.containsKey('min')) {
        var minTmp = requirement['min'];
        min = minTmp is int ? minTmp : int.parse(minTmp);
        if (min! < 0) {
          throw Exception('min value must be greater than or equal to zero');
        }
      }
      if (requirement.containsKey('max')) {
        var maxTmp = requirement['max'];
        max = maxTmp is int ? maxTmp : int.parse(maxTmp);
        if (max! <= 0) throw Exception('max value must be greater than zero');
        if (min != null && max! <= min!) {
          throw Exception('max must be greater than min');
        }
      }
      if (requirement.containsKey('count')) {
        var countTemp = requirement['count'];
        count = countTemp is int ? countTemp : int.parse(countTemp);
        if (count! <= 0) throw Exception('count must greater than zero');
      }
    } else {
      throw Exception('Unknown value for rule');
    }
  } else {
    throw FormatException('Rule property is required');
  }

  if (requirement.containsKey('from')) from = requirement['from'];
  if (requirement.containsKey('from_nested')) {
    List tmp = requirement['from_nested'];
    if (tmp.isNotEmpty) {
      fromNested = [];
      for (var s in tmp) {
        fromNested!.add(SubmissionRequirement.fromJson(s));
      }
    }
  }

  if (from == null && fromNested == null) {
    throw FormatException('Need either from or fromNested');
  }
  if (from != null && fromNested != null) {
    throw FormatException('Do nut use  from and fromNested together');
  }

  if (requirement.containsKey('name')) name = requirement['name'];
  if (requirement.containsKey('purpose')) purpose = requirement['purpose'];
}