{"record":{"id":"e9c1eadd08b8c3e7","repo":"json-path/JsonPath","slug":"expected-regexp-node","errorCode":null,"errorMessage":"Expected regexp node","messagePattern":"Expected regexp node","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java","lineNumber":24,"sourceCode":"import com.jayway.jsonpath.internal.Path;\nimport com.jayway.jsonpath.internal.path.PathCompiler;\nimport net.minidev.json.parser.JSONParser;\n\nimport java.time.OffsetDateTime;\nimport java.util.regex.Pattern;\n\nimport static com.jayway.jsonpath.internal.filter.ValueNodes.*;\n\npublic abstract class ValueNode {\n\n    public abstract Class<?> type(Predicate.PredicateContext ctx);\n\n    public boolean isPatternNode() {\n        return false;\n    }\n\n    public PatternNode asPatternNode() {\n        throw new InvalidPathException(\"Expected regexp node\");\n    }\n\n    public boolean isPathNode() {\n        return false;\n    }\n\n    public PathNode asPathNode() {\n        throw new InvalidPathException(\"Expected path node\");\n    }\n\n    public boolean isNumberNode() {\n        return false;\n    }\n\n    public NumberNode asNumberNode() {\n        throw new InvalidPathException(\"Expected number node\");\n    }\n","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java#L6-L42","documentation":"ValueNode.asPatternNode() is the default (non-PatternNode) implementation on ValueNode: only nodes that actually wrap a regex (PatternNode) can be converted, and every other node type throws InvalidPathException(\"Expected regexp node\"). It signals a type error during filter evaluation — the code expected the operand of a =~ comparison to be a regular-expression node but got a different ValueNode.","triggerScenarios":"Evaluating a filter where evaluate() calls asPatternNode() on the operand of a =~ operator but the parsed operand is a string/number/path node instead of a regex pattern; programmatically building a ValueNode.RelationalNode with a =~ operator and a non-pattern operand.","commonSituations":"Writing /regex/ without the pattern-node syntax so the parser treats it as a plain string node; dynamically constructed filters where the regex operand is passed as a quoted string; version differences in how the Jayway parser recognizes inline regex literals.","solutions":["Check isPatternNode() before calling asPatternNode() in custom evaluation code, and branch to an error path otherwise.","Write the regex operand as an inline pattern node (e.g. /pattern/ ) so the parser produces a PatternNode rather than a string node.","If comparing against a stored string, compare with == instead of =~, or wrap the value in a PatternNode when building filter nodes programmatically.","Verify the Jayway version: regex literal parsing has evolved; upgrade if your expression syntax is from an older/newer dialect."],"exampleFix":"// before\nValueNode node = new ValueNode.StringNode(\"^a.*z$\");\nnode.asPatternNode(); // throws\n\n// after\nValueNode node = new ValueNode.PatternNode(Pattern.compile(\"^a.*z$\"));\nif (node.isPatternNode()) {\n    node.asPatternNode();\n}","handlingStrategy":"type-guard","validationCode":"// Java: verify regex literals are written as /pattern/ so they parse as PatternNodes\nvoid validateRegexOperand(String filter) {\n    if (filter.contains(\"=~\") && !java.util.regex.Pattern.compile(\"=~\\\\s*/[^/]+/\").matcher(filter).find()) {\n        throw new IllegalArgumentException(\"=~ operand must be an inline /regex/ in: \" + filter);\n    }\n}","typeGuard":"boolean asPatternSafe(ValueNode node) {\n    return node.isPatternNode(); // only then call node.asPatternNode()\n}","tryCatchPattern":"try {\n    return node.asPatternNode();\n} catch (InvalidPathException e) {\n    if (e.getMessage().equals(\"Expected regexp node\")) {\n        throw new IllegalStateException(\"=~ requires a regex PatternNode, got \" + node.getClass().getSimpleName(), e);\n    }\n    throw e;\n}","preventionTips":["Check isPatternNode() before asPatternNode() in custom evaluation code.","Write =~ operands as inline /pattern/ literals, not quoted strings.","When building nodes programmatically, use PatternNode(Pattern.compile(...)).","Pin and test against the Jayway version whose regex syntax you use."],"tags":["json-path","filter-evaluation","regex","type-error"],"backgroundTag":"type-mismatch","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}