UNPKG

@datocms/cma-client

Version:
58 lines 2.39 kB
import { isValidId } from '../utilities/id'; import { isLocalizedFieldValue, } from '../utilities/normalizedFieldValues'; import { isItemId, isItemWithOptionalIdAndMeta, isItemWithOptionalMeta, } from './single_block'; /** * ============================================================================= * TYPE GUARDS - Runtime validation functions * ============================================================================= */ /** * Type guard for basic Modular Content field values (blocks as string IDs only). * Checks for array structure and ensures all blocks are string references. */ export function isRichTextFieldValue(value) { return (Array.isArray(value) && value.every((block) => typeof block === 'string' && isValidId(block))); } export function isLocalizedRichTextFieldValue(value) { return (isLocalizedFieldValue(value) && Object.values(value).every(isRichTextFieldValue)); } /** * Type guard for Modular Content field values in API request format. * Allows blocks as string IDs, full objects with IDs, or objects without IDs. */ export function isRichTextFieldValueInRequest(value) { if (value === null) return true; if (!Array.isArray(value)) return false; return value.every((block) => { // String ID - referencing existing block if (isItemId(block)) return true; // Object (either with or without ID for updates/creation) return isItemWithOptionalIdAndMeta(block); }); } export function isLocalizedRichTextFieldValueInRequest(value) { return (isLocalizedFieldValue(value) && Object.values(value).every(isRichTextFieldValueInRequest)); } /** * Type guard for Modular Content field values with nested blocks (?nested=true format). * Ensures all blocks are full RawApiTypes.Item objects with complete data. */ export function isRichTextFieldValueInNestedResponse(value) { if (!Array.isArray(value)) return false; return value.every((block) => { // Must be a full object with ID (nested format always includes complete block objects) return isItemWithOptionalMeta(block); }); } export function isLocalizedRichTextFieldValueInNestedResponse(value) { return (isLocalizedFieldValue(value) && Object.values(value).every(isRichTextFieldValueInNestedResponse)); } //# sourceMappingURL=rich_text.js.map