UNPKG

@atlaskit/adf-utils

Version:

Set of utilities to traverse, modify and create ADF documents.

27 lines (26 loc) 1.03 kB
export function extractAllowedContent(validatorSpecs, entity) { // Filter out content type + irrelevant nodes const potentialAllowedContent = Object.entries(validatorSpecs).filter(item => { const [node, value] = item; if (isValidatorSpec(value)) { return node === entity.type; } const [nodeName, spec] = [value[0], value[1]]; if (typeof spec === 'string' || typeof spec !== 'object') { return false; } return nodeName === entity.type; }); // Keep types such as `mediaSingle_full` which contain additional spec information const contentArray = potentialAllowedContent.map(allowedContent => { if (isValidatorSpec(allowedContent)) { return undefined; } const value = allowedContent[1]; return !isValidatorSpec(value) ? [[value[0], value[1]]] : undefined; }); return contentArray.filter(x => !!x); } function isValidatorSpec(spec) { return spec.props !== undefined || spec.required !== undefined || spec.maxItems !== undefined || spec.minItems !== undefined; }