Search And Data Processing

Precondition

Summary

In Xano, preconditions are a powerful feature that allow you to enforce specific conditions in your function stacks. If a condition is not met, the precondition will stop the execution of the function stack and throw an error. This ensures that your application logic always operates within the specified constraints, preventing potential issues or undesirable behavior.

What are Preconditions?

Preconditions are utility functions in Xano that check if a particular condition is true. If the condition is not satisfied, the precondition will throw an error, halting the execution of the function stack. This is different from conditional statements (e.g., `if` statements), which simply execute different code paths based on a condition but do not stop the execution entirely.

Setting up a Precondition

To set up a precondition in Xano, follow these steps:

  1. In your function stack, locate the "Utility Functions" section and click on "Precondition."
  2. In the precondition configuration, you can specify the condition that must be true for the function stack to continue executing.
  3. Define the condition using the available variables and values in your function stack. For example, you can check if a user's role is equal to "admin" using `user.role === "admin"`.
  4. Select an error type from the available options. This determines the type of error that will be thrown if the condition is not met. Common options include "Standard," "Access Denied," or custom error types.
  5. Optionally, you can provide an error message to further clarify the issue if the precondition fails.
  6. If desired, you can also include a payload. The payload allows you to return data that was processed before the precondition, even if the condition is not met and an error is thrown.

Example: Enforcing Admin Role Access

Let's consider an example where you want to ensure that only users with an "admin" role can access a certain function stack. Here's how you can set up a precondition for this scenario:

  1. In your function stack, retrieve the user record, including the user's role.
  2. Add a precondition and set the condition to `user.role === "admin"`.
  3. Select the "Access Denied" error type.
  4. Provide an error message, such as "You don't have an admin role."
  5. Run the function stack with different user records to test the precondition.

If the user's role is not "admin," the precondition will throw an "Access Denied" error with the specified message, preventing further execution of the function stack.

Using Payloads

Payloads in preconditions allow you to return data that was processed before the precondition, even if the condition is not met and an error is thrown. This can be useful when you need to provide additional context or data to the client or frontend, even in case of an error.

To include a payload, simply specify the variable or data you want to include in the payload field of the precondition configuration. This data will be returned along with the error, allowing you to handle the error and the payload data accordingly on the client side.

Conclusion

Preconditions in Xano are a powerful tool for enforcing conditions and ensuring that your application logic operates within specified constraints. By using preconditions, you can prevent potential issues or undesirable behavior, making your application more robust and reliable. Whether you're a no-code enthusiast, a citizen developer, a traditional developer, or a startup/small business, preconditions can help you build and deploy backend services more efficiently and with greater confidence.

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free