UNPKG

@datocms/cma-client

Version:
92 lines 4.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isLocalizedStructuredTextFieldValueInNestedResponse = exports.isStructuredTextFieldValueInNestedResponse = exports.isLocalizedStructuredTextFieldValueInRequest = exports.isStructuredTextFieldValueInRequest = exports.isLocalizedStructuredTextFieldValue = exports.isStructuredTextFieldValue = void 0; const datocms_structured_text_utils_1 = require("datocms-structured-text-utils"); const id_1 = require("../utilities/id"); const normalizedFieldValues_1 = require("../utilities/normalizedFieldValues"); const single_block_1 = require("./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 (0, datocms_structured_text_utils_1.everyNode)(node, (currentNode) => { // If this is a block or inlineBlock node, validate it with the callback if ((0, datocms_structured_text_utils_1.isBlock)(currentNode) || (0, datocms_structured_text_utils_1.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. */ function isStructuredTextFieldValue(value) { if (value === null) return true; if (!(0, datocms_structured_text_utils_1.isDocument)(value)) { return false; } // Check that all block/inlineBlock nodes have string item IDs return validateAllBlockNodes(value.document, (node) => { return typeof node.item === 'string' && (0, id_1.isValidId)(node.item); }); } exports.isStructuredTextFieldValue = isStructuredTextFieldValue; function isLocalizedStructuredTextFieldValue(value) { return ((0, normalizedFieldValues_1.isLocalizedFieldValue)(value) && Object.values(value).every(isStructuredTextFieldValue)); } exports.isLocalizedStructuredTextFieldValue = isLocalizedStructuredTextFieldValue; /** * Type guard for structured text field values in API request format. * Allows blocks as string IDs, full objects with IDs, or objects without IDs. */ function isStructuredTextFieldValueInRequest(value) { if (value === null) return true; if (!(0, datocms_structured_text_utils_1.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 ((0, single_block_1.isItemId)(item)) return true; // Object (either with or without ID) return (0, single_block_1.isItemWithOptionalIdAndMeta)(item); }); } exports.isStructuredTextFieldValueInRequest = isStructuredTextFieldValueInRequest; function isLocalizedStructuredTextFieldValueInRequest(value) { return ((0, normalizedFieldValues_1.isLocalizedFieldValue)(value) && Object.values(value).every(isStructuredTextFieldValueInRequest)); } exports.isLocalizedStructuredTextFieldValueInRequest = isLocalizedStructuredTextFieldValueInRequest; /** * Type guard for structured text field values with nested blocks (?nested=true format). * Ensures all block/inlineBlock nodes have full RawApiTypes.Item objects. */ function isStructuredTextFieldValueInNestedResponse(value) { if (value === null) return true; if (!(0, datocms_structured_text_utils_1.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 (0, single_block_1.isItemWithOptionalMeta)(item); }); } exports.isStructuredTextFieldValueInNestedResponse = isStructuredTextFieldValueInNestedResponse; function isLocalizedStructuredTextFieldValueInNestedResponse(value) { return ((0, normalizedFieldValues_1.isLocalizedFieldValue)(value) && Object.values(value).every(isStructuredTextFieldValueInNestedResponse)); } exports.isLocalizedStructuredTextFieldValueInNestedResponse = isLocalizedStructuredTextFieldValueInNestedResponse; //# sourceMappingURL=structured_text.js.map