|
| 1 | +// Copyright (c) Mapbox, Inc. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { z } from 'zod'; |
| 5 | + |
| 6 | +// Basic types |
| 7 | +const ColorSchema = z |
| 8 | + .string() |
| 9 | + .describe('Color as hex, rgb, rgba, hsl, or hsla'); |
| 10 | +const CoordinatesSchema = z.tuple([z.number(), z.number()]); |
| 11 | + |
| 12 | +// Transition schema |
| 13 | +const TransitionSchema = z.object({ |
| 14 | + duration: z.number().optional(), |
| 15 | + delay: z.number().optional() |
| 16 | +}); |
| 17 | + |
| 18 | +// Light schema |
| 19 | +const LightSchema = z.object({ |
| 20 | + anchor: z.enum(['map', 'viewport']).optional(), |
| 21 | + position: z.tuple([z.number(), z.number(), z.number()]).optional(), |
| 22 | + color: ColorSchema.optional(), |
| 23 | + intensity: z.number().optional() |
| 24 | +}); |
| 25 | + |
| 26 | +// Lights (3D) schema |
| 27 | +const LightsSchema = z.array( |
| 28 | + z.object({ |
| 29 | + id: z.string(), |
| 30 | + type: z.enum(['ambient', 'directional']), |
| 31 | + properties: z.record(z.any()).optional() |
| 32 | + }) |
| 33 | +); |
| 34 | + |
| 35 | +// Terrain schema |
| 36 | +const TerrainSchema = z.object({ |
| 37 | + source: z.string(), |
| 38 | + exaggeration: z.number().optional() |
| 39 | +}); |
| 40 | + |
| 41 | +// Source schemas |
| 42 | +const VectorSourceSchema = z.object({ |
| 43 | + type: z.literal('vector'), |
| 44 | + url: z.string().optional(), |
| 45 | + tiles: z.array(z.string()).optional(), |
| 46 | + bounds: z.tuple([z.number(), z.number(), z.number(), z.number()]).optional(), |
| 47 | + scheme: z.enum(['xyz', 'tms']).optional(), |
| 48 | + minzoom: z.number().min(0).max(22).optional(), |
| 49 | + maxzoom: z.number().min(0).max(22).optional(), |
| 50 | + attribution: z.string().optional(), |
| 51 | + promoteId: z.union([z.string(), z.record(z.string())]).optional(), |
| 52 | + volatile: z.boolean().optional() |
| 53 | +}); |
| 54 | + |
| 55 | +const RasterSourceSchema = z.object({ |
| 56 | + type: z.literal('raster'), |
| 57 | + url: z.string().optional(), |
| 58 | + tiles: z.array(z.string()).optional(), |
| 59 | + bounds: z.tuple([z.number(), z.number(), z.number(), z.number()]).optional(), |
| 60 | + minzoom: z.number().min(0).max(22).optional(), |
| 61 | + maxzoom: z.number().min(0).max(22).optional(), |
| 62 | + tileSize: z.number().optional(), |
| 63 | + scheme: z.enum(['xyz', 'tms']).optional(), |
| 64 | + attribution: z.string().optional(), |
| 65 | + volatile: z.boolean().optional() |
| 66 | +}); |
| 67 | + |
| 68 | +const RasterDemSourceSchema = z.object({ |
| 69 | + type: z.literal('raster-dem'), |
| 70 | + url: z.string().optional(), |
| 71 | + tiles: z.array(z.string()).optional(), |
| 72 | + bounds: z.tuple([z.number(), z.number(), z.number(), z.number()]).optional(), |
| 73 | + minzoom: z.number().min(0).max(22).optional(), |
| 74 | + maxzoom: z.number().min(0).max(22).optional(), |
| 75 | + tileSize: z.number().optional(), |
| 76 | + attribution: z.string().optional(), |
| 77 | + encoding: z.enum(['terrarium', 'mapbox']).optional(), |
| 78 | + volatile: z.boolean().optional() |
| 79 | +}); |
| 80 | + |
| 81 | +const GeoJSONSourceSchema = z.object({ |
| 82 | + type: z.literal('geojson'), |
| 83 | + data: z.union([z.string(), z.any()]), // URL or inline GeoJSON |
| 84 | + maxzoom: z.number().min(0).max(24).optional(), |
| 85 | + attribution: z.string().optional(), |
| 86 | + buffer: z.number().min(0).max(512).optional(), |
| 87 | + tolerance: z.number().optional(), |
| 88 | + cluster: z.boolean().optional(), |
| 89 | + clusterRadius: z.number().optional(), |
| 90 | + clusterMaxZoom: z.number().optional(), |
| 91 | + clusterProperties: z.record(z.any()).optional(), |
| 92 | + lineMetrics: z.boolean().optional(), |
| 93 | + generateId: z.boolean().optional(), |
| 94 | + promoteId: z.union([z.string(), z.record(z.string())]).optional() |
| 95 | +}); |
| 96 | + |
| 97 | +const ImageSourceSchema = z.object({ |
| 98 | + type: z.literal('image'), |
| 99 | + url: z.string(), |
| 100 | + coordinates: z.tuple([ |
| 101 | + CoordinatesSchema, |
| 102 | + CoordinatesSchema, |
| 103 | + CoordinatesSchema, |
| 104 | + CoordinatesSchema |
| 105 | + ]) |
| 106 | +}); |
| 107 | + |
| 108 | +const VideoSourceSchema = z.object({ |
| 109 | + type: z.literal('video'), |
| 110 | + urls: z.array(z.string()), |
| 111 | + coordinates: z.tuple([ |
| 112 | + CoordinatesSchema, |
| 113 | + CoordinatesSchema, |
| 114 | + CoordinatesSchema, |
| 115 | + CoordinatesSchema |
| 116 | + ]) |
| 117 | +}); |
| 118 | + |
| 119 | +const SourceSchema = z.union([ |
| 120 | + VectorSourceSchema, |
| 121 | + RasterSourceSchema, |
| 122 | + RasterDemSourceSchema, |
| 123 | + GeoJSONSourceSchema, |
| 124 | + ImageSourceSchema, |
| 125 | + VideoSourceSchema |
| 126 | +]); |
| 127 | + |
| 128 | +// Layer schema (simplified - full schema would be very extensive) |
| 129 | +const LayerSchema = z.object({ |
| 130 | + id: z.string().describe('Unique layer name'), |
| 131 | + type: z.enum([ |
| 132 | + 'fill', |
| 133 | + 'line', |
| 134 | + 'symbol', |
| 135 | + 'circle', |
| 136 | + 'heatmap', |
| 137 | + 'fill-extrusion', |
| 138 | + 'raster', |
| 139 | + 'hillshade', |
| 140 | + 'background', |
| 141 | + 'sky', |
| 142 | + 'slot', |
| 143 | + 'clip', |
| 144 | + 'model', |
| 145 | + 'raster-particle', |
| 146 | + 'building' |
| 147 | + ]), |
| 148 | + source: z |
| 149 | + .string() |
| 150 | + .optional() |
| 151 | + .describe('Source name (not required for background/sky/slot)'), |
| 152 | + 'source-layer': z |
| 153 | + .string() |
| 154 | + .optional() |
| 155 | + .describe('Layer from vector tile source'), |
| 156 | + minzoom: z.number().min(0).max(24).optional(), |
| 157 | + maxzoom: z.number().min(0).max(24).optional(), |
| 158 | + filter: z.any().optional().describe('Expression for filtering features'), |
| 159 | + layout: z.record(z.any()).optional(), |
| 160 | + paint: z.record(z.any()).optional(), |
| 161 | + metadata: z.record(z.any()).optional(), |
| 162 | + slot: z.string().optional().describe('Slot this layer is assigned to') |
| 163 | +}); |
| 164 | + |
| 165 | +// Style import schema |
| 166 | +const StyleImportSchema = z.object({ |
| 167 | + id: z.string(), |
| 168 | + url: z.string(), |
| 169 | + config: z.record(z.any()).optional() |
| 170 | +}); |
| 171 | + |
| 172 | +// Base Style properties (shared between input and output) |
| 173 | +export const BaseStylePropertiesSchema = z.object({ |
| 174 | + // Required Style Spec properties |
| 175 | + version: z |
| 176 | + .literal(8) |
| 177 | + .describe('Style specification version number. Must be 8'), |
| 178 | + sources: z.record(SourceSchema).describe('Data source specifications'), |
| 179 | + layers: z.array(LayerSchema).describe('Layers in draw order'), |
| 180 | + |
| 181 | + // Optional Style Spec properties |
| 182 | + metadata: z |
| 183 | + .record(z.any()) |
| 184 | + .optional() |
| 185 | + .describe('Arbitrary properties for tracking'), |
| 186 | + center: CoordinatesSchema.optional().describe( |
| 187 | + 'Default map center [longitude, latitude]' |
| 188 | + ), |
| 189 | + zoom: z.number().optional().describe('Default zoom level'), |
| 190 | + bearing: z.number().optional().describe('Default bearing in degrees'), |
| 191 | + pitch: z.number().optional().describe('Default pitch in degrees'), |
| 192 | + sprite: z |
| 193 | + .string() |
| 194 | + .optional() |
| 195 | + .describe('Base URL for sprite image and metadata'), |
| 196 | + glyphs: z.string().optional().describe('URL template for glyph sets'), |
| 197 | + light: LightSchema.optional() |
| 198 | + .nullable() |
| 199 | + .describe('Global light source (deprecated, use lights)'), |
| 200 | + lights: LightsSchema.optional() |
| 201 | + .nullable() |
| 202 | + .describe('Array of 3D light sources'), |
| 203 | + terrain: TerrainSchema.optional() |
| 204 | + .nullable() |
| 205 | + .describe('Global terrain elevation'), |
| 206 | + fog: z.record(z.any()).optional().nullable().describe('Fog properties'), |
| 207 | + projection: z |
| 208 | + .record(z.any()) |
| 209 | + .optional() |
| 210 | + .nullable() |
| 211 | + .describe('Map projection'), |
| 212 | + transition: TransitionSchema.optional() |
| 213 | + .nullable() |
| 214 | + .describe('Default transition timing'), |
| 215 | + imports: z |
| 216 | + .array(StyleImportSchema) |
| 217 | + .optional() |
| 218 | + .nullable() |
| 219 | + .describe('Imported styles') |
| 220 | +}); |
| 221 | + |
| 222 | +export type MapboxSource = z.infer<typeof SourceSchema>; |
| 223 | +export type MapboxLayer = z.infer<typeof LayerSchema>; |
0 commit comments