MCPcopy
hub / github.com/simstudioai/sim / validateVariable

Function validateVariable

apps/sim/stores/variables/store.ts:14–61  ·  view source on GitHub ↗
(variable: Variable)

Source from the content-addressed store, hash-verified

12const logger = createLogger('VariablesStore')
13
14function validateVariable(variable: Variable): string | undefined {
15 try {
16 switch (variable.type) {
17 case 'number':
18 if (Number.isNaN(Number(variable.value))) {
19 return 'Not a valid number'
20 }
21 break
22 case 'boolean':
23 if (!/^(true|false)$/i.test(String(variable.value).trim())) {
24 return 'Expected "true" or "false"'
25 }
26 break
27 case 'object':
28 try {
29 const valueToEvaluate = String(variable.value).trim()
30
31 if (!valueToEvaluate.startsWith('{') || !valueToEvaluate.endsWith('}')) {
32 return 'Not a valid object format'
33 }
34
35 const parsed = JSON5.parse(valueToEvaluate)
36
37 if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
38 return 'Not a valid object'
39 }
40
41 return undefined
42 } catch (e) {
43 logger.error('Object parsing error:', e)
44 return 'Invalid object syntax'
45 }
46 case 'array':
47 try {
48 const parsed = JSON5.parse(String(variable.value))
49 if (!Array.isArray(parsed)) {
50 return 'Not a valid array'
51 }
52 } catch {
53 return 'Invalid array syntax'
54 }
55 break
56 }
57 return undefined
58 } catch (e) {
59 return getErrorMessage(e, 'Invalid format')
60 }
61}
62
63export const useVariablesStore = create<VariablesStore>()(
64 devtools((set, get) => ({

Callers 1

store.tsFile · 0.85

Calls 4

getErrorMessageFunction · 0.90
testMethod · 0.80
parseMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected