https://discord.gg/botpress logo
hi guys, is there a way to access
# 💻developers
s
hi guys, is there a way to access created schemas in a code card? I want to validate a variable that it follows a created schema with
parse
method offered by
zod
.
w
s
Thanks @white-nest-40686 for the response, I see that type is available in code card. However, I would want to access to the zod schema to validate a variable that it follows the generated type.
w
Okay, it seems that Botpress uses the types created by the schema. I'm not sure if it is possible to access the Zod schema object, but you can create a copy of it.
Copy code
ts
const UserSchema = z.object({
  name: z.string(),
  age: z.number(),
  email: z.string().email(),
  address: z.object({
    street: z.string(),
    city: z.string(),
    zip: z.string().length(5)
  })
})

const user1 = { name: 'john', email: 'email' }
const user2 = UserSchema.safeParse(user1)
if (!user2.success) {
  // handle error then return
  console.log(user2.error);
} else {
  // do something
  console.log(user2.data);
}
I know it's not a great solution. Exactly why do you need to use Zod for validation? Is it not sufficient to just use types?
s
I would like to use the schema on input cards for validation or in code cards, In order to syncronize the validations on schema changes in all inputs.
w
When defining variables such as workflow, user, and conversation, use your schema as the type.
2 Views