Drizzle TeamDT
Drizzle Team16mo ago
zd

string.max message not respected?

How do I refine my createInsertSchema to override the message for the max length of a string?

I have a mySqlTable defined named "tags".
export const tags = createTable(
  "tags",
  {
...
    name: varchar("name", { length: 256 }).notNull(),
...


From there I create an insert schema.
export const insertTagSchema = createInsertSchema(tags, {
  name: (schema) =>
    schema.name
      .min(2, "Please enter a tag name of at least 2 characters")
      .max(256, "Please enter a tag name of 256 characters or less"),
...


I use that insertTagSchema in a react-hook-form resolver: zodResolver(insertTagSchema),.

If I enter 1 character I get the appropriate message from the .min. However, if I enter over 256 I get the default message ("String must contain at most 256 character(s)") for the max value instead of the message provided. If I set the .max smaller, like .max(25, "Please enter a tag name of 25 characters or less"),, I see the message provided between 26 and 256 characters.

It seems like the schema refinement of .max adds a parsing step as opposed to refining the existing step.
Was this page helpful?