Home App Docs Blog Github

How to use validation hooks to ensure the uniqueness of a field across all model content?

This can be accomplished through model validation hooks, the approach is simple with little JS code you can run on every change of the content, check if there’s content with the same field value and if so block publishing by returning an error object.

You can do that on any model by going to the model editor, press show more options, and edit validation hook (it’s an enterprise feature)

Then add

async function run() {
  // hit content api with your api key to test if there's content with that slug
  const matches = await fetch(`https://cdn.builder.io/api/v2/content/{{ add model name here}}?apiKey={{add api key here}}&query.data.slug=${contentModel.data.get('slug') + '&query.id.$ne=' + contentModel.id}`).then(res => res.json())

  if (matches.results.length > 0 ) {
   // if there's a content with the same slug value block publishing by sending an error object back
    return {
      level: 'error',
      message: 'slug is invalid!',
      secondaryMessage: 'The slug property is meant to be unique across all builder content of the same model'

return run()

This will notify the content creator when they choose a non unique slug, and it’ll disable the publishing button until error if fixed: