prisma-lint
Advanced tools
Comparing version 0.1.3 to 0.2.0
import pluralize from 'pluralize'; | ||
import { z } from 'zod'; | ||
import { isRegexOrRegexStr, toRegExp } from '#src/common/regex.js'; | ||
const RULE_NAME = 'model-name-grammatical-number'; | ||
@@ -7,2 +8,3 @@ const Config = z | ||
style: z.enum(['singular', 'plural']), | ||
allowlist: z.array(z.union([z.string(), z.instanceof(RegExp)])).optional(), | ||
}) | ||
@@ -37,2 +39,43 @@ .strict(); | ||
* | ||
* @example { style: "singular", allowlist: ["UserData"] } | ||
* // good | ||
* model UserData { | ||
* id String @id | ||
* } | ||
* | ||
* model User { | ||
* id String @id | ||
* } | ||
* | ||
* model Tenant { | ||
* id String @id | ||
* } | ||
* | ||
* // bad ("data" is considered plural by default) | ||
* model TenantData { | ||
* id String @id | ||
* } | ||
* | ||
* model Users { | ||
* id String @id | ||
* } | ||
* | ||
* @example { style: "singular", allowlist: ["/Data$/"] } | ||
* // good | ||
* model UserData { | ||
* id String @id | ||
* } | ||
* | ||
* model TenantData { | ||
* id String @id | ||
* } | ||
* | ||
* // bad | ||
* model DataRecords { | ||
* id String @id | ||
* } | ||
* | ||
* model Users { | ||
* id String @id | ||
* } | ||
*/ | ||
@@ -44,5 +87,17 @@ export default { | ||
const { style } = config; | ||
const allowlist = config.allowlist ?? []; | ||
const simpleAllowlist = allowlist.filter((s) => !isRegexOrRegexStr(s)); | ||
const regexAllowlist = allowlist | ||
.filter((s) => isRegexOrRegexStr(s)) | ||
.map(toRegExp); | ||
return { | ||
Model: (model) => { | ||
const isPlural = pluralize.isPlural(model.name); | ||
const modelName = model.name; | ||
if (simpleAllowlist.includes(modelName)) { | ||
return; | ||
} | ||
if (regexAllowlist.some((r) => r.test(modelName))) { | ||
return; | ||
} | ||
const isPlural = pluralize.isPlural(modelName); | ||
if (isPlural && style === 'singular') { | ||
@@ -49,0 +104,0 @@ context.report({ model, message: 'Expected singular model name.' }); |
@@ -8,5 +8,5 @@ import { z } from 'zod'; | ||
compoundWords: z.array(z.string()).optional(), | ||
irregularPlurals: z.record(z.string()).optional(), | ||
pluralize: z.boolean().optional(), | ||
trimPrefix: z.string().optional(), | ||
pluralize: z.boolean().optional(), | ||
irregularPlurals: z.record(z.string()).optional(), | ||
}) | ||
@@ -13,0 +13,0 @@ .strict() |
{ | ||
"name": "prisma-lint", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "A linter for Prisma schema files.", | ||
@@ -5,0 +5,0 @@ "repository": { |
62938
1774