Schemas

Blueprint that defines the structure, format, and validation rules for data.

A schema is a blueprint that defines the structure, required fields, data types, and validation rules for a piece of data. In Botpress, schemas are primarily used to ensure that data passed between various components—such as actions, modules, or API requests—meets the expected format and constraints.

Schemas are useful for:

  • Data Validation: Preventing errors by ensuring that data adheres to the expected format.
  • Consistency: Enforcing a consistent structure for data across different components.
  • Error Reduction: Reducing the likelihood of runtime errors caused by unexpected or malformed data.
  • Ease of Maintenance: Making it easier to understand and manage data flow within your Botpress application.

Creating and Using Schemas

JSON Schema Basics

Botpress uses JSON Schema, a standard for describing the structure and validation constraints of JSON documents.

A JSON Schema can define:

  • Types: The type of data (e.g., string, number, object, array).
  • Required Fields: Fields that must be present in the data.
  • Enum Values: Specific allowed values for a field.
  • Nested Structures: Complex data structures with multiple levels.

Creating a schema

You can create a new schema by navigating to the Schema menu in the left-hand menu. You can also create a new schema when adding a new variable through the Variables menu.

Using schemas

You can use schemas instead of selecting a variable type like String or Number. This means that the variable will contain the information in the structure you've defined in your schema.

For example, you might use a variable to contain information about your end user. This variable can use a schema to contain information like email, age, name, and home address, all contained in a single variable and structured by your schema.

Testing Schemas

Here's a simple demo schema you can create to familiarize yourself with them.

  1. Create a schema for a user object with the following properties:
zui.object({
  name: zui.string(),
  age: zui.number(),
  email: zui.string().email(),
  address: zui.object({
    street: zui.string(),
    city: zui.string(),
    zip: zui.string().length(5)
  })
})
  1. Create a Variable of the user schema type and name it UserInformation.

Usage in AI Task

  • Task Instructions:
Fetch name, age, email and address from the provided input
  • AI Task Input:
Rohan Kokkula, 26, at [email protected], received a mysterious parcel at his doorstep: 
42 Emerald Lane, Mystic Falls, 560078. 
Inside, a map beckoned him to a hidden world beneath Mumbai's bustling streets, 
where history and magic intertwined.
  • Store result in variables: Select the Variable we created above.