karatelabs/karate · error · RuntimeException
filterKeys() needs at least two arguments
Error message
filterKeys() needs at least two arguments
What it means
Karate's filterKeys() JS utility builds a new map containing only selected keys of a source map (by a keys map, array, or varargs). It requires at least two arguments: the source map plus a key selector; fewer throw this error.
Solutions
- Pass the keys to keep: filterKeys(map, ['a','b']) or filterKeys(map, {a:1})
- Verify the variable holding the key list is defined and non-empty
- Remember the source must be a Map — convert XML via Xml/Json first if needed
Example fix
// before var trimmed = karate.filterKeys(response); // after var trimmed = karate.filterKeys(response, ['id', 'name']);
Defensive patterns
Strategy: validation
Validate before calling
if (!map || !keys || (Array.isArray(keys) && keys.length === 0)) throw new Error('filterKeys requires a map and key selector'); Type guard
function canFilterKeys(args) { return args.length >= 2 && args[0] != null && args[1] != null; } Try / catch
try { trimmed = karate.filterKeys(map, keys); } catch (e) { karate.log('filterKeys failed: ' + e.message); } Prevention
- Ensure the key list/map variable is defined and non-empty
- Accepts a Map, array of keys, or varargs — pick one form consistently
- Convert XML to a map before filtering keys
When it happens
Trigger: Calling filterKeys(map) with only the map, or filterKeys() with no arguments; passing null as the second argument.
Common situations: Stripping a response field but forgetting the key list; dynamic key lists that end up undefined; confusing with filter() which expects a function.
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
- eval() needs one argument
- expect() needs at least one argument
- append() needs at least two arguments
- appendTo() needs at least two arguments: list and item(s)
- extract() needs three arguments: text, regex, group
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/730ce9abc807fe90.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:203
List<?> list = (List<?>) args[0];
JavaCallable fn = (JavaCallable) args[1];
List<Object> result = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Object item = list.get(i);
Object keep = fn.call(null, new Object[]{item, i});
if (Boolean.TRUE.equals(keep)) {
result.add(item);
}
}
return result;
};
}
@SuppressWarnings("unchecked")
static JavaInvokable filterKeys() {
return args -> {
if (args.length < 2) {
throw new RuntimeException("filterKeys() needs at least two arguments");
}
Map<String, Object> source = (Map<String, Object>) args[0];
Map<String, Object> result = new LinkedHashMap<>();
if (args[1] instanceof Map) {
// filterKeys(source, keysFromMap) - filter by keys present in the map
Map<String, Object> keysMap = (Map<String, Object>) args[1];
for (String key : keysMap.keySet()) {
if (source.containsKey(key)) {
result.put(key, source.get(key));
}
}
} else if (args[1] instanceof List) {
// filterKeys(source, [key1, key2, ...])
List<String> keys = (List<String>) args[1];
for (String key : keys) {
if (source.containsKey(key)) {
result.put(key, source.get(key));View on GitHub (pinned to a22eb90246)