@datocms/cma-client
Version:
JS client for DatoCMS REST Content Management API
83 lines • 3.38 kB
JavaScript
import { everyNode, isBlock, isDocument, isInlineBlock, } from 'datocms-structured-text-utils';
import { isValidId } from '../utilities/id';
import { isLocalizedFieldValue, } from '../utilities/normalizedFieldValues';
import { isItemId, isItemWithOptionalIdAndMeta, isItemWithOptionalMeta, } from './single_block';
/**
* Utility function to validate all block/inlineBlock nodes in a structured text document tree.
* Calls the provided callback for each block/inlineBlock node found and returns true only if all pass.
*/
function validateAllBlockNodes(node, callback) {
return everyNode(node, (currentNode) => {
// If this is a block or inlineBlock node, validate it with the callback
if (isBlock(currentNode) || isInlineBlock(currentNode)) {
return callback(currentNode);
}
// For all other node types, they're valid by default
return true;
});
}
/**
* Type guard for basic structured text field values (blocks as string IDs only).
* Checks for the expected structure and ensures all block/inlineBlock nodes have string IDs.
*/
export function isStructuredTextFieldValue(value) {
if (value === null)
return true;
if (!isDocument(value)) {
return false;
}
// Check that all block/inlineBlock nodes have string item IDs
return validateAllBlockNodes(value.document, (node) => {
return typeof node.item === 'string' && isValidId(node.item);
});
}
export function isLocalizedStructuredTextFieldValue(value) {
return (isLocalizedFieldValue(value) &&
Object.values(value).every(isStructuredTextFieldValue));
}
/**
* Type guard for structured text field values in API request format.
* Allows blocks as string IDs, full objects with IDs, or objects without IDs.
*/
export function isStructuredTextFieldValueInRequest(value) {
if (value === null)
return true;
if (!isDocument(value)) {
return false;
}
// Check that all block/inlineBlock nodes have valid request format items
return validateAllBlockNodes(value.document, (node) => {
const item = node.item;
// String ID
if (isItemId(item))
return true;
// Object (either with or without ID)
return isItemWithOptionalIdAndMeta(item);
});
}
export function isLocalizedStructuredTextFieldValueInRequest(value) {
return (isLocalizedFieldValue(value) &&
Object.values(value).every(isStructuredTextFieldValueInRequest));
}
/**
* Type guard for structured text field values with nested blocks (?nested=true format).
* Ensures all block/inlineBlock nodes have full RawApiTypes.Item objects.
*/
export function isStructuredTextFieldValueInNestedResponse(value) {
if (value === null)
return true;
if (!isDocument(value)) {
return false;
}
// Check that all block/inlineBlock nodes have full item objects
return validateAllBlockNodes(value.document, (node) => {
const item = node.item;
// Must be a full object with ID (nested format always includes full items)
return isItemWithOptionalMeta(item);
});
}
export function isLocalizedStructuredTextFieldValueInNestedResponse(value) {
return (isLocalizedFieldValue(value) &&
Object.values(value).every(isStructuredTextFieldValueInNestedResponse));
}
//# sourceMappingURL=structured_text.js.map