validateCallArgs method

void validateCallArgs (ActionMeta actionMeta, List args)

Validates the action call arguments. This method is invoked internally by the call methods. Throws exception on validation failure.

Implementation

void validateCallArgs(ActionMeta actionMeta, List args) {
  if (actionMeta?.args == null) {
    return;
  }

  int expectedAllArgCount = actionMeta.args.length;
  int expectedNonOptionalArgCount =
      actionMeta.args.where((argType) => !argType.optional).length;
  int actualArgCount = args?.length ?? 0;

  if (expectedNonOptionalArgCount == expectedAllArgCount) {
    Validate.isTrue(expectedAllArgCount == actualArgCount,
        'Incorrect number of arguments. Expected $expectedAllArgCount but got $actualArgCount');
  } else {
    Validate.isTrue(
        expectedNonOptionalArgCount <= actualArgCount &&
            actualArgCount <= expectedAllArgCount,
        'Incorrect number of arguments. Expected between $expectedNonOptionalArgCount and $expectedAllArgCount'
        ' but got $actualArgCount');
  }

  // Validate non-nullable arguments.
  for (int i = 0; i < actionMeta.args.length; i++) {
    var argType = actionMeta.args[i];
    Validate.isTrue(argType.optional || argType.nullable || args[i] != null,
        'Action argument ${argType.label ?? argType.name} is not set');
  }
}