mui-spfx-controls
Version:
SPFx component library built with MUI
25 lines • 1.02 kB
JavaScript
/**
* Validates and converts a user-provided function string into an executable function.
*
* @param {string} code - The user-provided function code as a string.
* @returns {((formData: Record<string, any>) => void) | null} - The validated function or null if invalid.
*/
export function validateUserFunction(code) {
try {
if (typeof code !== 'string' || code.trim() === '') {
throw new Error('Invalid function: Input is not a string or is empty.');
}
if (!/^(\s*\(?\s*\w+\s*\)?\s*=>|function\s*\()/m.test(code)) {
throw new Error('Invalid function: Must be an arrow function or function declaration.');
}
// eslint-disable-next-line no-new-func
var fn = new Function('formData', "\"use strict\"; return (".concat(code, ")(formData);"));
fn({});
return fn;
}
catch (error) {
console.error('Function validation error:', error);
return null;
}
}
//# sourceMappingURL=validateUserFunction.js.map