All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.src.style-spec.validate.validate_enum.js Maven / Gradle / Ivy

The newest version!

import ValidationError from '../error/validation_error';
import {unbundle} from '../util/unbundle_jsonlint';

export default function validateEnum(options) {
    const key = options.key;
    const value = options.value;
    const valueSpec = options.valueSpec;
    const errors = [];

    if (Array.isArray(valueSpec.values)) { // <=v7
        if (valueSpec.values.indexOf(unbundle(value)) === -1) {
            errors.push(new ValidationError(key, value, `expected one of [${valueSpec.values.join(', ')}], ${JSON.stringify(value)} found`));
        }
    } else { // >=v8
        if (Object.keys(valueSpec.values).indexOf(unbundle(value)) === -1) {
            errors.push(new ValidationError(key, value, `expected one of [${Object.keys(valueSpec.values).join(', ')}], ${JSON.stringify(value)} found`));
        }
    }
    return errors;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy