eslint-plugin-zod 3.5.1 → 3.5.2

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.
Files changed (138) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/dist/index.cjs +94 -83
  3. package/dist/index.d.cts +17 -16
  4. package/dist/index.d.mts +24 -0
  5. package/dist/index.mjs +98 -0
  6. package/dist/meta.cjs +11 -11
  7. package/dist/meta.mjs +10 -0
  8. package/dist/rules/array-style.cjs +88 -101
  9. package/dist/rules/array-style.mjs +89 -0
  10. package/dist/rules/consistent-import-source.cjs +59 -66
  11. package/dist/rules/consistent-import-source.mjs +60 -0
  12. package/dist/rules/consistent-import.cjs +146 -172
  13. package/dist/rules/consistent-import.mjs +152 -0
  14. package/dist/rules/consistent-object-schema-type.cjs +81 -84
  15. package/dist/rules/consistent-object-schema-type.mjs +81 -0
  16. package/dist/rules/no-any-schema.cjs +62 -67
  17. package/dist/rules/no-any-schema.mjs +62 -0
  18. package/dist/rules/no-empty-custom-schema.cjs +32 -40
  19. package/dist/rules/no-empty-custom-schema.mjs +34 -0
  20. package/dist/rules/no-number-schema-with-int.cjs +56 -62
  21. package/dist/rules/no-number-schema-with-int.mjs +57 -0
  22. package/dist/rules/no-optional-and-default-together.cjs +69 -80
  23. package/dist/rules/no-optional-and-default-together.mjs +71 -0
  24. package/dist/rules/no-string-schema-with-uuid.cjs +57 -63
  25. package/dist/rules/no-string-schema-with-uuid.mjs +58 -0
  26. package/dist/rules/no-throw-in-refine.cjs +69 -87
  27. package/dist/rules/no-throw-in-refine.mjs +69 -0
  28. package/dist/rules/no-unknown-schema.cjs +29 -35
  29. package/dist/rules/no-unknown-schema.mjs +31 -0
  30. package/dist/rules/prefer-enum-over-literal-union.cjs +63 -79
  31. package/dist/rules/prefer-enum-over-literal-union.mjs +63 -0
  32. package/dist/rules/prefer-meta-last.cjs +47 -63
  33. package/dist/rules/prefer-meta-last.mjs +49 -0
  34. package/dist/rules/prefer-meta.cjs +38 -48
  35. package/dist/rules/prefer-meta.mjs +40 -0
  36. package/dist/rules/prefer-namespace-import.cjs +110 -142
  37. package/dist/rules/prefer-namespace-import.mjs +116 -0
  38. package/dist/rules/prefer-string-schema-with-trim.cjs +45 -52
  39. package/dist/rules/prefer-string-schema-with-trim.mjs +47 -0
  40. package/dist/rules/require-brand-type-parameter.cjs +47 -60
  41. package/dist/rules/require-brand-type-parameter.mjs +49 -0
  42. package/dist/rules/require-error-message.cjs +83 -102
  43. package/dist/rules/require-error-message.mjs +83 -0
  44. package/dist/rules/require-schema-suffix.cjs +67 -78
  45. package/dist/rules/require-schema-suffix.mjs +67 -0
  46. package/dist/rules/schema-error-property-style.cjs +95 -107
  47. package/dist/rules/schema-error-property-style.mjs +94 -0
  48. package/dist/utils/build-zod-chain-replacement-fix.cjs +28 -29
  49. package/dist/utils/build-zod-chain-replacement-fix.mjs +30 -0
  50. package/dist/utils/create-plugin-rule.cjs +6 -6
  51. package/dist/utils/create-plugin-rule.mjs +6 -0
  52. package/dist/utils/detect-zod-schema-root-node.cjs +94 -96
  53. package/dist/utils/detect-zod-schema-root-node.mjs +101 -0
  54. package/dist/utils/is-zod-import-source.cjs +14 -18
  55. package/dist/utils/is-zod-import-source.mjs +16 -0
  56. package/dist/utils/track-zod-schema-imports.cjs +61 -63
  57. package/dist/utils/track-zod-schema-imports.mjs +64 -0
  58. package/package.json +14 -17
  59. package/dist/index.d.ts +0 -22
  60. package/dist/index.js +0 -84
  61. package/dist/meta.d.cts +0 -3
  62. package/dist/meta.d.ts +0 -3
  63. package/dist/meta.js +0 -8
  64. package/dist/rules/array-style.d.cts +0 -11
  65. package/dist/rules/array-style.d.ts +0 -11
  66. package/dist/rules/array-style.js +0 -99
  67. package/dist/rules/consistent-import-source.d.cts +0 -11
  68. package/dist/rules/consistent-import-source.d.ts +0 -11
  69. package/dist/rules/consistent-import-source.js +0 -64
  70. package/dist/rules/consistent-import.d.cts +0 -12
  71. package/dist/rules/consistent-import.d.ts +0 -12
  72. package/dist/rules/consistent-import.js +0 -176
  73. package/dist/rules/consistent-object-schema-type.d.cts +0 -13
  74. package/dist/rules/consistent-object-schema-type.d.ts +0 -13
  75. package/dist/rules/consistent-object-schema-type.js +0 -82
  76. package/dist/rules/no-any-schema.d.cts +0 -6
  77. package/dist/rules/no-any-schema.d.ts +0 -6
  78. package/dist/rules/no-any-schema.js +0 -65
  79. package/dist/rules/no-empty-custom-schema.d.cts +0 -5
  80. package/dist/rules/no-empty-custom-schema.d.ts +0 -5
  81. package/dist/rules/no-empty-custom-schema.js +0 -38
  82. package/dist/rules/no-number-schema-with-int.d.cts +0 -5
  83. package/dist/rules/no-number-schema-with-int.d.ts +0 -5
  84. package/dist/rules/no-number-schema-with-int.js +0 -60
  85. package/dist/rules/no-optional-and-default-together.d.cts +0 -12
  86. package/dist/rules/no-optional-and-default-together.d.ts +0 -12
  87. package/dist/rules/no-optional-and-default-together.js +0 -78
  88. package/dist/rules/no-string-schema-with-uuid.d.cts +0 -5
  89. package/dist/rules/no-string-schema-with-uuid.d.ts +0 -5
  90. package/dist/rules/no-string-schema-with-uuid.js +0 -61
  91. package/dist/rules/no-throw-in-refine.d.cts +0 -5
  92. package/dist/rules/no-throw-in-refine.d.ts +0 -5
  93. package/dist/rules/no-throw-in-refine.js +0 -85
  94. package/dist/rules/no-unknown-schema.d.cts +0 -5
  95. package/dist/rules/no-unknown-schema.d.ts +0 -5
  96. package/dist/rules/no-unknown-schema.js +0 -33
  97. package/dist/rules/prefer-enum-over-literal-union.d.cts +0 -5
  98. package/dist/rules/prefer-enum-over-literal-union.d.ts +0 -5
  99. package/dist/rules/prefer-enum-over-literal-union.js +0 -77
  100. package/dist/rules/prefer-meta-last.d.cts +0 -5
  101. package/dist/rules/prefer-meta-last.d.ts +0 -5
  102. package/dist/rules/prefer-meta-last.js +0 -61
  103. package/dist/rules/prefer-meta.d.cts +0 -5
  104. package/dist/rules/prefer-meta.d.ts +0 -5
  105. package/dist/rules/prefer-meta.js +0 -46
  106. package/dist/rules/prefer-namespace-import.d.cts +0 -5
  107. package/dist/rules/prefer-namespace-import.d.ts +0 -5
  108. package/dist/rules/prefer-namespace-import.js +0 -146
  109. package/dist/rules/prefer-string-schema-with-trim.d.cts +0 -5
  110. package/dist/rules/prefer-string-schema-with-trim.d.ts +0 -5
  111. package/dist/rules/prefer-string-schema-with-trim.js +0 -50
  112. package/dist/rules/require-brand-type-parameter.d.cts +0 -6
  113. package/dist/rules/require-brand-type-parameter.d.ts +0 -6
  114. package/dist/rules/require-brand-type-parameter.js +0 -58
  115. package/dist/rules/require-error-message.d.cts +0 -5
  116. package/dist/rules/require-error-message.d.ts +0 -5
  117. package/dist/rules/require-error-message.js +0 -100
  118. package/dist/rules/require-schema-suffix.d.cts +0 -9
  119. package/dist/rules/require-schema-suffix.d.ts +0 -9
  120. package/dist/rules/require-schema-suffix.js +0 -76
  121. package/dist/rules/schema-error-property-style.d.cts +0 -10
  122. package/dist/rules/schema-error-property-style.d.ts +0 -10
  123. package/dist/rules/schema-error-property-style.js +0 -102
  124. package/dist/utils/build-zod-chain-replacement-fix.d.cts +0 -12
  125. package/dist/utils/build-zod-chain-replacement-fix.d.ts +0 -12
  126. package/dist/utils/build-zod-chain-replacement-fix.js +0 -28
  127. package/dist/utils/create-plugin-rule.d.cts +0 -13
  128. package/dist/utils/create-plugin-rule.d.ts +0 -13
  129. package/dist/utils/create-plugin-rule.js +0 -3
  130. package/dist/utils/detect-zod-schema-root-node.d.cts +0 -10
  131. package/dist/utils/detect-zod-schema-root-node.d.ts +0 -10
  132. package/dist/utils/detect-zod-schema-root-node.js +0 -101
  133. package/dist/utils/is-zod-import-source.d.cts +0 -4
  134. package/dist/utils/is-zod-import-source.d.ts +0 -4
  135. package/dist/utils/is-zod-import-source.js +0 -17
  136. package/dist/utils/track-zod-schema-imports.d.cts +0 -18
  137. package/dist/utils/track-zod-schema-imports.d.ts +0 -18
  138. package/dist/utils/track-zod-schema-imports.js +0 -64
@@ -0,0 +1,83 @@
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/require-error-message.ts
5
+ const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("all");
6
+ const requireErrorMessage = createZodPluginRule({
7
+ name: "require-error-message",
8
+ meta: {
9
+ type: "suggestion",
10
+ fixable: "code",
11
+ docs: {
12
+ zodImportAllowedSource,
13
+ description: "Enforce that custom refinements include an error message"
14
+ },
15
+ messages: {
16
+ requireErrorMessage: "Custom refinements must include an error message",
17
+ preferError: "Use the \"error\" property instead of the deprecated \"message\" property",
18
+ removeMessage: "The \"message\" property is deprecated; use \"error\""
19
+ },
20
+ schema: []
21
+ },
22
+ defaultOptions: [],
23
+ create(context) {
24
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
25
+ return {
26
+ ImportDeclaration: importDeclarationListener,
27
+ CallExpression(node) {
28
+ if (!detectZodSchemaRootNode(node)) return;
29
+ const refine = collectZodChainMethods(node).find((it) => it.name === "refine" || it.name === "custom");
30
+ if (!refine) return;
31
+ const refineNode = refine.node;
32
+ if (refineNode.arguments.length < 2) {
33
+ context.report({
34
+ messageId: "requireErrorMessage",
35
+ node: node.callee
36
+ });
37
+ return;
38
+ }
39
+ const [, params] = refineNode.arguments;
40
+ if (params.type === AST_NODE_TYPES.Literal) return;
41
+ if (params.type === AST_NODE_TYPES.ObjectExpression) {
42
+ let errorPropertyNode;
43
+ let messagePropertyNode;
44
+ for (const property of params.properties) if (property.type === AST_NODE_TYPES.Property && property.key.type === AST_NODE_TYPES.Identifier) {
45
+ if (property.key.name === "error") errorPropertyNode = property;
46
+ if (property.key.name === "message") messagePropertyNode = property;
47
+ if (errorPropertyNode && messagePropertyNode) break;
48
+ }
49
+ if (errorPropertyNode && messagePropertyNode) {
50
+ context.report({
51
+ messageId: "removeMessage",
52
+ node: messagePropertyNode,
53
+ fix(fixer) {
54
+ const { sourceCode } = context;
55
+ const nextToken = sourceCode.getTokenAfter(messagePropertyNode);
56
+ let [, end] = messagePropertyNode.range;
57
+ if (nextToken?.value === ",") end = nextToken.range[1];
58
+ return fixer.removeRange([messagePropertyNode.range[0], end]);
59
+ }
60
+ });
61
+ return;
62
+ }
63
+ if (messagePropertyNode && !errorPropertyNode) {
64
+ context.report({
65
+ messageId: "preferError",
66
+ node: params,
67
+ fix(fixer) {
68
+ return fixer.replaceTextRange(messagePropertyNode.key.range, "error");
69
+ }
70
+ });
71
+ return;
72
+ }
73
+ if (!errorPropertyNode) context.report({
74
+ messageId: "requireErrorMessage",
75
+ node: params
76
+ });
77
+ }
78
+ }
79
+ };
80
+ }
81
+ });
82
+ //#endregion
83
+ export { requireErrorMessage };
@@ -1,79 +1,68 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requireSchemaSuffix = void 0;
4
- const utils_1 = require("@typescript-eslint/utils");
5
- const create_plugin_rule_js_1 = require("../utils/create-plugin-rule.cjs");
6
- const track_zod_schema_imports_js_1 = require("../utils/track-zod-schema-imports.cjs");
7
- const { zodImportAllowedSource, trackZodSchemaImports, } = (0, track_zod_schema_imports_js_1.createZodSchemaImportTrack)('all');
8
- exports.requireSchemaSuffix = (0, create_plugin_rule_js_1.createZodPluginRule)({
9
- name: 'require-schema-suffix',
10
- meta: {
11
- type: 'suggestion',
12
- docs: {
13
- zodImportAllowedSource,
14
- description: 'Require schema suffix when declaring a Zod schema',
15
- },
16
- messages: {
17
- noSchemaSuffix: 'Use the "{{suffix}}" suffix for Zod schemas. Rename this to "{{expected}}"',
18
- },
19
- schema: [
20
- {
21
- type: 'object',
22
- properties: {
23
- suffix: {
24
- type: 'string',
25
- description: 'The required suffix for Zod schema variables',
26
- },
27
- },
28
- additionalProperties: false,
29
- },
30
- ],
31
- },
32
- defaultOptions: [{ suffix: 'Schema' }],
33
- create(context, [{ suffix }]) {
34
- const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods, } = trackZodSchemaImports();
35
- return {
36
- ImportDeclaration: importDeclarationListener,
37
- VariableDeclarator(node) {
38
- const initNode = node.init;
39
- if ((initNode === null || initNode === void 0 ? void 0 : initNode.type) !== utils_1.AST_NODE_TYPES.CallExpression ||
40
- !detectZodSchemaRootNode(initNode)) {
41
- return;
42
- }
43
- const chainMethods = collectZodChainMethods(initNode).map((it) => it.name);
44
- const methodsThatProduceSomethingThatShouldNotBeValidated = [
45
- 'parse',
46
- 'parseAsync',
47
- 'safeParse',
48
- 'safeParseAsync',
49
- 'spa',
50
- 'encode',
51
- 'encodeAsync',
52
- 'decode',
53
- 'decodeAsync',
54
- 'safeEncode',
55
- 'safeEncodeAsync',
56
- 'safeDecode',
57
- 'safeDecodeAsync',
58
- 'codec',
59
- 'treeifyError',
60
- 'prettifyError',
61
- 'formatError',
62
- 'flattenError',
63
- ];
64
- if (methodsThatProduceSomethingThatShouldNotBeValidated.some((it) => chainMethods.includes(it))) {
65
- return;
66
- }
67
- if (node.id.type !== utils_1.AST_NODE_TYPES.Identifier ||
68
- node.id.name.endsWith(suffix)) {
69
- return;
70
- }
71
- context.report({
72
- node,
73
- messageId: 'noSchemaSuffix',
74
- data: { suffix, expected: `${node.id.name}${suffix}` },
75
- });
76
- },
77
- };
78
- },
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/require-schema-suffix.ts
6
+ const { zodImportAllowedSource, trackZodSchemaImports } = require_track_zod_schema_imports.createZodSchemaImportTrack("all");
7
+ const requireSchemaSuffix = require_create_plugin_rule.createZodPluginRule({
8
+ name: "require-schema-suffix",
9
+ meta: {
10
+ type: "suggestion",
11
+ docs: {
12
+ zodImportAllowedSource,
13
+ description: "Require schema suffix when declaring a Zod schema"
14
+ },
15
+ messages: { noSchemaSuffix: "Use the \"{{suffix}}\" suffix for Zod schemas. Rename this to \"{{expected}}\"" },
16
+ schema: [{
17
+ type: "object",
18
+ properties: { suffix: {
19
+ type: "string",
20
+ description: "The required suffix for Zod schema variables"
21
+ } },
22
+ additionalProperties: false
23
+ }]
24
+ },
25
+ defaultOptions: [{ suffix: "Schema" }],
26
+ create(context, [{ suffix }]) {
27
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
28
+ return {
29
+ ImportDeclaration: importDeclarationListener,
30
+ VariableDeclarator(node) {
31
+ const initNode = node.init;
32
+ if (initNode?.type !== _typescript_eslint_utils.AST_NODE_TYPES.CallExpression || !detectZodSchemaRootNode(initNode)) return;
33
+ const chainMethods = collectZodChainMethods(initNode).map((it) => it.name);
34
+ if ([
35
+ "parse",
36
+ "parseAsync",
37
+ "safeParse",
38
+ "safeParseAsync",
39
+ "spa",
40
+ "encode",
41
+ "encodeAsync",
42
+ "decode",
43
+ "decodeAsync",
44
+ "safeEncode",
45
+ "safeEncodeAsync",
46
+ "safeDecode",
47
+ "safeDecodeAsync",
48
+ "codec",
49
+ "treeifyError",
50
+ "prettifyError",
51
+ "formatError",
52
+ "flattenError"
53
+ ].some((it) => chainMethods.includes(it))) return;
54
+ if (node.id.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier || node.id.name.endsWith(suffix)) return;
55
+ context.report({
56
+ node,
57
+ messageId: "noSchemaSuffix",
58
+ data: {
59
+ suffix,
60
+ expected: `${node.id.name}${suffix}`
61
+ }
62
+ });
63
+ }
64
+ };
65
+ }
79
66
  });
67
+ //#endregion
68
+ exports.requireSchemaSuffix = requireSchemaSuffix;
@@ -0,0 +1,67 @@
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/require-schema-suffix.ts
5
+ const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("all");
6
+ const requireSchemaSuffix = createZodPluginRule({
7
+ name: "require-schema-suffix",
8
+ meta: {
9
+ type: "suggestion",
10
+ docs: {
11
+ zodImportAllowedSource,
12
+ description: "Require schema suffix when declaring a Zod schema"
13
+ },
14
+ messages: { noSchemaSuffix: "Use the \"{{suffix}}\" suffix for Zod schemas. Rename this to \"{{expected}}\"" },
15
+ schema: [{
16
+ type: "object",
17
+ properties: { suffix: {
18
+ type: "string",
19
+ description: "The required suffix for Zod schema variables"
20
+ } },
21
+ additionalProperties: false
22
+ }]
23
+ },
24
+ defaultOptions: [{ suffix: "Schema" }],
25
+ create(context, [{ suffix }]) {
26
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
27
+ return {
28
+ ImportDeclaration: importDeclarationListener,
29
+ VariableDeclarator(node) {
30
+ const initNode = node.init;
31
+ if (initNode?.type !== AST_NODE_TYPES.CallExpression || !detectZodSchemaRootNode(initNode)) return;
32
+ const chainMethods = collectZodChainMethods(initNode).map((it) => it.name);
33
+ if ([
34
+ "parse",
35
+ "parseAsync",
36
+ "safeParse",
37
+ "safeParseAsync",
38
+ "spa",
39
+ "encode",
40
+ "encodeAsync",
41
+ "decode",
42
+ "decodeAsync",
43
+ "safeEncode",
44
+ "safeEncodeAsync",
45
+ "safeDecode",
46
+ "safeDecodeAsync",
47
+ "codec",
48
+ "treeifyError",
49
+ "prettifyError",
50
+ "formatError",
51
+ "flattenError"
52
+ ].some((it) => chainMethods.includes(it))) return;
53
+ if (node.id.type !== AST_NODE_TYPES.Identifier || node.id.name.endsWith(suffix)) return;
54
+ context.report({
55
+ node,
56
+ messageId: "noSchemaSuffix",
57
+ data: {
58
+ suffix,
59
+ expected: `${node.id.name}${suffix}`
60
+ }
61
+ });
62
+ }
63
+ };
64
+ }
65
+ });
66
+ //#endregion
67
+ export { requireSchemaSuffix };
@@ -1,108 +1,96 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.schemaErrorPropertyStyle = void 0;
7
- const utils_1 = require("@typescript-eslint/utils");
8
- const esquery_1 = __importDefault(require("esquery"));
9
- const create_plugin_rule_js_1 = require("../utils/create-plugin-rule.cjs");
10
- const track_zod_schema_imports_js_1 = require("../utils/track-zod-schema-imports.cjs");
11
- const { zodImportAllowedSource, trackZodSchemaImports, } = (0, track_zod_schema_imports_js_1.createZodSchemaImportTrack)('all');
12
- exports.schemaErrorPropertyStyle = (0, create_plugin_rule_js_1.createZodPluginRule)({
13
- name: 'schema-error-property-style',
14
- meta: {
15
- type: 'suggestion',
16
- docs: {
17
- zodImportAllowedSource,
18
- description: 'Enforce consistent style for error messages in Zod schema validation (using ESQuery patterns)',
19
- },
20
- messages: {
21
- invalidSelector: 'Invalid ESQuery selector: "{{selector}}"',
22
- invalidStyle: 'Error message must follow the pattern "{{selector}}" (e.g., {{example}}). Found: {{actual}}.',
23
- },
24
- schema: [
25
- {
26
- type: 'object',
27
- properties: {
28
- selector: {
29
- description: 'An ESQuery string to match the required pattern',
30
- type: 'string',
31
- },
32
- example: {
33
- description: 'Example code to help the user understand the required pattern',
34
- type: 'string',
35
- },
36
- },
37
- additionalProperties: false,
38
- },
39
- ],
40
- },
41
- defaultOptions: [
42
- { selector: 'Literal,TemplateLiteral', example: "'error message'" },
43
- ],
44
- create(context, [{ selector, example }]) {
45
- const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods, } = trackZodSchemaImports();
46
- try {
47
- esquery_1.default.parse(selector);
48
- }
49
- catch (_a) {
50
- context.report({
51
- loc: { line: 1, column: 0 },
52
- messageId: 'invalidSelector',
53
- data: { selector },
54
- });
55
- return {};
56
- }
57
- return {
58
- ImportDeclaration: importDeclarationListener,
59
- CallExpression(node) {
60
- const zodSchemaMeta = detectZodSchemaRootNode(node);
61
- if (!zodSchemaMeta) {
62
- return;
63
- }
64
- if (zodSchemaMeta.schemaType !== 'custom' &&
65
- !collectZodChainMethods(node).some((it) => it.name === 'refine')) {
66
- return;
67
- }
68
- if (node.arguments.length < 2) {
69
- return;
70
- }
71
- let errorMessageNode;
72
- const [, params] = node.arguments;
73
- switch (params.type) {
74
- case utils_1.AST_NODE_TYPES.Literal:
75
- case utils_1.AST_NODE_TYPES.TemplateLiteral:
76
- errorMessageNode = params;
77
- break;
78
- case utils_1.AST_NODE_TYPES.ObjectExpression:
79
- for (const property of params.properties) {
80
- if (property.type === utils_1.AST_NODE_TYPES.Property &&
81
- property.key.type === utils_1.AST_NODE_TYPES.Identifier &&
82
- property.key.name === 'error') {
83
- errorMessageNode = property.value;
84
- break;
85
- }
86
- }
87
- break;
88
- }
89
- if (!errorMessageNode) {
90
- return;
91
- }
92
- const match = esquery_1.default.matches(errorMessageNode, esquery_1.default.parse(selector), errorMessageNode);
93
- if (match) {
94
- return;
95
- }
96
- context.report({
97
- node,
98
- messageId: 'invalidStyle',
99
- data: {
100
- selector,
101
- example,
102
- actual: context.sourceCode.getText(errorMessageNode),
103
- },
104
- });
105
- },
106
- };
107
- },
1
+ const require_runtime = 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
+ let esquery = require("esquery");
6
+ esquery = require_runtime.__toESM(esquery);
7
+ //#region src/rules/schema-error-property-style.ts
8
+ const { zodImportAllowedSource, trackZodSchemaImports } = require_track_zod_schema_imports.createZodSchemaImportTrack("all");
9
+ const schemaErrorPropertyStyle = require_create_plugin_rule.createZodPluginRule({
10
+ name: "schema-error-property-style",
11
+ meta: {
12
+ type: "suggestion",
13
+ docs: {
14
+ zodImportAllowedSource,
15
+ description: "Enforce consistent style for error messages in Zod schema validation (using ESQuery patterns)"
16
+ },
17
+ messages: {
18
+ invalidSelector: "Invalid ESQuery selector: \"{{selector}}\"",
19
+ invalidStyle: "Error message must follow the pattern \"{{selector}}\" (e.g., {{example}}). Found: {{actual}}."
20
+ },
21
+ schema: [{
22
+ type: "object",
23
+ properties: {
24
+ selector: {
25
+ description: "An ESQuery string to match the required pattern",
26
+ type: "string"
27
+ },
28
+ example: {
29
+ description: "Example code to help the user understand the required pattern",
30
+ type: "string"
31
+ }
32
+ },
33
+ additionalProperties: false
34
+ }]
35
+ },
36
+ defaultOptions: [{
37
+ selector: "Literal,TemplateLiteral",
38
+ example: "'error message'"
39
+ }],
40
+ create(context, [{ selector, example }]) {
41
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
42
+ /**
43
+ * Parsing `selector` to ensure it is valid,
44
+ * if not report an error and return empty rule listener
45
+ */
46
+ try {
47
+ esquery.default.parse(selector);
48
+ } catch {
49
+ context.report({
50
+ loc: {
51
+ line: 1,
52
+ column: 0
53
+ },
54
+ messageId: "invalidSelector",
55
+ data: { selector }
56
+ });
57
+ return {};
58
+ }
59
+ return {
60
+ ImportDeclaration: importDeclarationListener,
61
+ CallExpression(node) {
62
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
63
+ if (!zodSchemaMeta) return;
64
+ if (zodSchemaMeta.schemaType !== "custom" && !collectZodChainMethods(node).some((it) => it.name === "refine")) return;
65
+ if (node.arguments.length < 2) return;
66
+ let errorMessageNode;
67
+ const [, params] = node.arguments;
68
+ switch (params.type) {
69
+ case _typescript_eslint_utils.AST_NODE_TYPES.Literal:
70
+ case _typescript_eslint_utils.AST_NODE_TYPES.TemplateLiteral:
71
+ errorMessageNode = params;
72
+ break;
73
+ case _typescript_eslint_utils.AST_NODE_TYPES.ObjectExpression:
74
+ for (const property of params.properties) if (property.type === _typescript_eslint_utils.AST_NODE_TYPES.Property && property.key.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier && property.key.name === "error") {
75
+ errorMessageNode = property.value;
76
+ break;
77
+ }
78
+ break;
79
+ }
80
+ if (!errorMessageNode) return;
81
+ if (esquery.default.matches(errorMessageNode, esquery.default.parse(selector), errorMessageNode)) return;
82
+ context.report({
83
+ node,
84
+ messageId: "invalidStyle",
85
+ data: {
86
+ selector,
87
+ example,
88
+ actual: context.sourceCode.getText(errorMessageNode)
89
+ }
90
+ });
91
+ }
92
+ };
93
+ }
108
94
  });
95
+ //#endregion
96
+ exports.schemaErrorPropertyStyle = schemaErrorPropertyStyle;
@@ -0,0 +1,94 @@
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
+ import esquery from "esquery";
5
+ //#region src/rules/schema-error-property-style.ts
6
+ const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("all");
7
+ const schemaErrorPropertyStyle = createZodPluginRule({
8
+ name: "schema-error-property-style",
9
+ meta: {
10
+ type: "suggestion",
11
+ docs: {
12
+ zodImportAllowedSource,
13
+ description: "Enforce consistent style for error messages in Zod schema validation (using ESQuery patterns)"
14
+ },
15
+ messages: {
16
+ invalidSelector: "Invalid ESQuery selector: \"{{selector}}\"",
17
+ invalidStyle: "Error message must follow the pattern \"{{selector}}\" (e.g., {{example}}). Found: {{actual}}."
18
+ },
19
+ schema: [{
20
+ type: "object",
21
+ properties: {
22
+ selector: {
23
+ description: "An ESQuery string to match the required pattern",
24
+ type: "string"
25
+ },
26
+ example: {
27
+ description: "Example code to help the user understand the required pattern",
28
+ type: "string"
29
+ }
30
+ },
31
+ additionalProperties: false
32
+ }]
33
+ },
34
+ defaultOptions: [{
35
+ selector: "Literal,TemplateLiteral",
36
+ example: "'error message'"
37
+ }],
38
+ create(context, [{ selector, example }]) {
39
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
40
+ /**
41
+ * Parsing `selector` to ensure it is valid,
42
+ * if not report an error and return empty rule listener
43
+ */
44
+ try {
45
+ esquery.parse(selector);
46
+ } catch {
47
+ context.report({
48
+ loc: {
49
+ line: 1,
50
+ column: 0
51
+ },
52
+ messageId: "invalidSelector",
53
+ data: { selector }
54
+ });
55
+ return {};
56
+ }
57
+ return {
58
+ ImportDeclaration: importDeclarationListener,
59
+ CallExpression(node) {
60
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
61
+ if (!zodSchemaMeta) return;
62
+ if (zodSchemaMeta.schemaType !== "custom" && !collectZodChainMethods(node).some((it) => it.name === "refine")) return;
63
+ if (node.arguments.length < 2) return;
64
+ let errorMessageNode;
65
+ const [, params] = node.arguments;
66
+ switch (params.type) {
67
+ case AST_NODE_TYPES.Literal:
68
+ case AST_NODE_TYPES.TemplateLiteral:
69
+ errorMessageNode = params;
70
+ break;
71
+ case AST_NODE_TYPES.ObjectExpression:
72
+ for (const property of params.properties) if (property.type === AST_NODE_TYPES.Property && property.key.type === AST_NODE_TYPES.Identifier && property.key.name === "error") {
73
+ errorMessageNode = property.value;
74
+ break;
75
+ }
76
+ break;
77
+ }
78
+ if (!errorMessageNode) return;
79
+ if (esquery.matches(errorMessageNode, esquery.parse(selector), errorMessageNode)) return;
80
+ context.report({
81
+ node,
82
+ messageId: "invalidStyle",
83
+ data: {
84
+ selector,
85
+ example,
86
+ actual: context.sourceCode.getText(errorMessageNode)
87
+ }
88
+ });
89
+ }
90
+ };
91
+ }
92
+ });
93
+ //#endregion
94
+ export { schemaErrorPropertyStyle };