Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] VarChar size is ignored #261

Open
blahetal opened this issue Jun 27, 2024 · 4 comments
Open

[BUG] VarChar size is ignored #261

blahetal opened this issue Jun 27, 2024 · 4 comments
Assignees
Labels
bug (unconfirmed) Could be a bug

Comments

@blahetal
Copy link

Describe the bug
I have a prisma model where a size of VarChar fields is limited to a certain number of chars. The limit is not reflected in the generated zod schema

Scheme

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "linux-musl-openssl-3.0.x", "debian-openssl-3.0.x"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator zod {
  provider       = "zod-prisma-types"
}

model contacts {
  id       Int      @id @default(autoincrement())
  name     String   @db.VarChar(50)
  surname  String   @db.VarChar(50)
  supplier String   @db.VarChar(50)
  email    String?  @db.VarChar(50)
  phone    String?  @db.VarChar(30)
  country  String   @db.VarChar(50)
  note     String?
}

Output

export const contactsSchema = z.object({
  id: z.number().int(),
  name: z.string(),
  surname: z.string(),
  supplier: z.string(),
  email: z.string().nullable(),
  phone: z.string().nullable(),
  country: z.string(),
  note: z.string().nullable(),
})

Package versions (please complete the following information):

  • zod: 3.23.8
  • prisma: 5.16.0
@semy
Copy link

semy commented Nov 13, 2024

This is also confirmed

@KuryKat
Copy link

KuryKat commented Nov 21, 2024

I don't know if this was done automatically before, which then it could be considered a bug, but if it wasn't then it isn't necessarily a bug, more like a missing feature.

For now you can use Field Validators to achieve what you want!

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "linux-musl-openssl-3.0.x", "debian-openssl-3.0.x"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator zod {
  provider       = "zod-prisma-types"
}

model contacts {
  id       Int      @id @default(autoincrement())
  /// @zod.string.max(50, { message: "Error message" })
  name     String   @db.VarChar(50)
  /// @zod.string.max(50, { message: "Error message" })
  surname  String   @db.VarChar(50)
  /// @zod.string.max(50, { message: "Error message" })
  supplier String   @db.VarChar(50)
  /// @zod.string.max(50, { message: "Error message" })
  email    String?  @db.VarChar(50)
  /// @zod.string.max(30, { message: "Error message" })
  phone    String?  @db.VarChar(30)
  /// @zod.string.max(50, { message: "Error message" })
  country  String   @db.VarChar(50)
  note     String?
}

Tho I understand what you mean, it should be automatically detected, I agree this should be done by the package automatically.

@KuryKat
Copy link

KuryKat commented Nov 21, 2024

By the way I recommend using @db.VarChar(320) for email, as that's the maximum size an email can have:
Not recommended anymore, read the following comment to understand why.

Part of email address Maximum length
Local address 64 characters
@ 1 character
Domain name 255 characters
Total 320 characters

That way your database field can support even the most unusual email addresses.

References

RFC 5321 - Section 4.5.3.1.1 to Section 4.5.3.1.2
RFC 3696 - Section 3 (at the last paragraph)

@KuryKat
Copy link

KuryKat commented Nov 21, 2024

By the way I recommend using @db.VarChar(320) for email, as that's the maximum size an email can have:
Part of email address Maximum length
Local address 64 characters
@ 1 character
Domain name 255 characters
Total 320 characters

That way your database field can support even the most unusual email addresses.

References

RFC 5321 - Section 4.5.3.1.1 to Section 4.5.3.1.2 RFC 3696 - Section 3 (at the last paragraph)

Actually, searching a little bit more has shown me two errata in the mentioned RFC 3696

The erratum 1003 changed this value from 320 to 256 characters.

But this also was not precisely correct, then the erratum 1690 changed this value from 256 to 254 characters.

A good Stack Overflow answer that explains this well can be found here

So I believe the actual recommended size is @db.VarChar(254) for email addresses, and not @db.VarChar(320).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug (unconfirmed) Could be a bug
Projects
None yet
Development

No branches or pull requests

4 participants