eslint-plugin-zod 4.0.1 β 4.2.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 +34 -31
- package/dist/index.cjs +6 -0
- package/dist/index.mjs +6 -0
- package/dist/rules/no-native-enum.cjs +46 -0
- package/dist/rules/no-native-enum.mjs +45 -0
- package/dist/rules/no-string-schema-with-uuid.cjs +1 -0
- package/dist/rules/no-string-schema-with-uuid.mjs +1 -0
- package/dist/rules/prefer-top-level-string-formats.cjs +193 -0
- package/dist/rules/prefer-top-level-string-formats.mjs +192 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,37 +29,40 @@ 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
|
-
|
|
35
|
-
|
|
|
36
|
-
|
|
|
37
|
-
| [
|
|
38
|
-
| [consistent-import
|
|
39
|
-
| [consistent-
|
|
40
|
-
| [consistent-schema-
|
|
41
|
-
| [consistent-schema-
|
|
42
|
-
| [
|
|
43
|
-
| [no-
|
|
44
|
-
| [no-
|
|
45
|
-
| [no-
|
|
46
|
-
| [no-number-schema-with-
|
|
47
|
-
| [no-number-schema-with-
|
|
48
|
-
| [no-number-schema-with-
|
|
49
|
-
| [no-number-schema-with-
|
|
50
|
-
| [no-
|
|
51
|
-
| [no-
|
|
52
|
-
| [no-
|
|
53
|
-
| [no-
|
|
54
|
-
| [no-
|
|
55
|
-
| [
|
|
56
|
-
| [
|
|
57
|
-
| [prefer-
|
|
58
|
-
| [prefer-
|
|
59
|
-
| [prefer-
|
|
60
|
-
| [
|
|
61
|
-
| [
|
|
62
|
-
| [
|
|
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-native-enum](docs/rules/no-native-enum.md) | Disallow deprecated `z.nativeEnum()` in favor of `z.enum()`. | β
| π§ | | |
|
|
46
|
+
| [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. | β
| π§ | | |
|
|
47
|
+
| [no-number-schema-with-int](docs/rules/no-number-schema-with-int.md) | Disallow usage of `z.number().int()` as it is considered legacy | β
| π§ | | |
|
|
48
|
+
| [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`. | β
| | | |
|
|
49
|
+
| [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. | β
| | | |
|
|
50
|
+
| [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()`. | β
| π§ | | |
|
|
51
|
+
| [no-number-schema-with-step](docs/rules/no-number-schema-with-step.md) | Disallow deprecated `z.number().step()`. Use `.multipleOf()` instead. | β
| π§ | | |
|
|
52
|
+
| [no-optional-and-default-together](docs/rules/no-optional-and-default-together.md) | Disallow using both `.optional()` and `.default()` on the same Zod schema | β
| π§ | | |
|
|
53
|
+
| [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 | β
| π§ | | β |
|
|
54
|
+
| [no-throw-in-refine](docs/rules/no-throw-in-refine.md) | Disallow throwing errors directly inside Zod refine callbacks | β
| | | |
|
|
55
|
+
| [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 | | | | |
|
|
56
|
+
| [no-unknown-schema](docs/rules/no-unknown-schema.md) | Disallow usage of `z.unknown()` in Zod schemas | | | | |
|
|
57
|
+
| [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. | β
| π§ | | |
|
|
58
|
+
| [prefer-meta](docs/rules/prefer-meta.md) | Enforce usage of `.meta()` over `.describe()` | β
| π§ | | |
|
|
59
|
+
| [prefer-meta-last](docs/rules/prefer-meta-last.md) | Enforce `.meta()` as last method | β
| π§ | | |
|
|
60
|
+
| [prefer-string-schema-with-trim](docs/rules/prefer-string-schema-with-trim.md) | Enforce `z.string().trim()` to prevent accidental leading/trailing whitespace | β
| π§ | | |
|
|
61
|
+
| [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 | β
| π§ | | |
|
|
62
|
+
| [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 | β
| π§ | | |
|
|
63
|
+
| [require-brand-type-parameter](docs/rules/require-brand-type-parameter.md) | Require type parameter on `.brand()` functions | β
| | π‘ | |
|
|
64
|
+
| [require-error-message](docs/rules/require-error-message.md) | Enforce that custom refinements include an error message | β
| π§ | | |
|
|
65
|
+
| [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
66
|
|
|
64
67
|
<!-- end auto-generated rules list -->
|
|
65
68
|
|
package/dist/index.cjs
CHANGED
|
@@ -7,6 +7,7 @@ const require_consistent_schema_output_type_style = require("./rules/consistent-
|
|
|
7
7
|
const require_consistent_schema_var_name = require("./rules/consistent-schema-var-name.cjs");
|
|
8
8
|
const require_no_any_schema = require("./rules/no-any-schema.cjs");
|
|
9
9
|
const require_no_empty_custom_schema = require("./rules/no-empty-custom-schema.cjs");
|
|
10
|
+
const require_no_native_enum = require("./rules/no-native-enum.cjs");
|
|
10
11
|
const require_no_number_schema_with_finite = require("./rules/no-number-schema-with-finite.cjs");
|
|
11
12
|
const require_no_number_schema_with_int = require("./rules/no-number-schema-with-int.cjs");
|
|
12
13
|
const require_no_number_schema_with_is_finite = require("./rules/no-number-schema-with-is-finite.cjs");
|
|
@@ -22,6 +23,7 @@ const require_prefer_enum_over_literal_union = require("./rules/prefer-enum-over
|
|
|
22
23
|
const require_prefer_meta_last = require("./rules/prefer-meta-last.cjs");
|
|
23
24
|
const require_prefer_meta = require("./rules/prefer-meta.cjs");
|
|
24
25
|
const require_prefer_string_schema_with_trim = require("./rules/prefer-string-schema-with-trim.cjs");
|
|
26
|
+
const require_prefer_top_level_string_formats = require("./rules/prefer-top-level-string-formats.cjs");
|
|
25
27
|
const require_prefer_trim_before_string_length_checks = require("./rules/prefer-trim-before-string-length-checks.cjs");
|
|
26
28
|
const require_require_brand_type_parameter = require("./rules/require-brand-type-parameter.cjs");
|
|
27
29
|
const require_require_error_message = require("./rules/require-error-message.cjs");
|
|
@@ -41,6 +43,7 @@ const eslintPluginZod = {
|
|
|
41
43
|
"consistent-schema-output-type-style": require_consistent_schema_output_type_style.consistentSchemaOutputTypeStyle,
|
|
42
44
|
"no-any-schema": require_no_any_schema.noAnySchema,
|
|
43
45
|
"no-empty-custom-schema": require_no_empty_custom_schema.noEmptyCustomSchema,
|
|
46
|
+
"no-native-enum": require_no_native_enum.noNativeEnum,
|
|
44
47
|
"no-number-schema-with-finite": require_no_number_schema_with_finite.noNumberSchemaWithFinite,
|
|
45
48
|
"no-number-schema-with-int": require_no_number_schema_with_int.noNumberSchemaWithInt,
|
|
46
49
|
"no-number-schema-with-is-finite": require_no_number_schema_with_is_finite.noNumberSchemaWithIsFinite,
|
|
@@ -55,6 +58,7 @@ const eslintPluginZod = {
|
|
|
55
58
|
"prefer-enum-over-literal-union": require_prefer_enum_over_literal_union.preferEnumOverLiteralUnion,
|
|
56
59
|
"prefer-meta": require_prefer_meta.preferMeta,
|
|
57
60
|
"prefer-meta-last": require_prefer_meta_last.preferMetaLast,
|
|
61
|
+
"prefer-top-level-string-formats": require_prefer_top_level_string_formats.preferTopLevelStringFormats,
|
|
58
62
|
"prefer-string-schema-with-trim": require_prefer_string_schema_with_trim.preferStringSchemaWithTrim,
|
|
59
63
|
"prefer-trim-before-string-length-checks": require_prefer_trim_before_string_length_checks.preferTrimBeforeStringLengthChecks,
|
|
60
64
|
"require-brand-type-parameter": require_require_brand_type_parameter.requireBrandTypeParameter,
|
|
@@ -72,6 +76,7 @@ const recommendedConfig = {
|
|
|
72
76
|
"zod/consistent-schema-var-name": "error",
|
|
73
77
|
"zod/no-any-schema": "error",
|
|
74
78
|
"zod/no-empty-custom-schema": "error",
|
|
79
|
+
"zod/no-native-enum": "error",
|
|
75
80
|
"zod/no-number-schema-with-finite": "error",
|
|
76
81
|
"zod/no-number-schema-with-int": "error",
|
|
77
82
|
"zod/no-number-schema-with-is-finite": "error",
|
|
@@ -84,6 +89,7 @@ const recommendedConfig = {
|
|
|
84
89
|
"zod/prefer-enum-over-literal-union": "error",
|
|
85
90
|
"zod/prefer-meta": "error",
|
|
86
91
|
"zod/prefer-meta-last": "error",
|
|
92
|
+
"zod/prefer-top-level-string-formats": "error",
|
|
87
93
|
"zod/prefer-string-schema-with-trim": "error",
|
|
88
94
|
"zod/prefer-trim-before-string-length-checks": "error",
|
|
89
95
|
"zod/require-brand-type-parameter": "error",
|
package/dist/index.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import { consistentSchemaOutputTypeStyle } from "./rules/consistent-schema-outpu
|
|
|
7
7
|
import { consistentSchemaVarName } from "./rules/consistent-schema-var-name.mjs";
|
|
8
8
|
import { noAnySchema } from "./rules/no-any-schema.mjs";
|
|
9
9
|
import { noEmptyCustomSchema } from "./rules/no-empty-custom-schema.mjs";
|
|
10
|
+
import { noNativeEnum } from "./rules/no-native-enum.mjs";
|
|
10
11
|
import { noNumberSchemaWithFinite } from "./rules/no-number-schema-with-finite.mjs";
|
|
11
12
|
import { noNumberSchemaWithInt } from "./rules/no-number-schema-with-int.mjs";
|
|
12
13
|
import { noNumberSchemaWithIsFinite } from "./rules/no-number-schema-with-is-finite.mjs";
|
|
@@ -22,6 +23,7 @@ import { preferEnumOverLiteralUnion } from "./rules/prefer-enum-over-literal-uni
|
|
|
22
23
|
import { preferMetaLast } from "./rules/prefer-meta-last.mjs";
|
|
23
24
|
import { preferMeta } from "./rules/prefer-meta.mjs";
|
|
24
25
|
import { preferStringSchemaWithTrim } from "./rules/prefer-string-schema-with-trim.mjs";
|
|
26
|
+
import { preferTopLevelStringFormats } from "./rules/prefer-top-level-string-formats.mjs";
|
|
25
27
|
import { preferTrimBeforeStringLengthChecks } from "./rules/prefer-trim-before-string-length-checks.mjs";
|
|
26
28
|
import { requireBrandTypeParameter } from "./rules/require-brand-type-parameter.mjs";
|
|
27
29
|
import { requireErrorMessage } from "./rules/require-error-message.mjs";
|
|
@@ -41,6 +43,7 @@ const eslintPluginZod = {
|
|
|
41
43
|
"consistent-schema-output-type-style": consistentSchemaOutputTypeStyle,
|
|
42
44
|
"no-any-schema": noAnySchema,
|
|
43
45
|
"no-empty-custom-schema": noEmptyCustomSchema,
|
|
46
|
+
"no-native-enum": noNativeEnum,
|
|
44
47
|
"no-number-schema-with-finite": noNumberSchemaWithFinite,
|
|
45
48
|
"no-number-schema-with-int": noNumberSchemaWithInt,
|
|
46
49
|
"no-number-schema-with-is-finite": noNumberSchemaWithIsFinite,
|
|
@@ -55,6 +58,7 @@ const eslintPluginZod = {
|
|
|
55
58
|
"prefer-enum-over-literal-union": preferEnumOverLiteralUnion,
|
|
56
59
|
"prefer-meta": preferMeta,
|
|
57
60
|
"prefer-meta-last": preferMetaLast,
|
|
61
|
+
"prefer-top-level-string-formats": preferTopLevelStringFormats,
|
|
58
62
|
"prefer-string-schema-with-trim": preferStringSchemaWithTrim,
|
|
59
63
|
"prefer-trim-before-string-length-checks": preferTrimBeforeStringLengthChecks,
|
|
60
64
|
"require-brand-type-parameter": requireBrandTypeParameter,
|
|
@@ -72,6 +76,7 @@ const recommendedConfig = {
|
|
|
72
76
|
"zod/consistent-schema-var-name": "error",
|
|
73
77
|
"zod/no-any-schema": "error",
|
|
74
78
|
"zod/no-empty-custom-schema": "error",
|
|
79
|
+
"zod/no-native-enum": "error",
|
|
75
80
|
"zod/no-number-schema-with-finite": "error",
|
|
76
81
|
"zod/no-number-schema-with-int": "error",
|
|
77
82
|
"zod/no-number-schema-with-is-finite": "error",
|
|
@@ -84,6 +89,7 @@ const recommendedConfig = {
|
|
|
84
89
|
"zod/prefer-enum-over-literal-union": "error",
|
|
85
90
|
"zod/prefer-meta": "error",
|
|
86
91
|
"zod/prefer-meta-last": "error",
|
|
92
|
+
"zod/prefer-top-level-string-formats": "error",
|
|
87
93
|
"zod/prefer-string-schema-with-trim": "error",
|
|
88
94
|
"zod/prefer-trim-before-string-length-checks": "error",
|
|
89
95
|
"zod/require-brand-type-parameter": "error",
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
let _typescript_eslint_utils = require("@typescript-eslint/utils");
|
|
5
|
+
//#region src/rules/no-native-enum.ts
|
|
6
|
+
const { trackZodSchemaImports } = (0, _eslint_zod_utils.createZodSchemaImportTrack)(_eslint_zod_utils.zodImportScope);
|
|
7
|
+
const noNativeEnum = require_create_plugin_rule.createZodPluginRule({
|
|
8
|
+
name: "no-native-enum",
|
|
9
|
+
meta: {
|
|
10
|
+
type: "problem",
|
|
11
|
+
fixable: "code",
|
|
12
|
+
docs: { description: "Disallow deprecated `z.nativeEnum()` in favor of `z.enum()`." },
|
|
13
|
+
messages: { useEnum: "`z.nativeEnum()` is deprecated in Zod 4. Use `z.enum()` instead." },
|
|
14
|
+
schema: []
|
|
15
|
+
},
|
|
16
|
+
defaultOptions: [],
|
|
17
|
+
create(context) {
|
|
18
|
+
const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods, getNamedImportOriginal } = trackZodSchemaImports();
|
|
19
|
+
return {
|
|
20
|
+
ImportDeclaration: importDeclarationListener,
|
|
21
|
+
CallExpression(node) {
|
|
22
|
+
const zodSchema = detectZodSchemaRootNode(node);
|
|
23
|
+
const rootMethod = (zodSchema ? collectZodChainMethods(zodSchema.node) : void 0)?.[0];
|
|
24
|
+
const isNativeEnumRoot = zodSchema?.schemaDecl === "namespace" ? rootMethod?.name === "nativeEnum" : getNamedImportOriginal(rootMethod?.name ?? "") === "nativeEnum";
|
|
25
|
+
if (!zodSchema || !isNativeEnumRoot) return;
|
|
26
|
+
if (zodSchema.schemaDecl === "named") {
|
|
27
|
+
context.report({
|
|
28
|
+
node,
|
|
29
|
+
messageId: "useEnum"
|
|
30
|
+
});
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
context.report({
|
|
34
|
+
node,
|
|
35
|
+
messageId: "useEnum",
|
|
36
|
+
fix(fixer) {
|
|
37
|
+
if (rootMethod?.node.callee.type !== _typescript_eslint_utils.AST_NODE_TYPES.MemberExpression || rootMethod.node.callee.property.type !== _typescript_eslint_utils.AST_NODE_TYPES.Identifier) return null;
|
|
38
|
+
return fixer.replaceText(rootMethod.node.callee.property, "enum");
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
//#endregion
|
|
46
|
+
exports.noNativeEnum = noNativeEnum;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createZodPluginRule } from "../utils/create-plugin-rule.mjs";
|
|
2
|
+
import { createZodSchemaImportTrack, zodImportScope } from "@eslint-zod/utils";
|
|
3
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/utils";
|
|
4
|
+
//#region src/rules/no-native-enum.ts
|
|
5
|
+
const { trackZodSchemaImports } = createZodSchemaImportTrack(zodImportScope);
|
|
6
|
+
const noNativeEnum = createZodPluginRule({
|
|
7
|
+
name: "no-native-enum",
|
|
8
|
+
meta: {
|
|
9
|
+
type: "problem",
|
|
10
|
+
fixable: "code",
|
|
11
|
+
docs: { description: "Disallow deprecated `z.nativeEnum()` in favor of `z.enum()`." },
|
|
12
|
+
messages: { useEnum: "`z.nativeEnum()` is deprecated in Zod 4. Use `z.enum()` instead." },
|
|
13
|
+
schema: []
|
|
14
|
+
},
|
|
15
|
+
defaultOptions: [],
|
|
16
|
+
create(context) {
|
|
17
|
+
const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods, getNamedImportOriginal } = trackZodSchemaImports();
|
|
18
|
+
return {
|
|
19
|
+
ImportDeclaration: importDeclarationListener,
|
|
20
|
+
CallExpression(node) {
|
|
21
|
+
const zodSchema = detectZodSchemaRootNode(node);
|
|
22
|
+
const rootMethod = (zodSchema ? collectZodChainMethods(zodSchema.node) : void 0)?.[0];
|
|
23
|
+
const isNativeEnumRoot = zodSchema?.schemaDecl === "namespace" ? rootMethod?.name === "nativeEnum" : getNamedImportOriginal(rootMethod?.name ?? "") === "nativeEnum";
|
|
24
|
+
if (!zodSchema || !isNativeEnumRoot) return;
|
|
25
|
+
if (zodSchema.schemaDecl === "named") {
|
|
26
|
+
context.report({
|
|
27
|
+
node,
|
|
28
|
+
messageId: "useEnum"
|
|
29
|
+
});
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
context.report({
|
|
33
|
+
node,
|
|
34
|
+
messageId: "useEnum",
|
|
35
|
+
fix(fixer) {
|
|
36
|
+
if (rootMethod?.node.callee.type !== AST_NODE_TYPES.MemberExpression || rootMethod.node.callee.property.type !== AST_NODE_TYPES.Identifier) return null;
|
|
37
|
+
return fixer.replaceText(rootMethod.node.callee.property, "enum");
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
//#endregion
|
|
45
|
+
export { noNativeEnum };
|
|
@@ -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 };
|