loop-payments / prisma-lint

A linter for Prisma schema files.
https://www.npmjs.com/package/prisma-lint
MIT License
131 stars 6 forks source link

Add support for ignoring just the model @@map #503

Closed canadaduane closed 2 weeks ago

canadaduane commented 1 month ago

Thanks to this library, we're slowly moving towards a sane schema.prisma file with consistent naming of columns and tables.

However, several of our models do not match table name rules. Currently, the best we can do (I think) is add /// prisma-lint-ignore-model within the model. This succeeds in ignoring the model/table name inconsistency, but it also causes prisma-lint to completely ignore other rules within the model such as column name rules.

Example:

Is it possible to ignore the model/table @@map, so the other rules can take effect within a model?

maxh commented 1 month ago

Hi @canadaduane thanks for the feedback!

To ignore specific lint rules for a model and its fields, simply append the rule name at the end of the comment. You can ignore multiple rules with multiple lines:

model User {
  /// prisma-lint-ignore-model field-name-mapping-snake-case
  /// prisma-lint-ignore-model require-field-type
}

And some rules support parameterized ignores:

model User {
  /// prisma-lint-ignore-model require-field tenantId,revisionCreatedAt
}

That will ignore only tenantId and revisionCreatedAt field violations for the model. Other required fields will still be enforced. A comma-separated list of fields can be provided to ignore multiple required fields.

Does this support your use cases? Thanks!

canadaduane commented 2 weeks ago

Thanks for the tips!

However, I don't think it is working as expected. For instance, when I add field-name-mapping-snake-case to the ignore like so:

image

I still see a lint error that I don't expect:

@sai/database:lint: prisma/schema.prisma ✖
@sai/database:lint:   AuthorizationCode 133:1
@sai/database:lint:     error Model name must be mapped to "authorization_codes". model-name-mapping-snake-case
@sai/database:lint:  ELIFECYCLE  Command failed with exit code 1.
@sai/database:lint: ERROR: command finished with error: command (/Users/duane/schoolai/app/packages/database) /Users/duane/.nvm/versions/node/v20.13.1/bin/pnpm run lint exited (1)
@sai/database#lint: command (/Users/duane/schoolai/app/packages/database) /Users/duane/.nvm/versions/node/v20.13.1/bin/pnpm run lint exited (1)
maxh commented 2 weeks ago

Ah, the error to ignore here is about model name mapping, rather than field name mapping. So try an ignore comment like:

/// prisma-lint-ignore-model model-name-mapping-snake-case

In general, the name of the rule is printed to the right of the error message line, and that's what should be on the ignore line.

canadaduane commented 2 weeks ago

Thank you! That works perfectly.