{"record":{"id":"61b211320da2c7a2","repo":"karatelabs/karate","slug":"set-needs-at-least-two-arguments-name-and-value","errorCode":null,"errorMessage":"set() needs at least two arguments: name and value","messagePattern":"set\\(\\) needs at least two arguments: name and value","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":587,"sourceCode":"    }\n\n    @SuppressWarnings(\"unchecked\")\n    private JavaInvokable set() {\n        return args -> {\n            // v1 bulk form: karate.set(map) sets each top-level key as a variable.\n            // Common pattern is `karate.set(read('classpath:settings.json'))`\n            // to load a settings file into scope.\n            if (args.length == 1) {\n                if (args[0] instanceof Map<?, ?> bulk) {\n                    for (Map.Entry<?, ?> e : bulk.entrySet()) {\n                        engine.put(e.getKey() + \"\", e.getValue());\n                    }\n                    return null;\n                }\n                throw new RuntimeException(\"set() with a single argument expects a Map / JSON object\");\n            }\n            if (args.length < 2) {\n                throw new RuntimeException(\"set() needs at least two arguments: name and value\");\n            }\n            String name = args[0] + \"\";\n            if (args.length == 2) {\n                // Simple set: karate.set('name', value)\n                engine.put(name, args[1]);\n            } else {\n                // Path set: karate.set('name', 'path', value)\n                String path = args[1] + \"\";\n                Object value = args[2];\n                Object target = engine.get(name);\n\n                // Check if this is XPath (path starts with /) or target is XML\n                if (path.startsWith(\"/\") || target instanceof Node) {\n                    // XPath set on XML\n                    Document doc;\n                    if (target instanceof Document) {\n                        doc = (Document) target;\n                    } else if (target instanceof Node) {","sourceCodeStart":569,"sourceCodeEnd":605,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L569-L605","documentation":"The standard form karate.set(name, value) requires at least two arguments: the variable name and the value to assign. Karate throws this when fewer than two arguments are given and the single argument was not a Map for bulk assignment.","triggerScenarios":"karate.set('name') with only a name and no value; karate.set() with nothing (and not a Map).","commonSituations":"Partial refactor removing the value argument; dynamic value variable undefined leading to a collapsed call; confusing set() with the one-arg bulk-map form.","solutions":["Provide both name and value: karate.set('x', 123).","For multiple variables use a single Map argument: karate.set({ x: 1, y: 2 }).","For path-based sets use karate.set('name', 'path', value) with three arguments.","Check that the value variable is defined before the call."],"exampleFix":"// before\nkarate.set('counter');\n// after\nkarate.set('counter', 0);","handlingStrategy":"validation","validationCode":"if (name === undefined || value === undefined) throw new Error('set() needs name and value');\nkarate.set(name, value);","typeGuard":"function canSet(name, value) { return typeof name === 'string' && name.length > 0 && arguments.length >= 2; }","tryCatchPattern":"try { karate.set(name, value); }\ncatch (e) { if ((e.message || '').indexOf('set() needs at least two arguments') >= 0) karate.fail('incomplete karate.set call'); throw e; }","preventionTips":["Always pair the variable name with a value.","Use the Map bulk form karate.set({...}) for multiple variables.","Defend dynamic value variables with defaults before the call."],"tags":["javascript","variables","argument-validation"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}