cayleygraph/cayley · error

expected bool as second argument

Error message

expected bool as second argument

What it means

The regexp comparison helper (cmpRegexp) in the Gizmo query environment accepts an optional second argument that controls whether the regexp may match against references (IRIs/BNodes) instead of only plain strings. This argument must be a JavaScript boolean. If a second argument is supplied but is not a boolean, this error is thrown.

Source

Thrown at query/gizmo/environ.go:199

		return throwErr(vm, fmt.Errorf("wildcard: unsupported type: %T", args[0]))
	}
	return vm.ToValue(valFilter{f: shape.Wildcard{Pattern: pattern}})
}

func cmpRegexp(vm *goja.Runtime, call goja.FunctionCall) goja.Value {
	args := exportArgs(call.Arguments)
	if len(args) != 1 && len(args) != 2 {
		return throwErr(vm, errArgCount2{Expected: 1, Got: len(args)})
	}
	v, err := toQuadValue(args[0])
	if err != nil {
		return throwErr(vm, err)
	}
	allowRefs := false
	if len(args) > 1 {
		b, ok := args[1].(bool)
		if !ok {
			return throwErr(vm, fmt.Errorf("expected bool as second argument"))
		}
		allowRefs = b
	}
	switch vt := v.(type) {
	case quad.String:
		if allowRefs {
			v = quad.IRI(string(vt))
		}
	case quad.IRI:
		if !allowRefs {
			return throwErr(vm, errRegexpOnIRI)
		}
	case quad.BNode:
		if !allowRefs {
			return throwErr(vm, errRegexpOnIRI)
		}
	default:
		return throwErr(vm, fmt.Errorf("regexp: unsupported type: %T", v))

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Pass an actual boolean as the second argument: regexp("pattern", true) or regexp("pattern", false)
  2. Convert numeric/string flags with Boolean(x) or x === 1 before calling
  3. Omit the second argument entirely when only plain strings should be matched

Example fix

// before
g.V().Has("name", regexp("^Al", 1))
// after
g.V().Has("name", regexp("^Al", true))
Defensive patterns

Strategy: validation

Validate before calling

function safeRegexp(pattern, allowRefs) {
  if (allowRefs !== undefined && typeof allowRefs !== "boolean")
    throw new Error("second argument to regexp must be a boolean");
  return allowRefs === undefined ? regexp(pattern) : regexp(pattern, allowRefs);
}

Type guard

function isBool(v) { return typeof v === "boolean"; }

Try / catch

try {
  return g.V().Has("name", regexp(pat, flags))
} catch (e) {
  if (String(e).includes("expected bool as second argument")) {
    return g.V().Has("name", regexp(pat, Boolean(flags)))
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling regexp(pattern, x) from a Gizmo/JS query where x is a truthy/falsy non-boolean — e.g. regexp("^a", 1), regexp("^a", "true"), regexp("^a", null). Omitting the second argument entirely is fine; only providing a wrong-typed one fails.

Common situations: Copying examples from other query languages where the flag is 0/1; passing a string "true" from templated query construction; confusion with JS truthiness conventions.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/7089d35c371bc891. Report an issue: GitHub.