Validation hook to validate Page url

Hi,

I’d like to validate the “Page url” value (eg. wrt other field values of that page) but it doesn’t seem like that value is accessible in a validation hook.

ex: contentModel.data.get('url') is undefined

Is there any way to achieve this feature?

Hello @antmarot,

Welcome to the Builder.io forum!

To retrieve the urlPath for validation, you can use the following code:

// Extract urlPath from the content model query
let urlPath = contentModel.query.find(q => q.property === 'urlPath')?.toJSON().value;

// Handle cases where urlPath is an array
if (Array.isArray(urlPath)) {
  // If urlPath is an array (e.g., ["/my-new-page"]), use the first item
  urlPath = urlPath[0];
}

console.log('RLK: urlPath:', urlPath);

// Validate urlPath
if (!urlPath) {
  return {
    level: 'error',
    message: 'URL path is missing or empty!'
  };
}

If you need to set urlPath dynamically based on other fields (such as the title), you can use the change hook option available for model fields. You’ll find this under “Fields” → “More options” in the Builder.io interface.

Let me know if you have any questions!

Thanks,

1 Like