Specification and utilities for working with Mapbox GL styles including validation, migration, formatting, and expression evaluation
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Command-line utilities for batch processing, validation, formatting, and migration of Mapbox GL style files. These tools enable automation workflows and integration with build systems.
The package provides four CLI tools accessible after installation:
gl-style-validate - Validate style files against specificationgl-style-format - Format and prettify style JSON filesgl-style-migrate - Migrate older style versions to current specificationgl-style-composite - Composite multiple vector sources into single sourcenpm install -g @mapbox/mapbox-gl-style-spec
# Or use with npx without global installation
npx @mapbox/mapbox-gl-style-spec <command>Validates Mapbox GL style files against the specification with detailed error reporting.
Command: gl-style-validate
Usage:
gl-style-validate [options] <style-file>Options:
--json - Output errors in JSON format instead of human-readable text--mapbox-api-supported - Validate Mapbox API compatibility (checks for unsupported features)Examples:
# Basic validation
gl-style-validate my-style.json
# JSON output for programmatic processing
gl-style-validate --json my-style.json
# Check Mapbox API compatibility
gl-style-validate --mapbox-api-supported my-style.json
# Combine options
gl-style-validate --json --mapbox-api-supported my-style.jsonExit Codes:
0 - Style is valid1 - Style has validation errorsOutput Format:
Human-readable (default):
✓ Style is validJSON format (--json):
{
"valid": true,
"errors": []
}With errors:
{
"valid": false,
"errors": [
{
"message": "layers[0]: missing required property \"type\"",
"line": 12
}
]
}Formats style JSON files with proper key ordering and consistent indentation.
Command: gl-style-format
Usage:
gl-style-format [options] <style-file>Options:
--space <number> - Number of spaces for indentation (default: 2, use 0 for minified output)Examples:
# Format with default 2-space indentation
gl-style-format my-style.json
# Format with 4-space indentation
gl-style-format --space 4 my-style.json
# Minify (single line, no formatting)
gl-style-format --space 0 my-style.json
# Format and save to new file
gl-style-format my-style.json > formatted-style.jsonKey Ordering:
The formatter ensures consistent key ordering for better diffing and version control:
versionnamemetadatasourcesspriteglyphslayerstransitionlight/lightsterrainfogMigrates older style versions to the current specification format.
Command: gl-style-migrate
Usage:
gl-style-migrate <style-file>Supported Migrations:
Examples:
# Migrate v7 style to v8
gl-style-migrate v7-style.json
# Migrate and save to new file
gl-style-migrate old-style.json > migrated-style.jsonMigration Changes:
version field from 7 to 8Composites multiple Mapbox vector sources into a single optimized source.
Command: gl-style-composite
Usage:
gl-style-composite <style-file>Examples:
# Composite vector sources
gl-style-composite multi-source-style.json
# Composite and save result
gl-style-composite style.json > composited-style.jsonCompositing Process:
package.json scripts:
{
"scripts": {
"validate-styles": "gl-style-validate src/styles/*.json",
"format-styles": "gl-style-format --space 2 src/styles/*.json",
"migrate-styles": "gl-style-migrate legacy-styles/*.json",
"build-styles": "npm run migrate-styles && npm run format-styles && npm run validate-styles"
}
}.github/workflows/styles.yml:
name: Style Validation
on: [push, pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'
- run: npm install -g @mapbox/mapbox-gl-style-spec
- run: gl-style-validate --json styles/*.json#!/bin/bash
# Process all style files in a directory
STYLE_DIR="./styles"
OUTPUT_DIR="./processed-styles"
mkdir -p "$OUTPUT_DIR"
for style in "$STYLE_DIR"/*.json; do
filename=$(basename "$style")
echo "Processing $filename..."
# Migrate, format, and validate
gl-style-migrate "$style" | \
gl-style-format --space 2 > "$OUTPUT_DIR/$filename"
# Validate the result
if gl-style-validate "$OUTPUT_DIR/$filename"; then
echo "✓ $filename processed successfully"
else
echo "✗ $filename has validation errors"
fi
doneWhile these are CLI tools, you can also invoke them programmatically:
const { execSync } = require('child_process');
function validateStyle(stylePath) {
try {
const result = execSync(`gl-style-validate --json "${stylePath}"`, {
encoding: 'utf8'
});
return JSON.parse(result);
} catch (error) {
// Handle validation errors
return JSON.parse(error.stdout);
}
}
function formatStyle(stylePath, spaces = 2) {
return execSync(`gl-style-format --space ${spaces} "${stylePath}"`, {
encoding: 'utf8'
});
}All CLI tools use standard exit codes and provide detailed error messages:
Exit Codes:
0 - Success1 - Validation/processing errors2 - Invalid command line argumentsCommon Error Scenarios:
$ gl-style-validate nonexistent.json
Error: ENOENT: no such file or directory, open 'nonexistent.json'$ gl-style-validate invalid.json
Error: Unexpected token } in JSON at position 123$ gl-style-validate --json invalid-style.json
{
"valid": false,
"errors": [
{"message": "sources: required property missing", "line": 1}
]
}