UNPKG

mapbox-gl

Version:
52 lines (42 loc) 1.66 kB
const getType = require('../util/get_type'); const validate = require('./validate'); const ValidationError = require('../error/validation_error'); module.exports = function validateArray(options) { const array = options.value; const arraySpec = options.valueSpec; const style = options.style; const styleSpec = options.styleSpec; const key = options.key; const validateArrayElement = options.arrayElementValidator || validate; if (getType(array) !== 'array') { return [new ValidationError(key, array, 'array expected, %s found', getType(array))]; } if (arraySpec.length && array.length !== arraySpec.length) { return [new ValidationError(key, array, 'array length %d expected, length %d found', arraySpec.length, array.length)]; } if (arraySpec['min-length'] && array.length < arraySpec['min-length']) { return [new ValidationError(key, array, 'array length at least %d expected, length %d found', arraySpec['min-length'], array.length)]; } let arrayElementSpec = { "type": arraySpec.value }; if (styleSpec.$version < 7) { arrayElementSpec.function = arraySpec.function; } if (getType(arraySpec.value) === 'object') { arrayElementSpec = arraySpec.value; } let errors = []; for (let i = 0; i < array.length; i++) { errors = errors.concat(validateArrayElement({ array: array, arrayIndex: i, value: array[i], valueSpec: arrayElementSpec, style: style, styleSpec: styleSpec, key: `${key}[${i}]` })); } return errors; };