fix(variables): added the same input vars mapping from workflow block, added type validation to variables block, updated UI (#1761)

* fix(variables): added the same input vars mapping from workflow block, added type validation to variables block, updated UI

* cleanup

* lint
This commit is contained in:
Waleed
2025-10-29 01:22:10 -07:00
committed by GitHub
parent 7be9941bc9
commit fcf947df22
7 changed files with 228 additions and 84 deletions

View File

@@ -29,17 +29,13 @@ export class VariablesBlockHandler implements BlockHandler {
})
try {
// Initialize workflowVariables if not present
if (!context.workflowVariables) {
context.workflowVariables = {}
}
// Parse variable assignments from the custom input
const assignments = this.parseAssignments(inputs.variables)
// Update context.workflowVariables with new values
for (const assignment of assignments) {
// Find the variable by ID or name
const existingEntry = assignment.variableId
? [assignment.variableId, context.workflowVariables[assignment.variableId]]
: Object.entries(context.workflowVariables).find(
@@ -47,7 +43,6 @@ export class VariablesBlockHandler implements BlockHandler {
)
if (existingEntry?.[1]) {
// Update existing variable value
const [id, variable] = existingEntry
context.workflowVariables[id] = {
...variable,
@@ -68,15 +63,12 @@ export class VariablesBlockHandler implements BlockHandler {
})),
})
// Return assignments as a JSON object mapping variable names to values
const assignmentsOutput: Record<string, any> = {}
const output: Record<string, any> = {}
for (const assignment of assignments) {
assignmentsOutput[assignment.variableName] = assignment.value
output[assignment.variableName] = assignment.value
}
return {
assignments: assignmentsOutput,
}
return output
} catch (error: any) {
logger.error('Variables block execution failed:', error)
throw new Error(`Variables block execution failed: ${error.message}`)
@@ -97,7 +89,7 @@ export class VariablesBlockHandler implements BlockHandler {
if (assignment?.variableName?.trim()) {
const name = assignment.variableName.trim()
const type = assignment.type || 'string'
const value = this.parseValueByType(assignment.value, type)
const value = this.parseValueByType(assignment.value, type, name)
result.push({
variableId: assignment.variableId,
@@ -111,9 +103,8 @@ export class VariablesBlockHandler implements BlockHandler {
return result
}
private parseValueByType(value: any, type: string): any {
// Handle null/undefined early
if (value === null || value === undefined) {
private parseValueByType(value: any, type: string, variableName?: string): any {
if (value === null || value === undefined || value === '') {
if (type === 'number') return 0
if (type === 'boolean') return false
if (type === 'array') return []
@@ -121,7 +112,6 @@ export class VariablesBlockHandler implements BlockHandler {
return ''
}
// Handle plain and string types (plain is for backward compatibility)
if (type === 'string' || type === 'plain') {
return typeof value === 'string' ? value : String(value)
}
@@ -129,35 +119,71 @@ export class VariablesBlockHandler implements BlockHandler {
if (type === 'number') {
if (typeof value === 'number') return value
if (typeof value === 'string') {
const num = Number(value)
return Number.isNaN(num) ? 0 : num
const trimmed = value.trim()
if (trimmed === '') return 0
const num = Number(trimmed)
if (Number.isNaN(num)) {
throw new Error(
`Invalid number value for variable "${variableName || 'unknown'}": "${value}". Expected a valid number.`
)
}
return num
}
return 0
throw new Error(
`Invalid type for variable "${variableName || 'unknown'}": expected number, got ${typeof value}`
)
}
if (type === 'boolean') {
if (typeof value === 'boolean') return value
if (typeof value === 'string') {
return value.toLowerCase() === 'true'
const lower = value.toLowerCase().trim()
if (lower === 'true') return true
if (lower === 'false') return false
throw new Error(
`Invalid boolean value for variable "${variableName || 'unknown'}": "${value}". Expected "true" or "false".`
)
}
return Boolean(value)
}
if (type === 'object' || type === 'array') {
if (typeof value === 'object' && value !== null) {
if (type === 'array' && !Array.isArray(value)) {
throw new Error(
`Invalid array value for variable "${variableName || 'unknown'}": expected an array, got an object`
)
}
if (type === 'object' && Array.isArray(value)) {
throw new Error(
`Invalid object value for variable "${variableName || 'unknown'}": expected an object, got an array`
)
}
return value
}
if (typeof value === 'string' && value.trim()) {
try {
return JSON.parse(value)
} catch {
return type === 'array' ? [] : {}
const parsed = JSON.parse(value)
if (type === 'array' && !Array.isArray(parsed)) {
throw new Error(
`Invalid array value for variable "${variableName || 'unknown'}": parsed value is not an array`
)
}
if (type === 'object' && (Array.isArray(parsed) || typeof parsed !== 'object')) {
throw new Error(
`Invalid object value for variable "${variableName || 'unknown'}": parsed value is not an object`
)
}
return parsed
} catch (error: any) {
throw new Error(
`Invalid JSON for variable "${variableName || 'unknown'}": ${error.message}`
)
}
}
return type === 'array' ? [] : {}
}
// Default: return value as-is
return value
}
}