(fn: Function, config: Config)
| 66 | } |
| 67 | |
| 68 | function validateFunction(fn: Function, config: Config): ValidationError[] { |
| 69 | const schema = config.functions?.[fn.name]; |
| 70 | const errors: ValidationError[] = []; |
| 71 | |
| 72 | if (!schema) |
| 73 | return [ |
| 74 | { |
| 75 | id: 'function-undefined', |
| 76 | level: 'critical', |
| 77 | message: `Undefined function: '${fn.name}'`, |
| 78 | }, |
| 79 | ]; |
| 80 | |
| 81 | if (schema.validate) errors.push(...schema.validate(fn, config)); |
| 82 | |
| 83 | if (schema.parameters) { |
| 84 | for (const [key, value] of Object.entries(fn.parameters)) { |
| 85 | const param = schema.parameters?.[key]; |
| 86 | |
| 87 | if (!param) { |
| 88 | errors.push({ |
| 89 | id: 'parameter-undefined', |
| 90 | level: 'error', |
| 91 | message: `Invalid parameter: '${key}'`, |
| 92 | }); |
| 93 | |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | if (Ast.isAst(value) && !Ast.isFunction(value)) continue; |
| 98 | |
| 99 | if (param.type) { |
| 100 | const valid = validateType(param.type, value, config, key); |
| 101 | if (valid === false) { |
| 102 | errors.push({ |
| 103 | id: 'parameter-type-invalid', |
| 104 | level: 'error', |
| 105 | message: `Parameter '${key}' of '${ |
| 106 | fn.name |
| 107 | }' must be type of '${typeToString(param.type)}'`, |
| 108 | }); |
| 109 | } else if (Array.isArray(valid)) { |
| 110 | errors.push(...valid); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | for (const [key, { required }] of Object.entries(schema.parameters ?? {})) |
| 117 | if (required && fn.parameters[key] === undefined) |
| 118 | errors.push({ |
| 119 | id: 'parameter-missing-required', |
| 120 | level: 'error', |
| 121 | message: `Missing required parameter: '${key}'`, |
| 122 | }); |
| 123 | |
| 124 | return errors; |
| 125 | } |
no test coverage detected
searching dependent graphs…