Skip to content

Commit 78456fd

Browse files
committed
fix(lint): check table json inputs that hold bare block references
Table input checks JSON-parsed raw field values, so a reference written outside a JSON string (e.g. {"count": <start.count>} or a whole-field <start.row>), which resolves to valid JSON at runtime, made the whole check end with an 'Invalid JSON' could-not-complete note. Quote those references in the lint-local copy before the Table block's own parser runs, so literal keys are still checked and the references reach the existing runtime-resolution notes.
1 parent 6ee1d0d commit 78456fd

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎apps/sim/lib/workflows/application/read-workflow-lint-tables.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,64 @@ describe('standalone table diagnostics against the actual Table block', () => {
236236
expect(result.tableFieldIssues).toEqual([])
237237
})
238238

239+
it('checks literal row keys around bare references that resolve to JSON at runtime', async () => {
240+
setGraph(
241+
block('write', {
242+
operation: 'insert_row',
243+
data: '{"name": <start.name>, "missing": <start.count>, "note": "<3 {{SUFFIX}}", "env": {{N}}}',
244+
})
245+
)
246+
const result = await lint()
247+
expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([])
248+
expect(
249+
result.unresolvedReferences.filter((reference) => reference.kind === 'resource')
250+
).toEqual([
251+
expect.objectContaining({
252+
blockId: 'write',
253+
field: 'data',
254+
value: ['missing', 'note', 'env'],
255+
}),
256+
])
257+
})
258+
259+
it('checks literal filter fields around a bare reference value', async () => {
260+
setGraph(block('query', { filter: '{"name": <start.name>, "other": {"$gt": <start.n>}}' }))
261+
const result = await lint()
262+
expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([])
263+
expect(result.tableFieldIssues.map(({ field }) => field)).toEqual(['other'])
264+
})
265+
266+
it.each([
267+
['data', 'insert_row', '<start.row>', 'Table row "write".data is not a static object'],
268+
[
269+
'rows',
270+
'batch_insert_rows',
271+
'<start.rows>',
272+
'Table rows in block "write" are not a static array',
273+
],
274+
['rows', 'batch_insert_rows', '[<start.first>, {"name":"Ada"}]', 'Table row "write".rows[0]'],
275+
])(
276+
'notes a %s value that is a bare reference instead of a failed check (%s, %s)',
277+
async (field, operation, value, note) => {
278+
setGraph(block('write', { operation, [field]: value }))
279+
const result = await lint()
280+
expect(result.notes.filter((entry) => entry.includes('could not complete'))).toEqual([])
281+
expect(result.notes).toEqual(expect.arrayContaining([expect.stringContaining(note)]))
282+
}
283+
)
284+
285+
it('still reports malformed literal JSON that contains a reference', async () => {
286+
setGraph(block('write', { operation: 'insert_row', data: '{"name": <start.name>,}' }))
287+
const result = await lint()
288+
expect(result.notes).toEqual(
289+
expect.arrayContaining([
290+
expect.stringContaining(
291+
'Table input checks in block "write" could not complete before runtime: Invalid JSON in Row Data'
292+
),
293+
])
294+
)
295+
})
296+
239297
it('reports a query object whose entire value requires runtime resolution', async () => {
240298
setGraph(block('query', { filter: '"<start.filter>"' }))
241299
const result = await lint()

‎apps/sim/lib/workflows/application/read-workflow-lint.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { requirePrincipalSubjectUserId } from '@sim/auth/principal'
22
import { getErrorMessage } from '@sim/utils/errors'
33
import { isPlainRecord } from '@sim/utils/object'
4+
import { findWorkflowReferenceTokens } from '@sim/utils/workflow-references'
45
import type { CursorKey } from '@/lib/api/list-query'
56
import { bindCopilotWorkspaceOperation } from '@/lib/core/application/copilot-workspace-invocation'
67
import type { PrincipalForOperation } from '@/lib/core/application/workspace-operation'
@@ -254,6 +255,42 @@ function hasRuntimeReference(value: string): boolean {
254255
return createReferencePattern().test(value) || createEnvVarPattern().test(value)
255256
}
256257

258+
/**
259+
* Outside Function code a reference resolves to JSON text (objects, numbers, booleans), so
260+
* `{"name": <start.name>}` or a whole-field `<start.row>` is valid JSON at runtime but not before.
261+
* Quoting each reference written outside a JSON string lets the Table block's own parser read the
262+
* literal parts, and the quoted references then reach the existing runtime-resolution notes.
263+
* Only this lint-local copy is rewritten; references to string outputs left unquoted are reported
264+
* separately by `collectUnquotedJsonStringReferences`.
265+
*/
266+
function quoteBareReferenceParams(params: Record<string, unknown>): Record<string, unknown> {
267+
return Object.fromEntries(
268+
Object.entries(params).map(([key, value]) => [
269+
key,
270+
typeof value === 'string' ? quoteBareReferences(value) : value,
271+
])
272+
)
273+
}
274+
275+
function quoteBareReferences(json: string): string {
276+
const tokens = findWorkflowReferenceTokens(json)
277+
if (tokens.length === 0) return json
278+
let result = ''
279+
let cursor = 0
280+
let inString = false
281+
for (const token of tokens) {
282+
for (let index = cursor; index < token.start; index++) {
283+
const char = json[index]
284+
if (inString && char === '\\') index++
285+
else if (char === '"') inString = !inString
286+
}
287+
result += json.slice(cursor, token.start)
288+
result += inString ? token.value : JSON.stringify(token.value)
289+
cursor = token.end
290+
}
291+
return result + json.slice(cursor)
292+
}
293+
257294
/** Read each active table once; column checks consume the Table block's own pure input transform. */
258295
async function readTableDiagnostics(
259296
graph: ReadWorkflowGraphResult,
@@ -320,7 +357,7 @@ async function readTableDiagnostics(
320357
if (!transform) throw new Error('Table input normalization is unavailable')
321358
let normalized: Record<string, unknown>
322359
try {
323-
normalized = transform(params)
360+
normalized = transform(quoteBareReferenceParams(params))
324361
} catch (error) {
325362
result.notes.push(
326363
`Table input checks in block "${block.name || blockId}" could not complete before runtime: ${getErrorMessage(error)}`

0 commit comments

Comments
 (0)