eslint-plugin-zod 3.5.2 → 3.5.4

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.
@@ -15,14 +15,16 @@ const noEmptyCustomSchema = require_create_plugin_rule.createZodPluginRule({
15
15
  },
16
16
  defaultOptions: [],
17
17
  create(context) {
18
- const { importDeclarationListener, detectZodSchemaRootNode } = trackZodSchemaImports();
18
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
19
19
  return {
20
20
  ImportDeclaration: importDeclarationListener,
21
21
  CallExpression(node) {
22
22
  const zodSchemaMeta = detectZodSchemaRootNode(node);
23
23
  if (!zodSchemaMeta) return;
24
- if (zodSchemaMeta.schemaType === "custom" && node.arguments.length === 0) context.report({
25
- node,
24
+ if (zodSchemaMeta.schemaType !== "custom") return;
25
+ const customCallNode = collectZodChainMethods(node).find((method) => method.name === "custom")?.node;
26
+ if (customCallNode?.arguments.length === 0) context.report({
27
+ node: customCallNode,
26
28
  messageId: "noEmptyCustomSchema"
27
29
  });
28
30
  }
@@ -16,14 +16,16 @@ const noEmptyCustomSchema = createZodPluginRule({
16
16
  },
17
17
  defaultOptions: [],
18
18
  create(context) {
19
- const { importDeclarationListener, detectZodSchemaRootNode } = trackZodSchemaImports();
19
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
20
20
  return {
21
21
  ImportDeclaration: importDeclarationListener,
22
22
  CallExpression(node) {
23
23
  const zodSchemaMeta = detectZodSchemaRootNode(node);
24
24
  if (!zodSchemaMeta) return;
25
- if (zodSchemaMeta.schemaType === "custom" && node.arguments.length === 0) context.report({
26
- node,
25
+ if (zodSchemaMeta.schemaType !== "custom") return;
26
+ const customCallNode = collectZodChainMethods(node).find((method) => method.name === "custom")?.node;
27
+ if (customCallNode?.arguments.length === 0) context.report({
28
+ node: customCallNode,
27
29
  messageId: "noEmptyCustomSchema"
28
30
  });
29
31
  }
@@ -1,6 +1,8 @@
1
1
  const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
2
+ const require_track_zod_schema_imports = require("../utils/track-zod-schema-imports.cjs");
3
+ const require_find_parent_schema_matching_condition = require("../utils/find-parent-schema-matching-condition.cjs");
2
4
  //#region src/rules/prefer-string-schema-with-trim.ts
3
- const { zodImportAllowedSource, trackZodSchemaImports } = require("../utils/track-zod-schema-imports.cjs").createZodSchemaImportTrack("zod");
5
+ const { zodImportAllowedSource, trackZodSchemaImports } = require_track_zod_schema_imports.createZodSchemaImportTrack("zod");
4
6
  const preferStringSchemaWithTrim = require_create_plugin_rule.createZodPluginRule({
5
7
  name: "prefer-string-schema-with-trim",
6
8
  meta: {
@@ -21,6 +23,10 @@ const preferStringSchemaWithTrim = require_create_plugin_rule.createZodPluginRul
21
23
  CallExpression(node) {
22
24
  const zodSchemaMeta = detectZodSchemaRootNode(node);
23
25
  if (zodSchemaMeta?.schemaType !== "string") return;
26
+ if (require_find_parent_schema_matching_condition.findParentSchemaMatchingCondition(zodSchemaMeta.node, {
27
+ schemaName: "record",
28
+ condition: (callParent) => callParent.arguments.length > 0 && callParent.arguments[0] === zodSchemaMeta.node
29
+ })) return;
24
30
  const methods = collectZodChainMethods(zodSchemaMeta.node);
25
31
  if (methods.some((it) => it.name === "trim")) return;
26
32
  if (zodSchemaMeta.schemaDecl === "named") {
@@ -1,5 +1,6 @@
1
1
  import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
2
  import { createZodSchemaImportTrack } from "../utils/track-zod-schema-imports.mjs";
3
+ import { findParentSchemaMatchingCondition } from "../utils/find-parent-schema-matching-condition.mjs";
3
4
  //#region src/rules/prefer-string-schema-with-trim.ts
4
5
  const { zodImportAllowedSource, trackZodSchemaImports } = createZodSchemaImportTrack("zod");
5
6
  const preferStringSchemaWithTrim = createZodPluginRule({
@@ -22,6 +23,10 @@ const preferStringSchemaWithTrim = createZodPluginRule({
22
23
  CallExpression(node) {
23
24
  const zodSchemaMeta = detectZodSchemaRootNode(node);
24
25
  if (zodSchemaMeta?.schemaType !== "string") return;
26
+ if (findParentSchemaMatchingCondition(zodSchemaMeta.node, {
27
+ schemaName: "record",
28
+ condition: (callParent) => callParent.arguments.length > 0 && callParent.arguments[0] === zodSchemaMeta.node
29
+ })) return;
25
30
  const methods = collectZodChainMethods(zodSchemaMeta.node);
26
31
  if (methods.some((it) => it.name === "trim")) return;
27
32
  if (zodSchemaMeta.schemaDecl === "named") {
@@ -27,19 +27,20 @@ const requireErrorMessage = require_create_plugin_rule.createZodPluginRule({
27
27
  ImportDeclaration: importDeclarationListener,
28
28
  CallExpression(node) {
29
29
  if (!detectZodSchemaRootNode(node)) return;
30
- const refine = collectZodChainMethods(node).find((it) => it.name === "refine" || it.name === "custom");
31
- if (!refine) return;
32
- const refineNode = refine.node;
33
- if (refineNode.arguments.length < 2) {
34
- context.report({
35
- messageId: "requireErrorMessage",
36
- node: node.callee
37
- });
38
- return;
39
- }
40
- const [, params] = refineNode.arguments;
41
- if (params.type === _typescript_eslint_utils.AST_NODE_TYPES.Literal) return;
42
- if (params.type === _typescript_eslint_utils.AST_NODE_TYPES.ObjectExpression) {
30
+ const refines = collectZodChainMethods(node).filter((it) => it.name === "refine" || it.name === "custom");
31
+ if (refines.length === 0) return;
32
+ for (const refine of refines) {
33
+ const refineNode = refine.node;
34
+ if (refineNode.arguments.length < 2) {
35
+ context.report({
36
+ messageId: "requireErrorMessage",
37
+ node: refineNode
38
+ });
39
+ continue;
40
+ }
41
+ const [, params] = refineNode.arguments;
42
+ if (params.type === _typescript_eslint_utils.AST_NODE_TYPES.Literal) continue;
43
+ if (params.type !== _typescript_eslint_utils.AST_NODE_TYPES.ObjectExpression) continue;
43
44
  let errorPropertyNode;
44
45
  let messagePropertyNode;
45
46
  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) {
@@ -59,7 +60,7 @@ const requireErrorMessage = require_create_plugin_rule.createZodPluginRule({
59
60
  return fixer.removeRange([messagePropertyNode.range[0], end]);
60
61
  }
61
62
  });
62
- return;
63
+ continue;
63
64
  }
64
65
  if (messagePropertyNode && !errorPropertyNode) {
65
66
  context.report({
@@ -69,7 +70,7 @@ const requireErrorMessage = require_create_plugin_rule.createZodPluginRule({
69
70
  return fixer.replaceTextRange(messagePropertyNode.key.range, "error");
70
71
  }
71
72
  });
72
- return;
73
+ continue;
73
74
  }
74
75
  if (!errorPropertyNode) context.report({
75
76
  messageId: "requireErrorMessage",
@@ -26,19 +26,20 @@ const requireErrorMessage = createZodPluginRule({
26
26
  ImportDeclaration: importDeclarationListener,
27
27
  CallExpression(node) {
28
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) {
29
+ const refines = collectZodChainMethods(node).filter((it) => it.name === "refine" || it.name === "custom");
30
+ if (refines.length === 0) return;
31
+ for (const refine of refines) {
32
+ const refineNode = refine.node;
33
+ if (refineNode.arguments.length < 2) {
34
+ context.report({
35
+ messageId: "requireErrorMessage",
36
+ node: refineNode
37
+ });
38
+ continue;
39
+ }
40
+ const [, params] = refineNode.arguments;
41
+ if (params.type === AST_NODE_TYPES.Literal) continue;
42
+ if (params.type !== AST_NODE_TYPES.ObjectExpression) continue;
42
43
  let errorPropertyNode;
43
44
  let messagePropertyNode;
44
45
  for (const property of params.properties) if (property.type === AST_NODE_TYPES.Property && property.key.type === AST_NODE_TYPES.Identifier) {
@@ -58,7 +59,7 @@ const requireErrorMessage = createZodPluginRule({
58
59
  return fixer.removeRange([messagePropertyNode.range[0], end]);
59
60
  }
60
61
  });
61
- return;
62
+ continue;
62
63
  }
63
64
  if (messagePropertyNode && !errorPropertyNode) {
64
65
  context.report({
@@ -68,7 +69,7 @@ const requireErrorMessage = createZodPluginRule({
68
69
  return fixer.replaceTextRange(messagePropertyNode.key.range, "error");
69
70
  }
70
71
  });
71
- return;
72
+ continue;
72
73
  }
73
74
  if (!errorPropertyNode) context.report({
74
75
  messageId: "requireErrorMessage",
@@ -0,0 +1,25 @@
1
+ require("../_virtual/_rolldown/runtime.cjs");
2
+ let _typescript_eslint_utils = require("@typescript-eslint/utils");
3
+ //#region src/utils/find-parent-schema-matching-condition.ts
4
+ function findParentSchemaMatchingCondition(outermostNode, options) {
5
+ const { schemaName, condition } = options;
6
+ let current = outermostNode;
7
+ while (current.parent) {
8
+ const { parent } = current;
9
+ if (parent.type === _typescript_eslint_utils.AST_NODE_TYPES.CallExpression) {
10
+ const callParent = parent;
11
+ if (callParent.callee.type === _typescript_eslint_utils.AST_NODE_TYPES.MemberExpression) {
12
+ const memberExpr = callParent.callee;
13
+ if ((memberExpr.property.type === _typescript_eslint_utils.AST_NODE_TYPES.Identifier ? memberExpr.property.name : null) === schemaName) return condition(callParent);
14
+ }
15
+ }
16
+ if (parent.type === _typescript_eslint_utils.AST_NODE_TYPES.MemberExpression) {
17
+ current = parent;
18
+ continue;
19
+ }
20
+ current = parent;
21
+ }
22
+ return false;
23
+ }
24
+ //#endregion
25
+ exports.findParentSchemaMatchingCondition = findParentSchemaMatchingCondition;
@@ -0,0 +1,24 @@
1
+ import { AST_NODE_TYPES } from "@typescript-eslint/utils";
2
+ //#region src/utils/find-parent-schema-matching-condition.ts
3
+ function findParentSchemaMatchingCondition(outermostNode, options) {
4
+ const { schemaName, condition } = options;
5
+ let current = outermostNode;
6
+ while (current.parent) {
7
+ const { parent } = current;
8
+ if (parent.type === AST_NODE_TYPES.CallExpression) {
9
+ const callParent = parent;
10
+ if (callParent.callee.type === AST_NODE_TYPES.MemberExpression) {
11
+ const memberExpr = callParent.callee;
12
+ if ((memberExpr.property.type === AST_NODE_TYPES.Identifier ? memberExpr.property.name : null) === schemaName) return condition(callParent);
13
+ }
14
+ }
15
+ if (parent.type === AST_NODE_TYPES.MemberExpression) {
16
+ current = parent;
17
+ continue;
18
+ }
19
+ current = parent;
20
+ }
21
+ return false;
22
+ }
23
+ //#endregion
24
+ export { findParentSchemaMatchingCondition };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-zod",
3
- "version": "3.5.2",
3
+ "version": "3.5.4",
4
4
  "type": "module",
5
5
  "description": "ESLint plugin that adds custom linting rules to enforce best practices when using Zod",
6
6
  "engines": {