eslint-plugin-zod 4.0.1 β†’ 4.1.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
@@ -29,37 +29,39 @@ Find out more about [Oxlint's `jsPLugins`](https://oxc.rs/docs/guide/usage/linte
29
29
  πŸ’Ό Configurations enabled in.\
30
30
  βœ… Set in the `recommended` configuration.\
31
31
  πŸ”§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
32
- πŸ’‘ Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
33
-
34
- | NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ |
35
- | :----------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------- | :-- | :-- | :-- |
36
- | [array-style](docs/rules/array-style.md) | Enforce consistent Zod array style | βœ… | πŸ”§ | |
37
- | [consistent-import](docs/rules/consistent-import.md) | Enforce a consistent import style for Zod | βœ… | πŸ”§ | |
38
- | [consistent-import-source](docs/rules/consistent-import-source.md) | Enforce consistent source from Zod imports | | | πŸ’‘ |
39
- | [consistent-object-schema-type](docs/rules/consistent-object-schema-type.md) | Enforce consistent usage of Zod schema methods | | | πŸ’‘ |
40
- | [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 | | πŸ”§ | |
41
- | [consistent-schema-var-name](docs/rules/consistent-schema-var-name.md) | Enforce a consistent naming convention for Zod schema variables | βœ… | | |
42
- | [no-any-schema](docs/rules/no-any-schema.md) | Disallow usage of `z.any()` in Zod schemas | βœ… | | πŸ’‘ |
43
- | [no-empty-custom-schema](docs/rules/no-empty-custom-schema.md) | Disallow usage of `z.custom()` without arguments | βœ… | | |
44
- | [no-number-schema-with-finite](docs/rules/no-number-schema-with-finite.md) | Disallow deprecated `z.number().finite()`. In Zod 4+ number schemas do not allow infinite values by default, so it is a no-op. | βœ… | πŸ”§ | |
45
- | [no-number-schema-with-int](docs/rules/no-number-schema-with-int.md) | Disallow usage of `z.number().int()` as it is considered legacy | βœ… | πŸ”§ | |
46
- | [no-number-schema-with-is-finite](docs/rules/no-number-schema-with-is-finite.md) | Disallow using deprecated `isFinite` on a Zod number schema; in v4+ it is always `true`. | βœ… | | |
47
- | [no-number-schema-with-is-int](docs/rules/no-number-schema-with-is-int.md) | Disallow using deprecated `isInt` on a Zod number schema; check the `format` property instead. | βœ… | | |
48
- | [no-number-schema-with-safe](docs/rules/no-number-schema-with-safe.md) | Disallow deprecated `z.number().safe()`. Use `z.int()`; `.safe()` is now identical to `.int()`. | βœ… | πŸ”§ | |
49
- | [no-number-schema-with-step](docs/rules/no-number-schema-with-step.md) | Disallow deprecated `z.number().step()`. Use `.multipleOf()` instead. | βœ… | πŸ”§ | |
50
- | [no-optional-and-default-together](docs/rules/no-optional-and-default-together.md) | Disallow using both `.optional()` and `.default()` on the same Zod schema | βœ… | πŸ”§ | |
51
- | [no-string-schema-with-uuid](docs/rules/no-string-schema-with-uuid.md) | Disallow usage of `z.string().uuid()` in favor of the dedicated `z.uuid()` schema | βœ… | πŸ”§ | |
52
- | [no-throw-in-refine](docs/rules/no-throw-in-refine.md) | Disallow throwing errors directly inside Zod refine callbacks | βœ… | | |
53
- | [no-transform-in-record-key](docs/rules/no-transform-in-record-key.md) | Disallow transforms in z.record() key schemas, which can cause silent key mutations and data loss through key collisions | | | |
54
- | [no-unknown-schema](docs/rules/no-unknown-schema.md) | Disallow usage of `z.unknown()` in Zod schemas | | | |
55
- | [prefer-enum-over-literal-union](docs/rules/prefer-enum-over-literal-union.md) | Prefer `z.enum()` over `z.union()` when all members are string literals. | βœ… | πŸ”§ | |
56
- | [prefer-meta](docs/rules/prefer-meta.md) | Enforce usage of `.meta()` over `.describe()` | βœ… | πŸ”§ | |
57
- | [prefer-meta-last](docs/rules/prefer-meta-last.md) | Enforce `.meta()` as last method | βœ… | πŸ”§ | |
58
- | [prefer-string-schema-with-trim](docs/rules/prefer-string-schema-with-trim.md) | Enforce `z.string().trim()` to prevent accidental leading/trailing whitespace | βœ… | πŸ”§ | |
59
- | [prefer-trim-before-string-length-checks](docs/rules/prefer-trim-before-string-length-checks.md) | Enforce `.trim()` is called before string length checks to ensure accurate validation | βœ… | πŸ”§ | |
60
- | [require-brand-type-parameter](docs/rules/require-brand-type-parameter.md) | Require type parameter on `.brand()` functions | βœ… | | πŸ’‘ |
61
- | [require-error-message](docs/rules/require-error-message.md) | Enforce that custom refinements include an error message | βœ… | πŸ”§ | |
62
- | [schema-error-property-style](docs/rules/schema-error-property-style.md) | Enforce consistent style for error messages in Zod schema validation (using ESQuery patterns) | | | |
32
+ πŸ’‘ Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).\
33
+ ❌ Deprecated.
34
+
35
+ | NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | ❌ |
36
+ | :----------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------- | :-- | :-- | :-- | :-- |
37
+ | [array-style](docs/rules/array-style.md) | Enforce consistent Zod array style | βœ… | πŸ”§ | | |
38
+ | [consistent-import](docs/rules/consistent-import.md) | Enforce a consistent import style for Zod | βœ… | πŸ”§ | | |
39
+ | [consistent-import-source](docs/rules/consistent-import-source.md) | Enforce consistent source from Zod imports | | | πŸ’‘ | |
40
+ | [consistent-object-schema-type](docs/rules/consistent-object-schema-type.md) | Enforce consistent usage of Zod schema methods | | | πŸ’‘ | |
41
+ | [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 | | πŸ”§ | | |
42
+ | [consistent-schema-var-name](docs/rules/consistent-schema-var-name.md) | Enforce a consistent naming convention for Zod schema variables | βœ… | | | |
43
+ | [no-any-schema](docs/rules/no-any-schema.md) | Disallow usage of `z.any()` in Zod schemas | βœ… | | πŸ’‘ | |
44
+ | [no-empty-custom-schema](docs/rules/no-empty-custom-schema.md) | Disallow usage of `z.custom()` without arguments | βœ… | | | |
45
+ | [no-number-schema-with-finite](docs/rules/no-number-schema-with-finite.md) | Disallow deprecated `z.number().finite()`. In Zod 4+ number schemas do not allow infinite values by default, so it is a no-op. | βœ… | πŸ”§ | | |
46
+ | [no-number-schema-with-int](docs/rules/no-number-schema-with-int.md) | Disallow usage of `z.number().int()` as it is considered legacy | βœ… | πŸ”§ | | |
47
+ | [no-number-schema-with-is-finite](docs/rules/no-number-schema-with-is-finite.md) | Disallow using deprecated `isFinite` on a Zod number schema; in v4+ it is always `true`. | βœ… | | | |
48
+ | [no-number-schema-with-is-int](docs/rules/no-number-schema-with-is-int.md) | Disallow using deprecated `isInt` on a Zod number schema; check the `format` property instead. | βœ… | | | |
49
+ | [no-number-schema-with-safe](docs/rules/no-number-schema-with-safe.md) | Disallow deprecated `z.number().safe()`. Use `z.int()`; `.safe()` is now identical to `.int()`. | βœ… | πŸ”§ | | |
50
+ | [no-number-schema-with-step](docs/rules/no-number-schema-with-step.md) | Disallow deprecated `z.number().step()`. Use `.multipleOf()` instead. | βœ… | πŸ”§ | | |
51
+ | [no-optional-and-default-together](docs/rules/no-optional-and-default-together.md) | Disallow using both `.optional()` and `.default()` on the same Zod schema | βœ… | πŸ”§ | | |
52
+ | [no-string-schema-with-uuid](docs/rules/no-string-schema-with-uuid.md) | Disallow usage of `z.string().uuid()` in favor of the dedicated `z.uuid()` schema | βœ… | πŸ”§ | | ❌ |
53
+ | [no-throw-in-refine](docs/rules/no-throw-in-refine.md) | Disallow throwing errors directly inside Zod refine callbacks | βœ… | | | |
54
+ | [no-transform-in-record-key](docs/rules/no-transform-in-record-key.md) | Disallow transforms in z.record() key schemas, which can cause silent key mutations and data loss through key collisions | | | | |
55
+ | [no-unknown-schema](docs/rules/no-unknown-schema.md) | Disallow usage of `z.unknown()` in Zod schemas | | | | |
56
+ | [prefer-enum-over-literal-union](docs/rules/prefer-enum-over-literal-union.md) | Prefer `z.enum()` over `z.union()` when all members are string literals. | βœ… | πŸ”§ | | |
57
+ | [prefer-meta](docs/rules/prefer-meta.md) | Enforce usage of `.meta()` over `.describe()` | βœ… | πŸ”§ | | |
58
+ | [prefer-meta-last](docs/rules/prefer-meta-last.md) | Enforce `.meta()` as last method | βœ… | πŸ”§ | | |
59
+ | [prefer-string-schema-with-trim](docs/rules/prefer-string-schema-with-trim.md) | Enforce `z.string().trim()` to prevent accidental leading/trailing whitespace | βœ… | πŸ”§ | | |
60
+ | [prefer-top-level-string-formats](docs/rules/prefer-top-level-string-formats.md) | Prefer top-level string format schemas over deprecated `z.string().<format>()` methods | βœ… | πŸ”§ | | |
61
+ | [prefer-trim-before-string-length-checks](docs/rules/prefer-trim-before-string-length-checks.md) | Enforce `.trim()` is called before string length checks to ensure accurate validation | βœ… | πŸ”§ | | |
62
+ | [require-brand-type-parameter](docs/rules/require-brand-type-parameter.md) | Require type parameter on `.brand()` functions | βœ… | | πŸ’‘ | |
63
+ | [require-error-message](docs/rules/require-error-message.md) | Enforce that custom refinements include an error message | βœ… | πŸ”§ | | |
64
+ | [schema-error-property-style](docs/rules/schema-error-property-style.md) | Enforce consistent style for error messages in Zod schema validation (using ESQuery patterns) | | | | |
63
65
 
64
66
  <!-- end auto-generated rules list -->
65
67
 
package/dist/index.cjs CHANGED
@@ -22,6 +22,7 @@ const require_prefer_enum_over_literal_union = require("./rules/prefer-enum-over
22
22
  const require_prefer_meta_last = require("./rules/prefer-meta-last.cjs");
23
23
  const require_prefer_meta = require("./rules/prefer-meta.cjs");
24
24
  const require_prefer_string_schema_with_trim = require("./rules/prefer-string-schema-with-trim.cjs");
25
+ const require_prefer_top_level_string_formats = require("./rules/prefer-top-level-string-formats.cjs");
25
26
  const require_prefer_trim_before_string_length_checks = require("./rules/prefer-trim-before-string-length-checks.cjs");
26
27
  const require_require_brand_type_parameter = require("./rules/require-brand-type-parameter.cjs");
27
28
  const require_require_error_message = require("./rules/require-error-message.cjs");
@@ -55,6 +56,7 @@ const eslintPluginZod = {
55
56
  "prefer-enum-over-literal-union": require_prefer_enum_over_literal_union.preferEnumOverLiteralUnion,
56
57
  "prefer-meta": require_prefer_meta.preferMeta,
57
58
  "prefer-meta-last": require_prefer_meta_last.preferMetaLast,
59
+ "prefer-top-level-string-formats": require_prefer_top_level_string_formats.preferTopLevelStringFormats,
58
60
  "prefer-string-schema-with-trim": require_prefer_string_schema_with_trim.preferStringSchemaWithTrim,
59
61
  "prefer-trim-before-string-length-checks": require_prefer_trim_before_string_length_checks.preferTrimBeforeStringLengthChecks,
60
62
  "require-brand-type-parameter": require_require_brand_type_parameter.requireBrandTypeParameter,
@@ -84,6 +86,7 @@ const recommendedConfig = {
84
86
  "zod/prefer-enum-over-literal-union": "error",
85
87
  "zod/prefer-meta": "error",
86
88
  "zod/prefer-meta-last": "error",
89
+ "zod/prefer-top-level-string-formats": "error",
87
90
  "zod/prefer-string-schema-with-trim": "error",
88
91
  "zod/prefer-trim-before-string-length-checks": "error",
89
92
  "zod/require-brand-type-parameter": "error",
package/dist/index.mjs CHANGED
@@ -22,6 +22,7 @@ import { preferEnumOverLiteralUnion } from "./rules/prefer-enum-over-literal-uni
22
22
  import { preferMetaLast } from "./rules/prefer-meta-last.mjs";
23
23
  import { preferMeta } from "./rules/prefer-meta.mjs";
24
24
  import { preferStringSchemaWithTrim } from "./rules/prefer-string-schema-with-trim.mjs";
25
+ import { preferTopLevelStringFormats } from "./rules/prefer-top-level-string-formats.mjs";
25
26
  import { preferTrimBeforeStringLengthChecks } from "./rules/prefer-trim-before-string-length-checks.mjs";
26
27
  import { requireBrandTypeParameter } from "./rules/require-brand-type-parameter.mjs";
27
28
  import { requireErrorMessage } from "./rules/require-error-message.mjs";
@@ -55,6 +56,7 @@ const eslintPluginZod = {
55
56
  "prefer-enum-over-literal-union": preferEnumOverLiteralUnion,
56
57
  "prefer-meta": preferMeta,
57
58
  "prefer-meta-last": preferMetaLast,
59
+ "prefer-top-level-string-formats": preferTopLevelStringFormats,
58
60
  "prefer-string-schema-with-trim": preferStringSchemaWithTrim,
59
61
  "prefer-trim-before-string-length-checks": preferTrimBeforeStringLengthChecks,
60
62
  "require-brand-type-parameter": requireBrandTypeParameter,
@@ -84,6 +86,7 @@ const recommendedConfig = {
84
86
  "zod/prefer-enum-over-literal-union": "error",
85
87
  "zod/prefer-meta": "error",
86
88
  "zod/prefer-meta-last": "error",
89
+ "zod/prefer-top-level-string-formats": "error",
87
90
  "zod/prefer-string-schema-with-trim": "error",
88
91
  "zod/prefer-trim-before-string-length-checks": "error",
89
92
  "zod/require-brand-type-parameter": "error",
@@ -8,6 +8,7 @@ const noStringSchemaWithUuid = require_create_plugin_rule.createZodPluginRule({
8
8
  meta: {
9
9
  fixable: "code",
10
10
  type: "problem",
11
+ deprecated: { message: "Use `zod/prefer-top-level-string-formats` instead" },
11
12
  docs: {
12
13
  description: "Disallow usage of `z.string().uuid()` in favor of the dedicated `z.uuid()` schema",
13
14
  url: "https://zod.dev/api#uuids"
@@ -7,6 +7,7 @@ const noStringSchemaWithUuid = createZodPluginRule({
7
7
  meta: {
8
8
  fixable: "code",
9
9
  type: "problem",
10
+ deprecated: { message: "Use `zod/prefer-top-level-string-formats` instead" },
10
11
  docs: {
11
12
  description: "Disallow usage of `z.string().uuid()` in favor of the dedicated `z.uuid()` schema",
12
13
  url: "https://zod.dev/api#uuids"
@@ -0,0 +1,193 @@
1
+ require("../_virtual/_rolldown/runtime.cjs");
2
+ const require_create_plugin_rule = require("../utils/create-plugin-rule.cjs");
3
+ let _eslint_zod_utils = require("@eslint-zod/utils");
4
+ //#region src/rules/prefer-top-level-string-formats.ts
5
+ const TOP_LEVEL_STRING_FORMATS_URL = "https://zod.dev/v4?id=top-level-string-formats";
6
+ const TOP_LEVEL_STRING_FORMATS = [
7
+ {
8
+ sourceMethodName: "base64",
9
+ replacementMethodName: "base64"
10
+ },
11
+ {
12
+ sourceMethodName: "base64url",
13
+ replacementMethodName: "base64url"
14
+ },
15
+ {
16
+ sourceMethodName: "cidrv4",
17
+ replacementMethodName: "cidrv4"
18
+ },
19
+ {
20
+ sourceMethodName: "cidrv6",
21
+ replacementMethodName: "cidrv6"
22
+ },
23
+ {
24
+ sourceMethodName: "cuid",
25
+ replacementMethodName: "cuid"
26
+ },
27
+ {
28
+ sourceMethodName: "cuid2",
29
+ replacementMethodName: "cuid2"
30
+ },
31
+ {
32
+ sourceMethodName: "date",
33
+ replacementMethodName: "iso.date"
34
+ },
35
+ {
36
+ sourceMethodName: "datetime",
37
+ replacementMethodName: "iso.datetime"
38
+ },
39
+ {
40
+ sourceMethodName: "duration",
41
+ replacementMethodName: "iso.duration"
42
+ },
43
+ {
44
+ sourceMethodName: "e164",
45
+ replacementMethodName: "e164"
46
+ },
47
+ {
48
+ sourceMethodName: "email",
49
+ replacementMethodName: "email"
50
+ },
51
+ {
52
+ sourceMethodName: "emoji",
53
+ replacementMethodName: "emoji"
54
+ },
55
+ {
56
+ sourceMethodName: "guid",
57
+ replacementMethodName: "guid"
58
+ },
59
+ {
60
+ sourceMethodName: "ipv4",
61
+ replacementMethodName: "ipv4"
62
+ },
63
+ {
64
+ sourceMethodName: "ipv6",
65
+ replacementMethodName: "ipv6"
66
+ },
67
+ {
68
+ sourceMethodName: "jwt",
69
+ replacementMethodName: "jwt"
70
+ },
71
+ {
72
+ sourceMethodName: "ksuid",
73
+ replacementMethodName: "ksuid"
74
+ },
75
+ {
76
+ sourceMethodName: "nanoid",
77
+ replacementMethodName: "nanoid"
78
+ },
79
+ {
80
+ sourceMethodName: "time",
81
+ replacementMethodName: "iso.time"
82
+ },
83
+ {
84
+ sourceMethodName: "ulid",
85
+ replacementMethodName: "ulid"
86
+ },
87
+ {
88
+ sourceMethodName: "url",
89
+ replacementMethodName: "url"
90
+ },
91
+ {
92
+ sourceMethodName: "uuid",
93
+ replacementMethodName: "uuid"
94
+ },
95
+ {
96
+ sourceMethodName: "uuidv4",
97
+ replacementMethodName: "uuidv4"
98
+ },
99
+ {
100
+ sourceMethodName: "uuidv6",
101
+ replacementMethodName: "uuidv6"
102
+ },
103
+ {
104
+ sourceMethodName: "uuidv7",
105
+ replacementMethodName: "uuidv7"
106
+ },
107
+ {
108
+ sourceMethodName: "xid",
109
+ replacementMethodName: "xid"
110
+ }
111
+ ];
112
+ const { trackZodSchemaImports } = (0, _eslint_zod_utils.createZodSchemaImportTrack)(_eslint_zod_utils.zodImportScope);
113
+ const TOP_LEVEL_STRING_FORMAT_METHOD_NAMES = TOP_LEVEL_STRING_FORMATS.map(({ sourceMethodName }) => sourceMethodName);
114
+ const TOP_LEVEL_STRING_FORMATS_BY_SOURCE = Object.fromEntries(TOP_LEVEL_STRING_FORMATS.map((format) => [format.sourceMethodName, format]));
115
+ function isTopLevelStringFormatMethodName(value) {
116
+ return TOP_LEVEL_STRING_FORMAT_METHOD_NAMES.includes(value);
117
+ }
118
+ const preferTopLevelStringFormats = require_create_plugin_rule.createZodPluginRule({
119
+ name: "prefer-top-level-string-formats",
120
+ meta: {
121
+ type: "suggestion",
122
+ fixable: "code",
123
+ docs: {
124
+ description: "Prefer top-level string format schemas over deprecated `z.string().<format>()` methods",
125
+ url: TOP_LEVEL_STRING_FORMATS_URL
126
+ },
127
+ messages: { preferTopLevelStringFormat: "Use `z.{{replacementMethod}}()` instead of `z.string().{{sourceMethod}}()`." },
128
+ schema: [{
129
+ type: "object",
130
+ properties: { ignore: {
131
+ type: "array",
132
+ description: "Top-level string format methods to ignore for this rule.",
133
+ items: {
134
+ type: "string",
135
+ enum: [...TOP_LEVEL_STRING_FORMAT_METHOD_NAMES]
136
+ },
137
+ uniqueItems: true
138
+ } },
139
+ additionalProperties: false
140
+ }]
141
+ },
142
+ defaultOptions: [{}],
143
+ create(context, [{ ignore = [] }]) {
144
+ const { sourceCode } = context;
145
+ const ignoredMethods = new Set(ignore);
146
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
147
+ return {
148
+ ImportDeclaration: importDeclarationListener,
149
+ CallExpression(node) {
150
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
151
+ if (zodSchemaMeta?.schemaType !== "string") return;
152
+ const methods = collectZodChainMethods(node);
153
+ const stringIndex = methods.findIndex((method) => method.name === "string");
154
+ if (stringIndex === -1) return;
155
+ const formatMethod = methods.find((method, index) => index > stringIndex && isTopLevelStringFormatMethodName(method.name) && !ignoredMethods.has(method.name));
156
+ if (!formatMethod) return;
157
+ if (!isTopLevelStringFormatMethodName(formatMethod.name)) return;
158
+ const { replacementMethodName, sourceMethodName } = TOP_LEVEL_STRING_FORMATS_BY_SOURCE[formatMethod.name];
159
+ if (zodSchemaMeta.schemaDecl === "named") {
160
+ context.report({
161
+ node,
162
+ messageId: "preferTopLevelStringFormat",
163
+ data: {
164
+ replacementMethod: replacementMethodName,
165
+ sourceMethod: sourceMethodName
166
+ }
167
+ });
168
+ return;
169
+ }
170
+ context.report({
171
+ node,
172
+ messageId: "preferTopLevelStringFormat",
173
+ data: {
174
+ replacementMethod: replacementMethodName,
175
+ sourceMethod: sourceMethodName
176
+ },
177
+ fix(fixer) {
178
+ return (0, _eslint_zod_utils.buildZodChainReplacementFix)({
179
+ sourceCode,
180
+ fixer,
181
+ methods,
182
+ fromIndex: stringIndex,
183
+ toIndex: methods.indexOf(formatMethod),
184
+ toMethodName: replacementMethodName
185
+ });
186
+ }
187
+ });
188
+ }
189
+ };
190
+ }
191
+ });
192
+ //#endregion
193
+ exports.preferTopLevelStringFormats = preferTopLevelStringFormats;
@@ -0,0 +1,192 @@
1
+ import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
2
+ import { buildZodChainReplacementFix, createZodSchemaImportTrack, zodImportScope } from "@eslint-zod/utils";
3
+ //#region src/rules/prefer-top-level-string-formats.ts
4
+ const TOP_LEVEL_STRING_FORMATS_URL = "https://zod.dev/v4?id=top-level-string-formats";
5
+ const TOP_LEVEL_STRING_FORMATS = [
6
+ {
7
+ sourceMethodName: "base64",
8
+ replacementMethodName: "base64"
9
+ },
10
+ {
11
+ sourceMethodName: "base64url",
12
+ replacementMethodName: "base64url"
13
+ },
14
+ {
15
+ sourceMethodName: "cidrv4",
16
+ replacementMethodName: "cidrv4"
17
+ },
18
+ {
19
+ sourceMethodName: "cidrv6",
20
+ replacementMethodName: "cidrv6"
21
+ },
22
+ {
23
+ sourceMethodName: "cuid",
24
+ replacementMethodName: "cuid"
25
+ },
26
+ {
27
+ sourceMethodName: "cuid2",
28
+ replacementMethodName: "cuid2"
29
+ },
30
+ {
31
+ sourceMethodName: "date",
32
+ replacementMethodName: "iso.date"
33
+ },
34
+ {
35
+ sourceMethodName: "datetime",
36
+ replacementMethodName: "iso.datetime"
37
+ },
38
+ {
39
+ sourceMethodName: "duration",
40
+ replacementMethodName: "iso.duration"
41
+ },
42
+ {
43
+ sourceMethodName: "e164",
44
+ replacementMethodName: "e164"
45
+ },
46
+ {
47
+ sourceMethodName: "email",
48
+ replacementMethodName: "email"
49
+ },
50
+ {
51
+ sourceMethodName: "emoji",
52
+ replacementMethodName: "emoji"
53
+ },
54
+ {
55
+ sourceMethodName: "guid",
56
+ replacementMethodName: "guid"
57
+ },
58
+ {
59
+ sourceMethodName: "ipv4",
60
+ replacementMethodName: "ipv4"
61
+ },
62
+ {
63
+ sourceMethodName: "ipv6",
64
+ replacementMethodName: "ipv6"
65
+ },
66
+ {
67
+ sourceMethodName: "jwt",
68
+ replacementMethodName: "jwt"
69
+ },
70
+ {
71
+ sourceMethodName: "ksuid",
72
+ replacementMethodName: "ksuid"
73
+ },
74
+ {
75
+ sourceMethodName: "nanoid",
76
+ replacementMethodName: "nanoid"
77
+ },
78
+ {
79
+ sourceMethodName: "time",
80
+ replacementMethodName: "iso.time"
81
+ },
82
+ {
83
+ sourceMethodName: "ulid",
84
+ replacementMethodName: "ulid"
85
+ },
86
+ {
87
+ sourceMethodName: "url",
88
+ replacementMethodName: "url"
89
+ },
90
+ {
91
+ sourceMethodName: "uuid",
92
+ replacementMethodName: "uuid"
93
+ },
94
+ {
95
+ sourceMethodName: "uuidv4",
96
+ replacementMethodName: "uuidv4"
97
+ },
98
+ {
99
+ sourceMethodName: "uuidv6",
100
+ replacementMethodName: "uuidv6"
101
+ },
102
+ {
103
+ sourceMethodName: "uuidv7",
104
+ replacementMethodName: "uuidv7"
105
+ },
106
+ {
107
+ sourceMethodName: "xid",
108
+ replacementMethodName: "xid"
109
+ }
110
+ ];
111
+ const { trackZodSchemaImports } = createZodSchemaImportTrack(zodImportScope);
112
+ const TOP_LEVEL_STRING_FORMAT_METHOD_NAMES = TOP_LEVEL_STRING_FORMATS.map(({ sourceMethodName }) => sourceMethodName);
113
+ const TOP_LEVEL_STRING_FORMATS_BY_SOURCE = Object.fromEntries(TOP_LEVEL_STRING_FORMATS.map((format) => [format.sourceMethodName, format]));
114
+ function isTopLevelStringFormatMethodName(value) {
115
+ return TOP_LEVEL_STRING_FORMAT_METHOD_NAMES.includes(value);
116
+ }
117
+ const preferTopLevelStringFormats = createZodPluginRule({
118
+ name: "prefer-top-level-string-formats",
119
+ meta: {
120
+ type: "suggestion",
121
+ fixable: "code",
122
+ docs: {
123
+ description: "Prefer top-level string format schemas over deprecated `z.string().<format>()` methods",
124
+ url: TOP_LEVEL_STRING_FORMATS_URL
125
+ },
126
+ messages: { preferTopLevelStringFormat: "Use `z.{{replacementMethod}}()` instead of `z.string().{{sourceMethod}}()`." },
127
+ schema: [{
128
+ type: "object",
129
+ properties: { ignore: {
130
+ type: "array",
131
+ description: "Top-level string format methods to ignore for this rule.",
132
+ items: {
133
+ type: "string",
134
+ enum: [...TOP_LEVEL_STRING_FORMAT_METHOD_NAMES]
135
+ },
136
+ uniqueItems: true
137
+ } },
138
+ additionalProperties: false
139
+ }]
140
+ },
141
+ defaultOptions: [{}],
142
+ create(context, [{ ignore = [] }]) {
143
+ const { sourceCode } = context;
144
+ const ignoredMethods = new Set(ignore);
145
+ const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods } = trackZodSchemaImports();
146
+ return {
147
+ ImportDeclaration: importDeclarationListener,
148
+ CallExpression(node) {
149
+ const zodSchemaMeta = detectZodSchemaRootNode(node);
150
+ if (zodSchemaMeta?.schemaType !== "string") return;
151
+ const methods = collectZodChainMethods(node);
152
+ const stringIndex = methods.findIndex((method) => method.name === "string");
153
+ if (stringIndex === -1) return;
154
+ const formatMethod = methods.find((method, index) => index > stringIndex && isTopLevelStringFormatMethodName(method.name) && !ignoredMethods.has(method.name));
155
+ if (!formatMethod) return;
156
+ if (!isTopLevelStringFormatMethodName(formatMethod.name)) return;
157
+ const { replacementMethodName, sourceMethodName } = TOP_LEVEL_STRING_FORMATS_BY_SOURCE[formatMethod.name];
158
+ if (zodSchemaMeta.schemaDecl === "named") {
159
+ context.report({
160
+ node,
161
+ messageId: "preferTopLevelStringFormat",
162
+ data: {
163
+ replacementMethod: replacementMethodName,
164
+ sourceMethod: sourceMethodName
165
+ }
166
+ });
167
+ return;
168
+ }
169
+ context.report({
170
+ node,
171
+ messageId: "preferTopLevelStringFormat",
172
+ data: {
173
+ replacementMethod: replacementMethodName,
174
+ sourceMethod: sourceMethodName
175
+ },
176
+ fix(fixer) {
177
+ return buildZodChainReplacementFix({
178
+ sourceCode,
179
+ fixer,
180
+ methods,
181
+ fromIndex: stringIndex,
182
+ toIndex: methods.indexOf(formatMethod),
183
+ toMethodName: replacementMethodName
184
+ });
185
+ }
186
+ });
187
+ }
188
+ };
189
+ }
190
+ });
191
+ //#endregion
192
+ export { preferTopLevelStringFormats };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-zod",
3
- "version": "4.0.1",
3
+ "version": "4.1.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": {