Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model#call arguments validation #994

Open
cameronhunter opened this issue Jul 2, 2021 · 0 comments
Open

Model#call arguments validation #994

cameronhunter opened this issue Jul 2, 2021 · 0 comments

Comments

@cameronhunter
Copy link

cameronhunter commented Jul 2, 2021

Context

I was surprised that Model#call's arguments validation (source) changes depending on the number of arguments provided. For example:

// Passes input validation.
model.call(['hello', 'world'], [{ name: 'Falcor' }]);

// Fails input validation with "Invalid argument" error.
model.call(['hello', 'world'], [{ name: 'Falcor' }], undefined);

This difference likely doesn't affect developers invoking call directly, but it would affect developers that have wrapped Model#call for whatever reason. For example:

function call<T = any>(
  functionPath: string | Path,
  args?: any[],
  refPaths?: PathSet,
  thisPaths?: PathSet[],
): ModelResponse<JSONEnvelope<T>> {
  // Fails because optional parameters will always be passed as `undefined`.
  return model.call(functionPath, args, refPaths, thisPaths);
}

Workaround

The workaround is to use a spread to pass the same number of parameters:

function call<T = any>(
  functionPath: string | Path,
  args?: any[],
  refPaths?: PathSet,
  thisPaths?: PathSet[],
): ModelResponse<JSONEnvelope<T>>;
function call(...args: Parameters<Model['call']>) {
  return model.call(...args);
}

Change Request

  • It could be argued both ways that these calling signatures are either the same or different, so I'm unsure on exactly what approach should be taken. Perhaps the input validation can simply ignore a tail of undefineds and validate the rest?
  • The error message "Invalid argument" is very generic and could include more details to help the developer track down the root cause.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant