Add validate_expression_tool for Mapbox expression validation #51
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds a comprehensive offline Mapbox expression validation tool that checks style expressions for correctness without requiring API access.
Features
✅ Validates 90+ Mapbox expression operators:
✅ Comprehensive validation checks:
✅ Structured validation results:
validboolean flagerrorsarray (critical issues)warningsarray (non-critical issues like deep nesting)infoarray (informational messages)metadataobject (expressionType, returnType, depth)✅ Developer-friendly:
Implementation Details
BaseTool(offline, no HttpRequest dependency)Test Results
All 395 tests pass ✅
Example Usage
{ "expression": ["case", ["==", ["get", "type"], "park"], "green", "gray"] }Returns:
{ "valid": true, "errors": [], "warnings": [], "info": [], "metadata": { "expressionType": "case", "depth": 1 } }Error Example
{ "expression": ["unknown_operator", "value"] }Returns:
{ "valid": false, "errors": [ { "severity": "error", "message": "Unknown expression operator: \"unknown_operator\"", "path": "[0]", "suggestion": "Use a valid Mapbox expression operator (e.g., \"get\", \"case\", \"match\")" } ] }Testing
Test 1: Valid Simple Expression ✅
Validate this Mapbox expression:
Expected: Valid - gets the "name" property from a feature
Test 2: Valid Conditional Expression ✅
Can you validate this expression?
Expected: Valid - conditional styling based on feature type
Test 3: Valid Math Expression ✅
Validate this:
Expected: Valid - multiplies population by 0.001
Test 4: Invalid Operator ❌
Check this expression:
Expected:
Test 5: Wrong Argument Count ❌
Is this valid?
Expected:
Test 6: Valid Interpolation Expression ✅
Validate this interpolation:
Expected: Valid - interpolates circle radius based on zoom level
Test 7: Nested Expression with Error ❌
Check this nested expression:
Expected:
Test 8: Valid String Expression ✅
Validate:
Expected: Valid - concatenates strings for labels
Test 9: Valid Color Expression ✅
Is this valid?
Expected: Valid - creates RGB color
Test 10: Complex Valid Expression ✅
Validate this complex expression:
Expected: Valid - data-driven color based on population with exponential interpolation
Related
Part of a series of offline validation tools being added to the server: