Skip to content

Preview/prismalint #36

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

Closed
wants to merge 2 commits into from
Closed
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
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"]
}
]
Comment on lines +28 to +34
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Option name forNames looks suspicious

Up-stream docs use forFieldNames; the short form forNames is not mentioned.
If this key is wrong, the entire require-field-index rule will be ignored and the schema violations you are trying to catch (e.g. missing @@index([tenantId])) will slip through.

-        "forNames": ["tenantId"]
+        "forFieldNames": ["tenantId"]
📝 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
"require-field-index": [
"error",
{
"forAllRelations": true,
"forNames": ["tenantId"]
}
]
"require-field-index": [
"error",
{
"forAllRelations": true,
"forFieldNames": ["tenantId"]
}
]
🤖 Prompt for AI Agents
In the .prismalintrc.json file between lines 28 and 34, the option name
"forNames" used in the "require-field-index" rule is incorrect and should be
changed to "forFieldNames" to match the upstream documentation. Update the key
from "forNames" to "forFieldNames" to ensure the rule is properly recognized and
enforced, preventing schema violations from being missed.

}
}
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.

🛠️ Refactor suggestion

Hard-coding the connection string makes local & CI setup brittle

Replace the literal "fake-url" with an env("DATABASE_URL") reference so developers and CI can supply their own credentials without touching the schema.

-  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 makes local and CI setups brittle. Replace the literal string
with env("DATABASE_URL") to load the connection string from an environment
variable, allowing developers and CI pipelines to supply their own database
credentials without modifying the schema file.


model Users {
id String @id
emailAddress String
tenantId String
removeMe String @ignore
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 model violates multiple lint rules

  1. Model name is plural ⇒ fails model-name-grammatical-number.
  2. tenantId is not the first field ⇒ fails field-order.
  3. tenantId is missing an index ⇒ fails require-field-index.
  4. Required field removeMe is ignored ⇒ fails forbid-required-ignored-field.
-model Users {
+model User {
   id           String @id
-  emailAddress String
-  tenantId     String
-  removeMe     String @ignore
+  tenantId     String
+  emailAddress String
+  // removeMe was required but ignored. Either make it optional or drop it entirely.
+  // removeMe String? @ignore
+
+  @@index([tenantId])

Renaming the model will require cascading changes in your codebase & migrations, but it brings the schema in line with the configured rules.

📝 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 {
id String @id
tenantId String
emailAddress String
// removeMe was required but ignored. Either make it optional or drop it entirely.
// removeMe String? @ignore
@@index([tenantId])
tenant Tenant @relation(fields: [tenantId], references: [id])
@@map(name: "users")
}
🤖 Prompt for AI Agents
In example.prisma around lines 11 to 18, the Users model violates lint rules:
rename the model to singular form (User), move tenantId to be the first field,
add an index on tenantId, and either remove the required field removeMe or make
it optional if it must be ignored. These changes align the schema with lint
rules but will require updates in your codebase and migrations.


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

model UserRoleFoo {
id String @id
@@map(name: "unexpected_snake_case")
}

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 a relation and required index

userId is clearly intended to reference User, yet no @relation is declared and no index is present.
This will also violate the require-field-index rule (forAllRelations: true).

 model UserRole {
   id         String @id
-  userId     String @map(name: "userid")
-  // No mapping.
+  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's userId field lacks
a @relation attribute linking it to the User model and does not have an index,
which is required for foreign key fields. Add a proper @relation directive to
userId referencing the User model and create an index on userId to comply with
the require-field-index rule and establish the relationship correctly.