falcor
falcor copied to clipboard
Model#call arguments validation
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
undefined
s 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.