Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 65x 2x 63x 63x 63x 63x 62x 62x 2x 60x 60x 60x 60x 60x 60x 69x 2x 1x 1x 60x 60x 60x 4x 56x 11x 8x 8x 13x 13x 13x 13x 8x 8x 10x 6x 8x 3x | const { RESERVED_ENTITY_IDS } = require('./constants')
const { validateWorkflowFields } = require('./validateWorkflowFields')
const { validateEmailTemplates } = require('./emailTemplates')
const { validateFieldNames } = require('./validateFieldNames')
const { validateFieldDefinitions } = require('./validateFieldDefinitions')
const { renameDuplicateFieldNames } = require('./fieldNameUniqueness')
const { validateDescription } = require('./validateDescription')
const { normalizeName } = require('./nameUniqueness')
/**
* Shared by entities/import-check.js (preview, read-only) and
* entities/import.js (the actual commit) so the two can never drift apart —
* whatever the preview says will succeed must validate identically when the
* import is actually submitted.
*/
function errorMessageFrom(validationResult) {
try {
return JSON.parse(validationResult.errorResponse.body).error
} catch {
return 'Invalid entity'
}
}
/**
* Validates a single entity's schema shape (field names/definitions/workflow
* states, description). Does not check name uniqueness or cross-flow
* references — those depend on the rest of the batch and the destination
* flow, checked separately.
*
* @returns {{ valid: true } | { valid: false, error: string }}
*/
function validateEntitySchema(entity) {
if (!entity || typeof entity !== 'object' || !Array.isArray(entity.fields)) {
return { valid: false, error: 'Missing or invalid entity' }
}
const workflowValidation = validateWorkflowFields(entity.fields, {
emailTemplates: entity.emailTemplates
})
Iif (!workflowValidation.valid)
return { valid: false, error: errorMessageFrom(workflowValidation) }
const templatesValidation = validateEmailTemplates(entity.emailTemplates, entity.fields)
if (!templatesValidation.valid) return { valid: false, error: 'INVALID_EMAIL_TEMPLATE' }
const fieldNameValidation = validateFieldNames(entity.fields)
if (!fieldNameValidation.valid)
return { valid: false, error: errorMessageFrom(fieldNameValidation) }
// Imports rename duplicate field names (import-worker.js), so they never refuse them
const { fields } = renameDuplicateFieldNames(entity.fields, entity.defaultLanguage)
const fieldDefinitionValidation = validateFieldDefinitions(fields)
Iif (!fieldDefinitionValidation.valid)
return { valid: false, error: errorMessageFrom(fieldDefinitionValidation) }
const descriptionValidation = validateDescription(entity.description)
Iif (!descriptionValidation.valid)
return { valid: false, error: errorMessageFrom(descriptionValidation) }
return { valid: true }
}
function getDisplayName(name, fallback) {
if (typeof name === 'string') return name
if (name && typeof name === 'object')
return name.en || name.fr || Object.values(name)[0] || fallback
return fallback
}
/**
* @returns {{ valid: true } | { valid: false, error: string }}
*/
function checkReservedName(name) {
const displayName = getDisplayName(name, 'Imported Entity')
const proposedId = displayName.toLowerCase().replace(/[^a-z0-9-]/g, '-')
if (RESERVED_ENTITY_IDS.includes(proposedId)) {
return {
valid: false,
error: `Entity name "${displayName}" conflicts with reserved system entity. Reserved names: ${RESERVED_ENTITY_IDS.join(', ')}`
}
}
return { valid: true }
}
/**
* Finds every tempId in `entities` (shape: [{ tempId, entity }]) whose
* (post-rename, if `resolveName` is given) name collides with another
* entity earlier or later in the same batch — a within-batch conflict can
* never be resolved by merging (there's no existing entity to merge into),
* only by renaming one of them.
*
* @param {Array<{ tempId: string, entity: { name: unknown } }>} entities
* @param {(entry: { tempId: string, entity: { name: unknown } }) => unknown} [resolveName]
* @returns {Set<string>} tempIds involved in a within-batch name collision
*/
function findBatchNameConflicts(entities, resolveName = (entry) => entry.entity?.name) {
const tempIdsByName = new Map()
for (const entry of entities) {
const key = normalizeName(resolveName(entry))
Iif (!key) continue
if (!tempIdsByName.has(key)) tempIdsByName.set(key, [])
tempIdsByName.get(key).push(entry.tempId)
}
const conflicting = new Set()
for (const tempIds of tempIdsByName.values()) {
if (tempIds.length > 1) {
tempIds.forEach((tempId) => conflicting.add(tempId))
}
}
return conflicting
}
module.exports = {
validateEntitySchema,
checkReservedName,
findBatchNameConflicts,
getDisplayName
}
|