UNPKG

@umbraco/json-models-builders

Version:
106 lines 3.63 kB
"use strict"; /** * Utility functions for builder classes to reduce code duplication. * These functions are designed to be called from within builder methods * without changing the builder's external API. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.buildComposition = exports.buildProperty = exports.buildContainer = exports.buildValidation = exports.ensureIdExists = void 0; /** * Ensures an ID/key field is set, generating a UUID if not provided. * Uses consistent null-check pattern across all builders. * * @param currentValue - The current value of the id/key field * @returns The existing value if set, or a new UUID */ function ensureIdExists(currentValue) { if (currentValue == null) { const crypto = require('crypto'); return crypto.randomUUID(); } return currentValue; } exports.ensureIdExists = ensureIdExists; /** * Builds a property validation object with consistent defaults. * Uses null as the standard default for optional message fields. * * @param validation - Object containing validation properties * @returns Standardized validation object */ function buildValidation(validation) { return { mandatory: validation.mandatory || false, mandatoryMessage: validation.mandatoryMessage || null, regEx: validation.regEx || null, regExMessage: validation.regExMessage || null }; } exports.buildValidation = buildValidation; /** * Builds a container object with consistent defaults. * * @param container - Object containing container properties * @returns Standardized container object */ function buildContainer(container) { return { id: container.id || null, parent: container.parentId ? { id: container.parentId } : null, name: container.name || '', type: container.type || 'Group', sortOrder: container.sortOrder || 0 }; } exports.buildContainer = buildContainer; /** * Builds a property object for content type builders with consistent defaults. * * @param property - Object containing property definition * @returns Standardized property object */ function buildProperty(property) { return { id: property.id, container: { id: property.containerId || null }, sortOrder: property.sortOrder || 0, alias: property.alias || '', name: property.name || '', description: property.description || '', dataType: { id: property.dataTypeId || null }, variesByCulture: property.variesByCulture || false, variesBySegment: property.variesBySegment || false, validation: buildValidation({ mandatory: property.mandatory, mandatoryMessage: property.mandatoryMessage, regEx: property.regEx, regExMessage: property.regExMessage }), appearance: { labelOnTop: property.labelOnTop || false } }; } exports.buildProperty = buildProperty; /** * Builds a composition reference object with consistent defaults. * * @param typeIdField - The field name for the type ID (e.g., 'documentType', 'mediaType', 'memberType') * @param typeId - The ID value * @param compositionType - The composition type string * @returns Standardized composition object */ function buildComposition(typeIdField, typeId, compositionType) { return { [typeIdField]: { id: typeId || null }, compositionType: compositionType || 'Composition' }; } exports.buildComposition = buildComposition; //# sourceMappingURL=BuilderUtils.js.map