eslint-plugin-zod 3.9.0 → 3.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,6 +44,7 @@ This plugin is primarily built for `zod`, so some rules are exclusive to `zod` a
44
44
  | [consistent-import-source](docs/rules/consistent-import-source.md) | Enforce consistent source from Zod imports | | | 💡 | |
45
45
  | [consistent-object-schema-type](docs/rules/consistent-object-schema-type.md) | Enforce consistent usage of Zod schema methods | | | 💡 | |
46
46
  | [consistent-schema-output-type-style](docs/rules/consistent-schema-output-type-style.md) | Enforce consistent use of z.infer or z.output for schema type inference | | 🔧 | | |
47
+ | [consistent-schema-var-name](docs/rules/consistent-schema-var-name.md) | Enforce a consistent naming convention for Zod schema variables | ✅ ✔️ | | | |
47
48
  | [no-any-schema](docs/rules/no-any-schema.md) | Disallow usage of `z.any()` in Zod schemas | ✅ ✔️ | | 💡 | |
48
49
  | [no-empty-custom-schema](docs/rules/no-empty-custom-schema.md) | Disallow usage of `z.custom()` without arguments | ✅ | | | |
49
50
  | [no-unknown-schema](docs/rules/no-unknown-schema.md) | Disallow usage of `z.unknown()` in Zod schemas | | | | |
@@ -51,7 +52,7 @@ This plugin is primarily built for `zod`, so some rules are exclusive to `zod` a
51
52
  | [prefer-namespace-import](docs/rules/prefer-namespace-import.md) | Enforce importing zod as a namespace import (`import * as z from 'zod'`) | | 🔧 | | ❌ |
52
53
  | [require-brand-type-parameter](docs/rules/require-brand-type-parameter.md) | Require type parameter on `.brand()` functions | ✅ ✔️ | | 💡 | |
53
54
  | [require-error-message](docs/rules/require-error-message.md) | Enforce that custom refinements include an error message | ✅ ✔️ | 🔧 | | |
54
- | [require-schema-suffix](docs/rules/require-schema-suffix.md) | Require schema suffix when declaring a Zod schema | ✅ ✔️ | | | |
55
+ | [require-schema-suffix](docs/rules/require-schema-suffix.md) | Require schema suffix when declaring a Zod schema | | | ||
55
56
  | [schema-error-property-style](docs/rules/schema-error-property-style.md) | Enforce consistent style for error messages in Zod schema validation (using ESQuery patterns) | | | | |
56
57
 
57
58
  ### `zod` exclusive rules
package/dist/index.cjs CHANGED
@@ -4,6 +4,7 @@ const require_consistent_import_source = require("./rules/consistent-import-sour
4
4
  const require_consistent_import = require("./rules/consistent-import.cjs");
5
5
  const require_consistent_object_schema_type = require("./rules/consistent-object-schema-type.cjs");
6
6
  const require_consistent_schema_output_type_style = require("./rules/consistent-schema-output-type-style.cjs");
7
+ const require_consistent_schema_var_name = require("./rules/consistent-schema-var-name.cjs");
7
8
  const require_no_any_schema = require("./rules/no-any-schema.cjs");
8
9
  const require_no_empty_custom_schema = require("./rules/no-empty-custom-schema.cjs");
9
10
  const require_no_number_schema_with_finite = require("./rules/no-number-schema-with-finite.cjs");
@@ -37,6 +38,7 @@ const eslintPluginZod = {
37
38
  "consistent-import-source": require_consistent_import_source.consistentImportSource,
38
39
  "consistent-import": require_consistent_import.consistentImport,
39
40
  "consistent-object-schema-type": require_consistent_object_schema_type.consistentObjectSchemaType,
41
+ "consistent-schema-var-name": require_consistent_schema_var_name.consistentSchemaVarName,
40
42
  "consistent-schema-output-type-style": require_consistent_schema_output_type_style.consistentSchemaOutputTypeStyle,
41
43
  "no-any-schema": require_no_any_schema.noAnySchema,
42
44
  "no-empty-custom-schema": require_no_empty_custom_schema.noEmptyCustomSchema,
@@ -72,6 +74,7 @@ const recommendedConfig = {
72
74
  rules: {
73
75
  "zod/array-style": "error",
74
76
  "zod/consistent-import": "error",
77
+ "zod/consistent-schema-var-name": "error",
75
78
  "zod/no-any-schema": "error",
76
79
  "zod/no-empty-custom-schema": "error",
77
80
  "zod/no-number-schema-with-finite": "error",
@@ -88,18 +91,17 @@ const recommendedConfig = {
88
91
  "zod/prefer-meta-last": "error",
89
92
  "zod/prefer-string-schema-with-trim": "error",
90
93
  "zod/require-brand-type-parameter": "error",
91
- "zod/require-error-message": "error",
92
- "zod/require-schema-suffix": "error"
94
+ "zod/require-error-message": "error"
93
95
  }
94
96
  };
95
97
  const recommendedConfigMini = {
96
98
  ...baseConfig,
97
99
  rules: {
100
+ "zod/consistent-schema-var-name": "error",
98
101
  "zod/no-any-schema": "error",
99
102
  "zod/prefer-meta": "error",
100
103
  "zod/require-brand-type-parameter": "error",
101
- "zod/require-error-message": "error",
102
- "zod/require-schema-suffix": "error"
104
+ "zod/require-error-message": "error"
103
105
  }
104
106
  };
105
107
  var src_default = {
package/dist/index.mjs CHANGED
@@ -4,6 +4,7 @@ import { consistentImportSource } from "./rules/consistent-import-source.mjs";
4
4
  import { consistentImport } from "./rules/consistent-import.mjs";
5
5
  import { consistentObjectSchemaType } from "./rules/consistent-object-schema-type.mjs";
6
6
  import { consistentSchemaOutputTypeStyle } from "./rules/consistent-schema-output-type-style.mjs";
7
+ import { consistentSchemaVarName } from "./rules/consistent-schema-var-name.mjs";
7
8
  import { noAnySchema } from "./rules/no-any-schema.mjs";
8
9
  import { noEmptyCustomSchema } from "./rules/no-empty-custom-schema.mjs";
9
10
  import { noNumberSchemaWithFinite } from "./rules/no-number-schema-with-finite.mjs";
@@ -37,6 +38,7 @@ const eslintPluginZod = {
37
38
  "consistent-import-source": consistentImportSource,
38
39
  "consistent-import": consistentImport,
39
40
  "consistent-object-schema-type": consistentObjectSchemaType,
41
+ "consistent-schema-var-name": consistentSchemaVarName,
40
42
  "consistent-schema-output-type-style": consistentSchemaOutputTypeStyle,
41
43
  "no-any-schema": noAnySchema,
42
44
  "no-empty-custom-schema": noEmptyCustomSchema,
@@ -72,6 +74,7 @@ const recommendedConfig = {
72
74
  rules: {
73
75
  "zod/array-style": "error",
74
76
  "zod/consistent-import": "error",
77
+ "zod/consistent-schema-var-name": "error",
75
78
  "zod/no-any-schema": "error",
76
79
  "zod/no-empty-custom-schema": "error",
77
80
  "zod/no-number-schema-with-finite": "error",
@@ -88,18 +91,17 @@ const recommendedConfig = {
88
91
  "zod/prefer-meta-last": "error",
89
92
  "zod/prefer-string-schema-with-trim": "error",
90
93
  "zod/require-brand-type-parameter": "error",
91
- "zod/require-error-message": "error",
92
- "zod/require-schema-suffix": "error"
94
+ "zod/require-error-message": "error"
93
95
  }
94
96
  };
95
97
  const recommendedConfigMini = {
96
98
  ...baseConfig,
97
99
  rules: {
100
+ "zod/consistent-schema-var-name": "error",
98
101
  "zod/no-any-schema": "error",
99
102
  "zod/prefer-meta": "error",
100
103
  "zod/require-brand-type-parameter": "error",
101
- "zod/require-error-message": "error",
102
- "zod/require-schema-suffix": "error"
104
+ "zod/require-error-message": "error"
103
105
  }
104
106
  };
105
107
  var src_default = {
@@ -0,0 +1,76 @@
1
+ require("../_virtual/_rolldown/runtime.cjs");
2
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
+ const require_track_zod_schema_imports = require("../utils/track-zod-schema-imports.cjs");
4
+ let _typescript_eslint_utils = require("@typescript-eslint/utils");
5
+ //#region src/rules/consistent-schema-var-name.ts
6
+ const { zodImportAllowedSource, trackZodSchemaImports } = require_track_zod_schema_imports.createZodSchemaImportTrack("all");
7
+ const consistentSchemaVarName = require_create_plugin_rule.createZodPluginRule({
8
+ name: "consistent-schema-var-name",
9
+ meta: {
10
+ type: "suggestion",
11
+ docs: {
12
+ zodImportAllowedSource,
13
+ description: "Enforce a consistent naming convention for Zod schema variables"
14
+ },
15
+ messages: { invalidName: "Rename this Zod schema to \"{{expected}}\"" },
16
+ schema: [{
17
+ type: "object",
18
+ properties: {
19
+ before: {
20
+ type: "string",
21
+ description: "The required prefix for Zod schema variables"
22
+ },
23
+ after: {
24
+ type: "string",
25
+ description: "The required suffix for Zod schema variables"
26
+ }
27
+ },
28
+ additionalProperties: false
29
+ }]
30
+ },
31
+ defaultOptions: [{ after: "Schema" }],
32
+ create(context, [{ before = "", after = "" }]) {
33
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
34
+ return {
35
+ ImportDeclaration: importDeclarationListener,
36
+ VariableDeclarator(node) {
37
+ const initNode = node.init;
38
+ if (initNode?.type !== _typescript_eslint_utils.AST_NODE_TYPES.CallExpression || !detectZodSchemaRootNode(initNode)) return;
39
+ const chainMethods = collectZodChainMethods(initNode).map((it) => it.name);
40
+ if ([
41
+ "parse",
42
+ "parseAsync",
43
+ "safeParse",
44
+ "safeParseAsync",
45
+ "spa",
46
+ "encode",
47
+ "encodeAsync",
48
+ "decode",
49
+ "decodeAsync",
50
+ "safeEncode",
51
+ "safeEncodeAsync",
52
+ "safeDecode",
53
+ "safeDecodeAsync",
54
+ "codec",
55
+ "treeifyError",
56
+ "prettifyError",
57
+ "formatError",
58
+ "flattenError"
59
+ ].some((it) => chainMethods.includes(it))) return;
60
+ if (node.id.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier) return;
61
+ const { name } = node.id;
62
+ const validPrefix = !before || name.startsWith(before);
63
+ const validSuffix = !after || name.endsWith(after);
64
+ if (validPrefix && validSuffix) return;
65
+ const expected = (validPrefix ? "" : before) + name + (validSuffix ? "" : after);
66
+ context.report({
67
+ node,
68
+ messageId: "invalidName",
69
+ data: { expected }
70
+ });
71
+ }
72
+ };
73
+ }
74
+ });
75
+ //#endregion
76
+ exports.consistentSchemaVarName = consistentSchemaVarName;
@@ -0,0 +1,75 @@
1
+ import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { createZodSchemaImportTrack } from "../utils/track-zod-schema-imports.mjs";
3
+ import { AST_NODE_TYPES } from "@typescript-eslint/utils";
4
+ //#region src/rules/consistent-schema-var-name.ts
5
+ const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("all");
6
+ const consistentSchemaVarName = createZodPluginRule({
7
+ name: "consistent-schema-var-name",
8
+ meta: {
9
+ type: "suggestion",
10
+ docs: {
11
+ zodImportAllowedSource,
12
+ description: "Enforce a consistent naming convention for Zod schema variables"
13
+ },
14
+ messages: { invalidName: "Rename this Zod schema to \"{{expected}}\"" },
15
+ schema: [{
16
+ type: "object",
17
+ properties: {
18
+ before: {
19
+ type: "string",
20
+ description: "The required prefix for Zod schema variables"
21
+ },
22
+ after: {
23
+ type: "string",
24
+ description: "The required suffix for Zod schema variables"
25
+ }
26
+ },
27
+ additionalProperties: false
28
+ }]
29
+ },
30
+ defaultOptions: [{ after: "Schema" }],
31
+ create(context, [{ before = "", after = "" }]) {
32
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
33
+ return {
34
+ ImportDeclaration: importDeclarationListener,
35
+ VariableDeclarator(node) {
36
+ const initNode = node.init;
37
+ if (initNode?.type !== AST_NODE_TYPES.CallExpression || !detectZodSchemaRootNode(initNode)) return;
38
+ const chainMethods = collectZodChainMethods(initNode).map((it) => it.name);
39
+ if ([
40
+ "parse",
41
+ "parseAsync",
42
+ "safeParse",
43
+ "safeParseAsync",
44
+ "spa",
45
+ "encode",
46
+ "encodeAsync",
47
+ "decode",
48
+ "decodeAsync",
49
+ "safeEncode",
50
+ "safeEncodeAsync",
51
+ "safeDecode",
52
+ "safeDecodeAsync",
53
+ "codec",
54
+ "treeifyError",
55
+ "prettifyError",
56
+ "formatError",
57
+ "flattenError"
58
+ ].some((it) => chainMethods.includes(it))) return;
59
+ if (node.id.type !== AST_NODE_TYPES.Identifier) return;
60
+ const { name } = node.id;
61
+ const validPrefix = !before || name.startsWith(before);
62
+ const validSuffix = !after || name.endsWith(after);
63
+ if (validPrefix && validSuffix) return;
64
+ const expected = (validPrefix ? "" : before) + name + (validSuffix ? "" : after);
65
+ context.report({
66
+ node,
67
+ messageId: "invalidName",
68
+ data: { expected }
69
+ });
70
+ }
71
+ };
72
+ }
73
+ });
74
+ //#endregion
75
+ export { consistentSchemaVarName };
@@ -8,6 +8,7 @@ const requireSchemaSuffix = require_create_plugin_rule.createZodPluginRule({
8
8
  name: "require-schema-suffix",
9
9
  meta: {
10
10
  type: "suggestion",
11
+ deprecated: { message: "Use `zod/consistent-schema-var-name`" },
11
12
  docs: {
12
13
  zodImportAllowedSource,
13
14
  description: "Require schema suffix when declaring a Zod schema"
@@ -7,6 +7,7 @@ const requireSchemaSuffix = createZodPluginRule({
7
7
  name: "require-schema-suffix",
8
8
  meta: {
9
9
  type: "suggestion",
10
+ deprecated: { message: "Use `zod/consistent-schema-var-name`" },
10
11
  docs: {
11
12
  zodImportAllowedSource,
12
13
  description: "Require schema suffix when declaring a Zod schema"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-zod",
3
- "version": "3.9.0",
3
+ "version": "3.11.0",
4
4
  "type": "module",
5
5
  "description": "ESLint plugin that adds custom linting rules to enforce best practices when using Zod",
6
6
  "engines": {