MCPcopy Index your code
hub / github.com/simstudioai/sim / parseOptionalJsonInput

Function parseOptionalJsonInput

apps/sim/blocks/utils.ts:444–463  ·  view source on GitHub ↗
(value: unknown, label: string)

Source from the content-addressed store, hash-verified

442 * Returns `undefined` for empty values and throws a helpful error for invalid JSON strings.
443 */
444export function parseOptionalJsonInput<T = unknown>(value: unknown, label: string): T | undefined {
445 if (value === undefined || value === null || value === '') {
446 return undefined
447 }
448
449 if (typeof value === 'string') {
450 const trimmed = value.trim()
451 if (trimmed.length === 0) {
452 return undefined
453 }
454
455 try {
456 return JSON.parse(trimmed) as T
457 } catch (error) {
458 throw new Error(`Invalid JSON for ${label}: ${toError(error).message}`)
459 }
460 }
461
462 return value as T
463}
464
465/**
466 * Parses an optional numeric block input value.

Callers 3

utils.test.tsFile · 0.90
crowdstrike.tsFile · 0.90
codepipeline.tsFile · 0.90

Calls 2

toErrorFunction · 0.90
parseMethod · 0.80

Tested by

no test coverage detected