karatelabs/karate · error · RuntimeException

keysOf() needs one argument

Error message

keysOf() needs one argument

What it means

Arity guard in the karate.keysOf() JS utility: it requires one argument, the map/object whose keys should be returned. Fires when keysOf() is called with no argument; pass the object to extract keys from.

Solutions

  1. Pass the map as the first argument: keysOf(myMap).
  2. Verify the variable holding the map is defined and in scope before the call.
  3. If the value may be missing, guard the call with an existence check before invoking keysOf().

Example fix

// before
def keys = keysOf()
// after
def keys = keysOf(myMap)
Defensive patterns

Strategy: validation

Validate before calling

// in Karate JS, before calling
def keys = map ? keysOf(map) : []

Type guard

function safeKeysOf(v) { return v && typeof v === 'object' && !(v instanceof Array) ? keysOf(v) : [] }

Prevention

When it happens

Trigger: Calling karate.keysOf() with no arguments from a Karate script, e.g. keysOf() instead of keysOf(myMap).

Common situations: Typo or refactor left the call without its argument; passing a variable that was removed during editing; copying a snippet where the argument was a placeholder.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/8427555196bd29c5. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:295

            if (args.length < 2) {
                throw new RuntimeException("jsonPath() needs two arguments: object and path");
            }
            Object json = args[0];
            if (json instanceof Node node) {
                // v1 parity: an XML argument is converted to a Map first, otherwise jayway
                // sees an opaque scalar and no path can match
                json = Xml.toObject(node);
            }
            String path = args[1].toString();
            return JsonPath.using(jsonPathConfig).parse(json).read(path);
        };
    }

    @SuppressWarnings("unchecked")
    static JavaInvokable keysOf() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("keysOf() needs one argument");
            }
            Map<String, Object> map = (Map<String, Object>) args[0];
            return new ArrayList<>(map.keySet());
        };
    }

    static JavaInvokable lowerCase() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("lowerCase() needs one argument");
            }
            Object obj = args[0];
            if (obj instanceof String) {
                return ((String) obj).toLowerCase();
            } else if (obj instanceof Map) {
                // Convert to JSON string, lowercase, parse back
                String json = Json.stringifyStrict(obj).toLowerCase();
                return Json.of(json).value();

View on GitHub (pinned to a22eb90246)