diff --git a/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift b/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift index c35a5d19..3d234065 100644 --- a/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift +++ b/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift @@ -49,9 +49,15 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior, Pa public func onPageNew(rootMolecules: [MoleculeModelProtocol], _ delegateObject: MVMCoreUIDelegateObject?, changes: inout [MoleculeModelProtocol]) -> [MoleculeModelProtocol]? { self.delegateObject = delegateObject - let moleculeModels = moleculeIds.compactMap { moleculeId in + let moleculeModels: [MoleculeModelProtocol] = moleculeIds.compactMap { moleculeId in do { - return try delegateObject?.moleculeDelegate?.getModuleWithName(moleculeId) + guard let molecule = try delegateObject?.moleculeDelegate?.getModuleWithName(moleculeId) else { + return nil // Valid. Module might not be present yet. + } + guard molecule.id == moleculeId else { + throw MolecularError.validationError("The module name \(moleculeId) must match the molecule id \(molecule.id) it encloses!") + } + return molecule } catch { let coreError = MVMCoreErrorObject.createErrorObject(for: error, location: String(describing: type(of: self)))! if let error = error as? HumanReadableDecodingErrorProtocol {