eslint-plugin-zod 4.1.0 → 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 +1 -0
- package/dist/index.cjs +3 -0
- package/dist/index.mjs +3 -0
- package/dist/rules/no-native-enum.cjs +46 -0
- package/dist/rules/no-native-enum.mjs +45 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -42,6 +42,7 @@ Find out more about [Oxlint's `jsPLugins`](https://oxc.rs/docs/guide/usage/linte
|
|
|
42
42
|
| [consistent-schema-var-name](docs/rules/consistent-schema-var-name.md) | Enforce a consistent naming convention for Zod schema variables | ✅ | | | |
|
|
43
43
|
| [no-any-schema](docs/rules/no-any-schema.md) | Disallow usage of `z.any()` in Zod schemas | ✅ | | 💡 | |
|
|
44
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()`. | ✅ | 🔧 | | |
|
|
45
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. | ✅ | 🔧 | | |
|
|
46
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 | ✅ | 🔧 | | |
|
|
47
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`. | ✅ | | | |
|
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");
|
|
@@ -42,6 +43,7 @@ const eslintPluginZod = {
|
|
|
42
43
|
"consistent-schema-output-type-style": require_consistent_schema_output_type_style.consistentSchemaOutputTypeStyle,
|
|
43
44
|
"no-any-schema": require_no_any_schema.noAnySchema,
|
|
44
45
|
"no-empty-custom-schema": require_no_empty_custom_schema.noEmptyCustomSchema,
|
|
46
|
+
"no-native-enum": require_no_native_enum.noNativeEnum,
|
|
45
47
|
"no-number-schema-with-finite": require_no_number_schema_with_finite.noNumberSchemaWithFinite,
|
|
46
48
|
"no-number-schema-with-int": require_no_number_schema_with_int.noNumberSchemaWithInt,
|
|
47
49
|
"no-number-schema-with-is-finite": require_no_number_schema_with_is_finite.noNumberSchemaWithIsFinite,
|
|
@@ -74,6 +76,7 @@ const recommendedConfig = {
|
|
|
74
76
|
"zod/consistent-schema-var-name": "error",
|
|
75
77
|
"zod/no-any-schema": "error",
|
|
76
78
|
"zod/no-empty-custom-schema": "error",
|
|
79
|
+
"zod/no-native-enum": "error",
|
|
77
80
|
"zod/no-number-schema-with-finite": "error",
|
|
78
81
|
"zod/no-number-schema-with-int": "error",
|
|
79
82
|
"zod/no-number-schema-with-is-finite": "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";
|
|
@@ -42,6 +43,7 @@ const eslintPluginZod = {
|
|
|
42
43
|
"consistent-schema-output-type-style": consistentSchemaOutputTypeStyle,
|
|
43
44
|
"no-any-schema": noAnySchema,
|
|
44
45
|
"no-empty-custom-schema": noEmptyCustomSchema,
|
|
46
|
+
"no-native-enum": noNativeEnum,
|
|
45
47
|
"no-number-schema-with-finite": noNumberSchemaWithFinite,
|
|
46
48
|
"no-number-schema-with-int": noNumberSchemaWithInt,
|
|
47
49
|
"no-number-schema-with-is-finite": noNumberSchemaWithIsFinite,
|
|
@@ -74,6 +76,7 @@ const recommendedConfig = {
|
|
|
74
76
|
"zod/consistent-schema-var-name": "error",
|
|
75
77
|
"zod/no-any-schema": "error",
|
|
76
78
|
"zod/no-empty-custom-schema": "error",
|
|
79
|
+
"zod/no-native-enum": "error",
|
|
77
80
|
"zod/no-number-schema-with-finite": "error",
|
|
78
81
|
"zod/no-number-schema-with-int": "error",
|
|
79
82
|
"zod/no-number-schema-with-is-finite": "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 };
|