action-schema.js 864 B

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * 工作流结点严格格式:除各 type 专有字段外,仅允许下列可选流程字段。
  3. */
  4. const FLOW_OPTIONAL_KEYS = new Set([
  5. 'condition',
  6. 'delay',
  7. 'times',
  8. 'timeout',
  9. 'retry',
  10. 'data',
  11. 'model',
  12. ])
  13. /**
  14. * @param {object} action
  15. * @param {string[]} baseKeys - 允许的专有字段(须含 type)
  16. * @param {string} path - 如 execute[0]
  17. */
  18. function assertStrictKeys (action, baseKeys, path) {
  19. if (!action || typeof action !== 'object') return
  20. const allowed = new Set(baseKeys)
  21. FLOW_OPTIONAL_KEYS.forEach((k) => allowed.add(k))
  22. const extra = Object.keys(action).filter((k) => !allowed.has(k))
  23. if (extra.length > 0) {
  24. throw new Error(
  25. `${path}: 不允许的字段: ${extra.sort().join(', ')}。允许: ${[...allowed].sort().join(', ')}`,
  26. )
  27. }
  28. }
  29. module.exports = { FLOW_OPTIONAL_KEYS, assertStrictKeys }