validateVerificationInput method

List<CheckpointValidationError> validateVerificationInput(
  1. Checkpoint checkpoint
)

Implementation

List<CheckpointValidationError> validateVerificationInput(
  Checkpoint checkpoint,
) {
  List<CheckpointValidationError> errors = [];

  if (checkpoint.isOk == CheckpointStatus.na) return errors;

  // --- Char verification --- //
  if (checkpoint.verifRefvalChar1.isNotEmpty) {
    if (checkpoint.verifRefvalChar1 != checkpoint.verificationValue1) {
      errors.add(CheckpointValidationError.verification1DoesNotMatchRef);
    }

    if (checkpoint.verifRefvalChar2.isNotEmpty) {
      if (checkpoint.verifRefvalChar2 != checkpoint.verificationValue2 &&
          checkpoint.numberOfValues > 1) {
        errors.add(CheckpointValidationError.verification2DoesNotMatchRef);
      }
    }

    return errors;
  }

  // --- int or float verification --- //
  int intRef1 = checkpoint.verifRefvalInt1 ?? 0;
  int intRef2 = checkpoint.verifRefvalInt2 ?? 0;

  double? fltRef1 = double.tryParse(checkpoint.verifRefvalFlt1) ?? 0;
  double? fltRef2 = double.tryParse(checkpoint.verifRefvalFlt2) ?? 0;

  bool useFloat = fltRef2 > intRef2 || fltRef1 > intRef1;

  num ref1 = useFloat ? fltRef1 : intRef1;
  num ref2 = useFloat ? fltRef2 : intRef2;

  num? currentValue = useFloat
      ? double.tryParse(checkpoint.verificationValue1)
      : int.tryParse(checkpoint.verificationValue1);
  num? currentValue2 = useFloat
      ? double.tryParse(checkpoint.verificationValue2)
      : int.tryParse(checkpoint.verificationValue2);

  if (currentValue == null) {
    errors.add(CheckpointValidationError.verification1DoesNotMatchRef);
    return errors;
  }

  if (checkpoint.numberOfValues > 1 && currentValue2 == null) {
    errors.add(CheckpointValidationError.verification2DoesNotMatchRef);
    return errors;
  }

  switch (checkpoint.verifRule) {
    case CheckpointVerificationRule.lessThan:
      if (currentValue >= ref1) {
        errors.add(CheckpointValidationError.verification1DoesNotMatchRef);
      }
      if (checkpoint.numberOfValues > 1 && currentValue2! >= ref2) {
        errors.add(CheckpointValidationError.verification2DoesNotMatchRef);
      }
    case CheckpointVerificationRule.equal:
      if (currentValue != ref1) {
        errors.add(CheckpointValidationError.verification1DoesNotMatchRef);
      }
      if (checkpoint.numberOfValues > 1 && currentValue2 != ref2) {
        errors.add(CheckpointValidationError.verification2DoesNotMatchRef);
      }
    case CheckpointVerificationRule.notEqual:
      if (currentValue == ref1) {
        errors.add(CheckpointValidationError.verification1DoesNotMatchRef);
      }
      if (checkpoint.numberOfValues > 1 && currentValue2 == ref2) {
        errors.add(CheckpointValidationError.verification2DoesNotMatchRef);
      }
    case CheckpointVerificationRule.greaterThan:
      if (currentValue <= ref1) {
        errors.add(CheckpointValidationError.verification1DoesNotMatchRef);
      }
      if (checkpoint.numberOfValues > 1 && currentValue2! <= ref2) {
        errors.add(CheckpointValidationError.verification2DoesNotMatchRef);
      }
    case CheckpointVerificationRule.inRange:
      if (currentValue < ref1 || currentValue > ref2) {
        errors.add(CheckpointValidationError.verification1DoesNotMatchRef);
      }
      if (checkpoint.numberOfValues > 1 &&
          (currentValue2! < ref1 || currentValue2 > ref2)) {
        errors.add(CheckpointValidationError.verification2DoesNotMatchRef);
      }
    case CheckpointVerificationRule.notInRange:
      if (currentValue >= ref1 && currentValue <= ref2) {
        errors.add(CheckpointValidationError.verification1DoesNotMatchRef);
      }
      if (checkpoint.numberOfValues > 1 &&
          (currentValue2! >= ref1 && currentValue2 <= ref2)) {
        errors.add(CheckpointValidationError.verification2DoesNotMatchRef);
      }
    default:
      break;
  }

  return errors;
}