(scripts, className, field)
| 15 | * @returns {Object} - { validScripts: Array, validator: Function|null } |
| 16 | */ |
| 17 | export function getValidScripts(scripts, className, field) { |
| 18 | let validator = null; |
| 19 | const validScripts = (scripts || []).filter(script => { |
| 20 | if (script.classes?.includes(className)) { |
| 21 | return true; |
| 22 | } |
| 23 | for (const scriptClass of script?.classes || []) { |
| 24 | if (scriptClass?.name !== className) { |
| 25 | continue; |
| 26 | } |
| 27 | const fields = scriptClass?.fields || []; |
| 28 | if (scriptClass?.fields.includes(field) || scriptClass?.fields.includes('*')) { |
| 29 | return true; |
| 30 | } |
| 31 | for (const currentField of fields) { |
| 32 | if (Object.prototype.toString.call(currentField) === '[object Object]') { |
| 33 | if (currentField.name === field) { |
| 34 | if (typeof currentField.validator === 'string') { |
| 35 | // SAFETY: eval() is used here on validator strings from trusted admin-controlled |
| 36 | // dashboard configuration only (not user input). These validators are used solely |
| 37 | // for UI validation logic to enable/disable script menu items. This is an accepted |
| 38 | // tradeoff in this trusted admin context. If requirements change, consider replacing |
| 39 | // with Function constructor or a safer expression parser. |
| 40 | validator = eval(currentField.validator); |
| 41 | } else { |
| 42 | validator = currentField.validator; |
| 43 | } |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return false; |
| 50 | }); |
| 51 | |
| 52 | return { validScripts, validator }; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Checks if a script response is a ScriptResponse with a form definition. |
no outgoing calls
no test coverage detected