{"version":3,"file":"validate_layer.mjs","names":["extend"],"sources":["../../src/validate/validate_layer.ts"],"sourcesContent":["import {ValidationError} from '../error/validation_error';\nimport {unbundle} from '../util/unbundle_jsonlint';\nimport {validateObject} from './validate_object';\nimport {validateFilter} from './validate_filter';\nimport {validatePaintProperty} from './validate_paint_property';\nimport {validateLayoutProperty} from './validate_layout_property';\nimport {extendBy as extend} from '../util/extend';\nimport {getType} from '../util/get_type';\n\nexport function validateLayer(options) {\n    let errors = [];\n\n    const layer = options.value;\n    const key = options.key;\n    const style = options.style;\n    const styleSpec = options.styleSpec;\n\n    if (getType(layer) !== 'object') {\n        return [new ValidationError(key, layer, `object expected, ${getType(layer)} found`)];\n    }\n\n    if (!layer.type && !layer.ref) {\n        errors.push(new ValidationError(key, layer, 'either \"type\" or \"ref\" is required'));\n    }\n    let type = unbundle(layer.type);\n    const ref = unbundle(layer.ref);\n\n    if (layer.id) {\n        const layerId = unbundle(layer.id);\n        for (let i = 0; i < options.arrayIndex; i++) {\n            const otherLayer = style.layers[i];\n            if (unbundle(otherLayer.id) === layerId) {\n                errors.push(\n                    new ValidationError(\n                        key,\n                        layer.id,\n                        `duplicate layer id \"${layer.id}\", previously used at line ${otherLayer.id.__line__}`\n                    )\n                );\n            }\n        }\n    }\n\n    if ('ref' in layer) {\n        ['type', 'source', 'source-layer', 'filter', 'layout'].forEach((p) => {\n            if (p in layer) {\n                errors.push(\n                    new ValidationError(key, layer[p], `\"${p}\" is prohibited for ref layers`)\n                );\n            }\n        });\n\n        let parent;\n\n        style.layers.forEach((layer) => {\n            if (unbundle(layer.id) === ref) parent = layer;\n        });\n\n        if (!parent) {\n            errors.push(new ValidationError(key, layer.ref, `ref layer \"${ref}\" not found`));\n        } else if (parent.ref) {\n            errors.push(\n                new ValidationError(key, layer.ref, 'ref cannot reference another ref layer')\n            );\n        } else {\n            type = unbundle(parent.type);\n        }\n    } else if (type !== 'background') {\n        if (!layer.source) {\n            errors.push(new ValidationError(key, layer, 'missing required property \"source\"'));\n        } else {\n            const source = style.sources && style.sources[layer.source];\n            const sourceType = source && unbundle(source.type);\n            if (!source) {\n                errors.push(\n                    new ValidationError(key, layer.source, `source \"${layer.source}\" not found`)\n                );\n            } else if (sourceType === 'vector' && type === 'raster') {\n                errors.push(\n                    new ValidationError(\n                        key,\n                        layer.source,\n                        `layer \"${layer.id}\" requires a raster source`\n                    )\n                );\n            } else if (sourceType !== 'raster-dem' && type === 'hillshade') {\n                errors.push(\n                    new ValidationError(\n                        key,\n                        layer.source,\n                        `layer \"${layer.id}\" requires a raster-dem source`\n                    )\n                );\n            } else if (sourceType !== 'raster-dem' && type === 'color-relief') {\n                errors.push(\n                    new ValidationError(\n                        key,\n                        layer.source,\n                        `layer \"${layer.id}\" requires a raster-dem source`\n                    )\n                );\n            } else if (sourceType === 'raster' && type !== 'raster') {\n                errors.push(\n                    new ValidationError(\n                        key,\n                        layer.source,\n                        `layer \"${layer.id}\" requires a vector source`\n                    )\n                );\n            } else if (sourceType === 'vector' && !layer['source-layer']) {\n                errors.push(\n                    new ValidationError(\n                        key,\n                        layer,\n                        `layer \"${layer.id}\" must specify a \"source-layer\"`\n                    )\n                );\n            } else if (\n                sourceType === 'raster-dem' &&\n                type !== 'hillshade' &&\n                type !== 'color-relief'\n            ) {\n                errors.push(\n                    new ValidationError(\n                        key,\n                        layer.source,\n                        \"raster-dem source can only be used with layer type 'hillshade' or 'color-relief'.\"\n                    )\n                );\n            } else if (\n                type === 'line' &&\n                layer.paint &&\n                layer.paint['line-gradient'] &&\n                (sourceType !== 'geojson' || !source.lineMetrics)\n            ) {\n                errors.push(\n                    new ValidationError(\n                        key,\n                        layer,\n                        `layer \"${layer.id}\" specifies a line-gradient, which requires a GeoJSON source with \\`lineMetrics\\` enabled.`\n                    )\n                );\n            }\n        }\n    }\n\n    if (type === 'raster' && layer.paint?.resampling && layer.paint?.['raster-resampling']) {\n        errors.push(\n            new ValidationError(\n                key,\n                layer.paint,\n                `layer \"${layer.id}\" redundantly specifies \"resampling\" and \"raster-resampling\" paint properties, but only one is allowed. It is advised to use \"resampling\".`\n            )\n        );\n    }\n\n    errors = errors.concat(\n        validateObject({\n            key,\n            value: layer,\n            valueSpec: styleSpec.layer,\n            style: options.style,\n            styleSpec: options.styleSpec,\n            validateSpec: options.validateSpec,\n            objectElementValidators: {\n                '*'() {\n                    return [];\n                },\n                // We don't want to enforce the spec's `\"requires\": true` for backward compatibility with refs;\n                // the actual requirement is validated above. See https://github.com/mapbox/mapbox-gl-js/issues/5772.\n                type() {\n                    return options.validateSpec({\n                        key: `${key}.type`,\n                        value: layer.type,\n                        valueSpec: styleSpec.layer.type,\n                        style: options.style,\n                        styleSpec: options.styleSpec,\n                        validateSpec: options.validateSpec,\n                        object: layer,\n                        objectKey: 'type'\n                    });\n                },\n                filter: validateFilter,\n                layout(options) {\n                    return validateObject({\n                        layer,\n                        key: options.key,\n                        value: options.value,\n                        style: options.style,\n                        styleSpec: options.styleSpec,\n                        validateSpec: options.validateSpec,\n                        objectElementValidators: {\n                            '*'(options) {\n                                return validateLayoutProperty(extend({layerType: type}, options));\n                            }\n                        }\n                    });\n                },\n                paint(options) {\n                    return validateObject({\n                        layer,\n                        key: options.key,\n                        value: options.value,\n                        style: options.style,\n                        styleSpec: options.styleSpec,\n                        validateSpec: options.validateSpec,\n                        objectElementValidators: {\n                            '*'(options) {\n                                return validatePaintProperty(extend({layerType: type}, options));\n                            }\n                        }\n                    });\n                }\n            }\n        })\n    );\n\n    return errors;\n}\n"],"mappings":";;;;;;;;;AASA,SAAgB,cAAc,SAAS;CACnC,IAAI,SAAS,CAAC;CAEd,MAAM,QAAQ,QAAQ;CACtB,MAAM,MAAM,QAAQ;CACpB,MAAM,QAAQ,QAAQ;CACtB,MAAM,YAAY,QAAQ;CAE1B,IAAI,QAAQ,KAAK,MAAM,UACnB,OAAO,CAAC,IAAI,gBAAgB,KAAK,OAAO,oBAAoB,QAAQ,KAAK,EAAE,OAAO,CAAC;CAGvF,IAAI,CAAC,MAAM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