Skip to content

Preview/prismalint #39

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .coderabbit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reviews:
path_filters: ["**/.prismalintrc.json",".prismalintrc.json", "**/example.prisma", "example.prisma"]
36 changes: 36 additions & 0 deletions .prismalintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"rules": {
"field-name-mapping-snake-case": [
"error",
{
"compoundWords": ["S3"]
}
],
"field-order": [
"error",
{
"order": ["tenantId", "..."]
}
],
"forbid-required-ignored-field": ["error"],
"model-name-grammatical-number": [
"error",
{
"style": "singular"
}
],
"model-name-mapping-snake-case": [
"error",
{
"compoundWords": ["GraphQL"]
}
],
"require-field-index": [
"error",
{
"forAllRelations": true,
"forNames": ["tenantId"]
}
]
}
}
35 changes: 35 additions & 0 deletions example.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
generator client {
provider = "prisma-client-js"
output = "../generated/prisma/client"
}

datasource db {
provider = "postgresql"
url = "fake-url"
}
Comment on lines +6 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Datasource URL should come from env var

Hard-coding "fake-url" prevents local overrides and breaks prisma generate in CI/CD.
Typical pattern:

-  url      = "fake-url"
+  url      = env("DATABASE_URL")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
datasource db {
provider = "postgresql"
url = "fake-url"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
🤖 Prompt for AI Agents
In example.prisma around lines 6 to 9, the datasource URL is hard-coded as
"fake-url", which prevents local environment overrides and causes issues in
CI/CD pipelines. Replace the hard-coded URL with an environment variable
reference using the syntax env("DATABASE_URL") to allow dynamic configuration
based on the deployment environment.


model Users {
id String @id
emailAddress String
tenantId String
removeMe String @ignore
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ignored field is required

removeMe is String (required by default). Either mark it optional (String?) or drop it; otherwise it blocks deployments despite @ignore.

🤖 Prompt for AI Agents
In example.prisma at line 15, the field `removeMe` is marked with `@ignore` but
is defined as a required `String`, which causes deployment issues. To fix this,
either make the field optional by changing its type to `String?` or remove the
field entirely from the model to avoid blocking deployments.

tenant Tenant @relation(fields: [tenantId], references: [id])
@@map(name: "users")
}
Comment on lines +11 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Users violates four freshly-added lint rules

  1. Model name is plural (Users) but model-name-grammatical-number demands singular.
  2. Required removeMe field is marked @ignore, violating forbid-required-ignored-field.
  3. tenantId must be the first field per field-order.
  4. tenantId lacks the mandatory @@index([tenantId]).

A minimal conforming refactor:

-model Users {
-  id           String @id
-  emailAddress String
-  tenantId     String
-  removeMe     String @ignore
-  tenant       Tenant @relation(fields: [tenantId], references: [id])
-  @@map(name: "users")
-}
+model User {
+  tenantId     String
+  id           String @id
+  emailAddress String
+  // removeMe removed – ignored required fields not allowed
+  tenant       Tenant @relation(fields: [tenantId], references: [id])
+
+  @@index([tenantId])
+  @@map(name: "users")
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
model Users {
id String @id
emailAddress String
tenantId String
removeMe String @ignore
tenant Tenant @relation(fields: [tenantId], references: [id])
@@map(name: "users")
}
model User {
tenantId String
id String @id
emailAddress String
// removeMe removed – ignored required fields not allowed
tenant Tenant @relation(fields: [tenantId], references: [id])
@@index([tenantId])
@@map(name: "users")
}
🤖 Prompt for AI Agents
In example.prisma around lines 11 to 18, fix the Users model by renaming it to
singular form (User), remove the required field 'removeMe' or make it optional
instead of using @ignore, reorder fields so that 'tenantId' is the first field,
and add an @@index([tenantId]) attribute to index the tenantId field as required
by the lint rules.


model Tenant {
id String @id
name String
@@map(name: "tenant")
}

model UserRoleFoo {
id String @id
@@map(name: "unexpected_snake_case")
}
Comment on lines +26 to +29
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

UserRoleFoo mapping should follow snake-case convention

Rule model-name-mapping-snake-case expects the map name to be user_role_foo.

 model UserRoleFoo {
   id         String @id
-  @@map(name: "unexpected_snake_case")
+  @@map(name: "user_role_foo")
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
model UserRoleFoo {
id String @id
@@map(name: "unexpected_snake_case")
}
model UserRoleFoo {
id String @id
@@map(name: "user_role_foo")
}
🧰 Tools
🪛 PrismaLint (0.10.2)

26-26: Model name must be mapped to "user_role_foo". (model-name-mapping-snake-case)

🤖 Prompt for AI Agents
In example.prisma around lines 26 to 29, the model UserRoleFoo has a @@map
attribute with the name "unexpected_snake_case" which does not follow the
required snake-case convention. Update the @@map name to "user_role_foo" to
comply with the model-name-mapping-snake-case rule.


model UserRole {
id String @id
userId String @map(name: "userid")
// No mapping.
}
Comment on lines +31 to +35
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

UserRole lacks relation to User

userId is declared but no @relation or @@index([userId]) is present.
With require-field-index set to forAllRelations: true this will fail linting once a relation is added. Recommend:

 model UserRole {
   id         String @id
-  userId     String @map(name: "userid")
+  userId     String @map(name: "userid")
+  user       User   @relation(fields: [userId], references: [id])
+
+  @@index([userId])
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
model UserRole {
id String @id
userId String @map(name: "userid")
// No mapping.
}
model UserRole {
id String @id
userId String @map(name: "userid")
user User @relation(fields: [userId], references: [id])
@@index([userId])
}
🤖 Prompt for AI Agents
In example.prisma around lines 31 to 35, the UserRole model defines a userId
field without a relation or index. To fix this, add a proper @relation attribute
linking userId to the User model's id field and include an @@index on userId to
satisfy the require-field-index linting rule. This ensures the relation is
explicit and indexed for performance and linting compliance.