Made in Builder.io

Visual Copilot Livestream | Dec 6 @10am PST

Introducing Visual Copilot: Figma to code with AI

Builder.io and Figma
Talk to Us
Product
Developers
Talk to Us

Blog

Home

Resources

Blog

Forum

Github

Login

Signup

×

Visual CMS

Drag-and-drop visual editor and headless CMS for any tech stack

Theme Studio for Shopify

Build and optimize your Shopify-hosted storefront, no coding required

Resources

Blog

Get StartedLogin

enterprise plans

Model validation hooks are a powerful feature of Builder that help you define custom validation logic for your model's content using JavaScript.

To set up a model validation hook:

  1. Go to Models.
  2. Open the model you want to validate and click the Show More Options button.
  3. Click the Edit Validation Hook button.
  4. In the code editor that opens, add your validation logic.

The following video shows this process:

When you click Edit Validation Hook, a code editor opens where you can add your validation hook. The placeholder code serves as an example:

 async function run() {
   if (contentModel.data.get('myProperty') === 'some invalid value') {
     return {
       level: 'error',
       message: 'myProperty is invalid!'
     }
   }
 }
 return run();

The code you write here needs access to these constants:

  • model: The model object being validated. This contains information about the structure and settings of the model.
  • contentModel: The entire content object currently loaded in the Visual Editor. This includes all the data and variations associated with the content.
  • editingVariation: The current content variation being edited. This gives access to the specific variation of the content being validated.

These constants help you access and manipulate the data within the content being validated. For example, you can retrieve values of specific properties, check conditions, and perform custom checks based on the content's attributes.

Validation works by returning validation error objects from this code or returning nothing (undefined) to indicate no errors.

When validation errors occur, you need to return a JavaScript object representing each error. This object must have specific properties that provide information about the nature and severity of the error.

The validation error object are regular JavaScript objects and should have the following shape:

  • level: (required) Indicates the severity of the problem. Possible values are the strings warning or error, depending on the seriousness of the issue found during validation.
  • message: (required) Main error text, which describes the problem that occurred during validation.
  • secondaryMessage: (optional) An additional text that can provide more context or details about the error.
  • element: (optional) If the validation error is specific to a particular element in the content, you can specify that element here. This is useful when you want the Visual Editor to highlight the element in question for the user's convenience.
  • moreInfoUrl: (optional) A URL for external help on the error.
  • affectedElements: (optional) In cases where the error affects multiple elements in the content, you can specify all the affected elements as an array.

You can return a single object to indicate a single error, or you can return an array of these errors to indicate more than one.


Looking to hire a third party to help with your project?

Submit a project request and our partnerships team will reach out to connect you with an Expert from our partner ecosystem.

Connect with us

Was this article helpful?

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Get the latest from Builder.io

Developer Newsletter

Latest tips, tricks, and news for frontend developers from our blog

Product Newsletter

Latest features and updates on the Builder.io platform

By submitting, you agree to our Privacy Policy

Product

Visual Copilot Beta

Visual Headless CMS

Integrations

What's New

Company

About

Careers

Developers

Builder for Developers

Documentation

Devtools

Try Builder Playground

OPEN SOURCE

Builder

Mitosis

Qwik

Partytown

Solutions

Ecommerce

Marketing Sites

Landing Pages

Mobile Apps

Multi-brand

Headless CMS

Popular Guides

SaaS Marketing Site Ebook

Composable Commerce Ebook

Headless CMS Guide

Headless Commerce Guide

Product

Visual Copilot

Visual Copilot Beta

Visual Headless CMS

Integrations

What's New

Company

About

Careers

Developers

Builder for Developers

Documentation

Devtools

Try Builder Playground

OPEN SOURCE

Builder

Mitosis

Qwik

Partytown

Builder.io logo

Visually build and optimize digital experiences on any tech stack. No coding required, and developer approved.

Get Started

Log In

Product

Features

Pricing

Integrations

React.js

Next.js

Gatsby

Angular

Vue

Nuxt

Hydrogen

Salesforce

All Integrations

Use Cases

Product

Features

Pricing

Product

Visual Headless CMS

Integrations

What's New

Company

About

Careers

Developers

Builder for Developers

Developer Docs

Open Source Projects

Performance Insights

Developers

Builder for Developers

Documentation

Dev Tools

Try Builder Playground

Open Source

Builder

Mitosis

Qwik

Partytown

Resources

Documentation

Blog

Community Forum

Templates

Partners

Submit an Idea

Solutions

Ecommerce

Landing Pages

Multi-brand

Headless CMS

Popular Guides

SaaS Marketing Site Ebook

Composable Commerce Ebook

Headless CMS Guide

Landing Pages

Headless CMS

Headless Storefront

Customer Showcase

Customer Success Stories

Frameworks

React

Next.js

Qwik

Gatsby

Angular

Vue

Nuxt

Hydrogen

All Integrations

CMS

React

Integrations

React.js

Next.js

Gatsby

Angular

Vue

Nuxt

Hydrogen

Salesforce

All Integrations

Resources

Blog

Knowledge Base

Community Forum

Partners

Performance Insights

Templates

Success Stories

Showcase

Integrations

React.js

Next.js

Gatsby

Angular

Vue

Nuxt

Hydrogen

Salesforce

All Integrations

Resources

Blog

Knowledge Base

Community Forum

Partners


Templates

Success Stories

Showcase

Use Cases

Company

About

Careers

Resources

Blog

Knowledge Base

Community Forum

Partners

Performance Insights

Templates

Success Stories

Showcase

© 2023 Builder.io, Inc.

Security

Privacy Policy

SaaS Terms