Data Model - Validation hook that checks for duplicate URL in list of MenuItems

Example of a validation hook code to check for duplicate URLs in a list of menu items.

async function run() {
  // Initialize error flag
  let error = false;

  // Get the list of menu items from the content model data
  const menuItems = contentModel.data.get('links');

  // Iterate through each menu item
  menuItems.forEach(function (menu, key) {
    // Extract the URL from the current menu item
    const url = menu.get('url');

    // Check for duplicates by comparing URLs with other menu items
    if ([...menuItems.entries()].some(([otherKey, otherMenu]) => key !== otherKey && otherMenu.get('url') === url)) {
      // Set the error flag to true if a duplicate URL is found
      error = true;
    }
  });

  // If duplicates are found, return an error message
  if (error) {
    return {
      level: 'error',
      message: 'Duplicate URLs found in the list of menu items!'
    };
  }
}

// Execute the validation function and return the result
return run();

Data Model Structure: