eslint-plugin-zod 1.4.0 → 3.0.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/LICENSE +19 -22
- package/README.md +83 -65
- package/dist/index.cjs +71 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +68 -0
- package/dist/meta.cjs +13 -0
- package/dist/meta.d.cts +3 -0
- package/dist/meta.d.ts +3 -0
- package/dist/meta.js +8 -0
- package/dist/rules/array-style.cjs +99 -0
- package/dist/rules/array-style.d.cts +10 -0
- package/dist/rules/array-style.d.ts +10 -0
- package/dist/rules/array-style.js +96 -0
- package/dist/rules/consistent-import-source.cjs +73 -0
- package/dist/rules/consistent-import-source.d.cts +10 -0
- package/dist/rules/consistent-import-source.d.ts +10 -0
- package/dist/rules/consistent-import-source.js +70 -0
- package/dist/rules/consistent-object-schema-type.cjs +83 -0
- package/dist/rules/consistent-object-schema-type.d.cts +11 -0
- package/dist/rules/consistent-object-schema-type.d.ts +11 -0
- package/dist/rules/consistent-object-schema-type.js +80 -0
- package/dist/rules/no-any-schema.cjs +66 -0
- package/dist/rules/no-any-schema.d.cts +4 -0
- package/dist/rules/no-any-schema.d.ts +4 -0
- package/dist/rules/no-any-schema.js +63 -0
- package/dist/rules/no-empty-custom-schema.cjs +40 -0
- package/dist/rules/no-empty-custom-schema.d.cts +4 -0
- package/dist/rules/no-empty-custom-schema.d.ts +4 -0
- package/dist/rules/no-empty-custom-schema.js +37 -0
- package/dist/rules/no-number-schema-with-int.cjs +78 -0
- package/dist/rules/no-number-schema-with-int.d.cts +4 -0
- package/dist/rules/no-number-schema-with-int.d.ts +4 -0
- package/dist/rules/no-number-schema-with-int.js +75 -0
- package/dist/rules/no-optional-and-default-together.cjs +80 -0
- package/dist/rules/no-optional-and-default-together.d.cts +11 -0
- package/dist/rules/no-optional-and-default-together.d.ts +11 -0
- package/dist/rules/no-optional-and-default-together.js +77 -0
- package/dist/rules/no-throw-in-refine.cjs +84 -0
- package/dist/rules/no-throw-in-refine.d.cts +4 -0
- package/dist/rules/no-throw-in-refine.d.ts +4 -0
- package/dist/rules/no-throw-in-refine.js +81 -0
- package/dist/rules/no-unknown-schema.cjs +35 -0
- package/dist/rules/no-unknown-schema.d.cts +4 -0
- package/dist/rules/no-unknown-schema.d.ts +4 -0
- package/dist/rules/no-unknown-schema.js +32 -0
- package/dist/rules/prefer-enum-over-literal-union.cjs +72 -0
- package/dist/rules/prefer-enum-over-literal-union.d.cts +4 -0
- package/dist/rules/prefer-enum-over-literal-union.d.ts +4 -0
- package/dist/rules/prefer-enum-over-literal-union.js +69 -0
- package/dist/rules/prefer-meta-last.cjs +71 -0
- package/dist/rules/prefer-meta-last.d.cts +4 -0
- package/dist/rules/prefer-meta-last.d.ts +4 -0
- package/dist/rules/prefer-meta-last.js +68 -0
- package/dist/rules/prefer-meta.cjs +48 -0
- package/dist/rules/prefer-meta.d.cts +4 -0
- package/dist/rules/prefer-meta.d.ts +4 -0
- package/dist/rules/prefer-meta.js +45 -0
- package/dist/rules/prefer-namespace-import.cjs +145 -0
- package/dist/rules/prefer-namespace-import.d.cts +4 -0
- package/dist/rules/prefer-namespace-import.d.ts +4 -0
- package/dist/rules/prefer-namespace-import.js +142 -0
- package/dist/rules/require-brand-type-parameter.cjs +60 -0
- package/dist/rules/require-brand-type-parameter.d.cts +4 -0
- package/dist/rules/require-brand-type-parameter.d.ts +4 -0
- package/dist/rules/require-brand-type-parameter.js +57 -0
- package/dist/rules/require-error-message.cjs +101 -0
- package/dist/rules/require-error-message.d.cts +4 -0
- package/dist/rules/require-error-message.d.ts +4 -0
- package/dist/rules/require-error-message.js +98 -0
- package/dist/rules/require-schema-suffix.cjs +73 -0
- package/dist/rules/require-schema-suffix.d.cts +8 -0
- package/dist/rules/require-schema-suffix.d.ts +8 -0
- package/dist/rules/require-schema-suffix.js +70 -0
- package/dist/rules/schema-error-property-style.cjs +106 -0
- package/dist/rules/schema-error-property-style.d.cts +9 -0
- package/dist/rules/schema-error-property-style.d.ts +9 -0
- package/dist/rules/schema-error-property-style.js +100 -0
- package/dist/utils/detect-zod-schema-root-node.cjs +104 -0
- package/dist/utils/detect-zod-schema-root-node.d.cts +10 -0
- package/dist/utils/detect-zod-schema-root-node.d.ts +10 -0
- package/dist/utils/detect-zod-schema-root-node.js +101 -0
- package/dist/utils/get-outermost-call.cjs +14 -0
- package/dist/utils/get-outermost-call.d.cts +2 -0
- package/dist/utils/get-outermost-call.d.ts +2 -0
- package/dist/utils/get-outermost-call.js +11 -0
- package/dist/utils/is-zod-expression.cjs +14 -0
- package/dist/utils/is-zod-expression.d.cts +2 -0
- package/dist/utils/is-zod-expression.d.ts +2 -0
- package/dist/utils/is-zod-expression.js +11 -0
- package/dist/utils/is-zod-import-source.cjs +6 -0
- package/dist/utils/is-zod-import-source.d.cts +1 -0
- package/dist/utils/is-zod-import-source.d.ts +1 -0
- package/dist/utils/is-zod-import-source.js +3 -0
- package/dist/utils/track-zod-schema-imports.cjs +61 -0
- package/dist/utils/track-zod-schema-imports.d.cts +13 -0
- package/dist/utils/track-zod-schema-imports.d.ts +13 -0
- package/dist/utils/track-zod-schema-imports.js +58 -0
- package/package.json +82 -38
- package/dist/src/index.d.ts +0 -50
- package/dist/src/index.js +0 -17
- package/dist/src/rules/preferEnum.d.ts +0 -19
- package/dist/src/rules/preferEnum.js +0 -43
- package/dist/src/rules/requireStrict.d.ts +0 -24
- package/dist/src/rules/requireStrict.js +0 -69
- package/tsconfig.json +0 -28
package/LICENSE
CHANGED
|
@@ -1,24 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
All rights reserved.
|
|
1
|
+
MIT License
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
modification, are permitted provided that the following conditions are met:
|
|
6
|
-
* Redistributions of source code must retain the above copyright
|
|
7
|
-
notice, this list of conditions and the following disclaimer.
|
|
8
|
-
* Redistributions in binary form must reproduce the above copyright
|
|
9
|
-
notice, this list of conditions and the following disclaimer in the
|
|
10
|
-
documentation and/or other materials provided with the distribution.
|
|
11
|
-
* Neither the name of the Gajus Kuizinas (https://gajus.com/) nor the
|
|
12
|
-
names of its contributors may be used to endorse or promote products
|
|
13
|
-
derived from this software without specific prior written permission.
|
|
3
|
+
Copyright (c) 2025-present Marco Pasqualetti
|
|
14
4
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,88 +1,106 @@
|
|
|
1
|
-
<a name="user-content-eslint-plugin-zod"></a>
|
|
2
|
-
<a name="eslint-plugin-zod"></a>
|
|
3
1
|
# eslint-plugin-zod
|
|
4
2
|
|
|
5
|
-
[![
|
|
6
|
-
[![
|
|
7
|
-
[![
|
|
3
|
+
[![CI][CIBadge]][CIURL]
|
|
4
|
+
[![Code style: prettier][CodeStyleBadge]][CodeStyleURL]
|
|
5
|
+
[![Lint: eslint][lintBadge]][lintURL]
|
|
6
|
+
[![npm version][npmVersionBadge]][npmVersionURL]
|
|
7
|
+
[![issues][issuesBadge]][issuesURL]
|
|
8
|
+
|
|
9
|
+
[CIBadge]: https://img.shields.io/github/actions/workflow/status/marcalexiei/eslint-plugin-zod/ci.yml?style=for-the-badge&logo=github&event=push&label=CI
|
|
10
|
+
[CIURL]: https://github.com/marcalexiei/eslint-plugin-zod/actions/workflows/CI.yml/badge.svg
|
|
11
|
+
[CodeStyleBadge]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=for-the-badge&logo=prettier
|
|
12
|
+
[CodeStyleURL]: https://prettier.io
|
|
13
|
+
[npmVersionBadge]: https://img.shields.io/npm/v/eslint-plugin-zod.svg?style=for-the-badge&logo=npm
|
|
14
|
+
[npmVersionURL]: https://www.npmjs.com/package/eslint-plugin-zod
|
|
15
|
+
[lintBadge]: https://img.shields.io/badge/lint-eslint-3A33D1?logo=eslint&style=for-the-badge
|
|
16
|
+
[lintURL]: https://eslint.org
|
|
17
|
+
[issuesBadge]: https://img.shields.io/github/issues/marcalexiei/eslint-plugin-zod.svg?style=for-the-badge
|
|
18
|
+
[issuesURL]: https://github.com/marcalexiei/eslint-plugin-zod/issues
|
|
19
|
+
|
|
20
|
+
[ESLint](https://eslint.org) plugin that adds custom linting rules to enforce best practices when using [Zod](https://github.com/colinhacks/zod)
|
|
8
21
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* [eslint-plugin-zod](#user-content-eslint-plugin-zod)
|
|
12
|
-
* [Installation](#user-content-eslint-plugin-zod-installation)
|
|
13
|
-
* [Configuration](#user-content-eslint-plugin-zod-configuration)
|
|
14
|
-
* [Rules](#user-content-eslint-plugin-zod-rules)
|
|
15
|
-
* [`prefer-enum`](#user-content-eslint-plugin-zod-rules-prefer-enum)
|
|
16
|
-
* [`require-strict`](#user-content-eslint-plugin-zod-rules-require-strict)
|
|
22
|
+
## Rules
|
|
17
23
|
|
|
24
|
+
<!-- begin auto-generated rules list -->
|
|
25
|
+
|
|
26
|
+
💼 Configurations enabled in.\
|
|
27
|
+
✅ Set in the `recommended` configuration.\
|
|
28
|
+
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
|
|
29
|
+
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
30
|
+
|
|
31
|
+
| Name | Description | 💼 | 🔧 | 💡 |
|
|
32
|
+
| :--------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------- | :-- | :-- | :-- |
|
|
33
|
+
| [array-style](docs/rules/array-style.md) | Enforce consistent Zod array style | ✅ | 🔧 | |
|
|
34
|
+
| [consistent-import-source](docs/rules/consistent-import-source.md) | Enforce consistent source from Zod imports | | | 💡 |
|
|
35
|
+
| [consistent-object-schema-type](docs/rules/consistent-object-schema-type.md) | Enforce consistent usage of Zod schema methods | | | 💡 |
|
|
36
|
+
| [no-any-schema](docs/rules/no-any-schema.md) | Disallow usage of `z.any()` in Zod schemas | ✅ | | 💡 |
|
|
37
|
+
| [no-empty-custom-schema](docs/rules/no-empty-custom-schema.md) | Disallow usage of `z.custom()` without arguments | ✅ | | |
|
|
38
|
+
| [no-number-schema-with-int](docs/rules/no-number-schema-with-int.md) | Disallow usage of `z.number().int()` as it is considered legacy | ✅ | 🔧 | |
|
|
39
|
+
| [no-optional-and-default-together](docs/rules/no-optional-and-default-together.md) | Disallow using both `.optional()` and `.default()` on the same Zod schema | ✅ | 🔧 | |
|
|
40
|
+
| [no-throw-in-refine](docs/rules/no-throw-in-refine.md) | Disallow throwing errors directly inside Zod refine callbacks | ✅ | | |
|
|
41
|
+
| [no-unknown-schema](docs/rules/no-unknown-schema.md) | Disallow usage of `z.unknown()` in Zod schemas | | | |
|
|
42
|
+
| [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. | ✅ | 🔧 | |
|
|
43
|
+
| [prefer-meta](docs/rules/prefer-meta.md) | Enforce usage of `.meta()` over `.describe()` | ✅ | 🔧 | |
|
|
44
|
+
| [prefer-meta-last](docs/rules/prefer-meta-last.md) | Enforce `.meta()` as last method | ✅ | 🔧 | |
|
|
45
|
+
| [prefer-namespace-import](docs/rules/prefer-namespace-import.md) | Enforce importing zod as a namespace import (`import * as z from 'zod'`) | ✅ | 🔧 | |
|
|
46
|
+
| [require-brand-type-parameter](docs/rules/require-brand-type-parameter.md) | Require type parameter on `.brand()` functions | ✅ | | 💡 |
|
|
47
|
+
| [require-error-message](docs/rules/require-error-message.md) | Enforce that custom refinements include an error message | ✅ | 🔧 | |
|
|
48
|
+
| [require-schema-suffix](docs/rules/require-schema-suffix.md) | Require schema suffix when declaring a Zod schema | ✅ | | |
|
|
49
|
+
| [schema-error-property-style](docs/rules/schema-error-property-style.md) | Enforce consistent style for error messages in Zod schema validation (using ESQuery patterns) | | | |
|
|
50
|
+
|
|
51
|
+
<!-- end auto-generated rules list -->
|
|
18
52
|
|
|
19
|
-
<a name="user-content-eslint-plugin-zod-installation"></a>
|
|
20
|
-
<a name="eslint-plugin-zod-installation"></a>
|
|
21
53
|
## Installation
|
|
22
54
|
|
|
23
|
-
|
|
24
|
-
1. Install [`eslint-plugin-zod`](https://github.com/gajus/eslint-plugin-zod) plugin.
|
|
55
|
+
Install `eslint` and `eslint-plugin-zod` using your preferred package manager:
|
|
25
56
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
```sh
|
|
29
|
-
npm install eslint --save-dev
|
|
30
|
-
npm install eslint-plugin-zod --save-dev
|
|
57
|
+
```shell
|
|
58
|
+
npm i --save-dev eslint eslint-plugin-zod
|
|
31
59
|
```
|
|
32
60
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
## Configuration
|
|
36
|
-
|
|
37
|
-
1. Add `plugins` section and specify `eslint-plugin-zod` as a plugin.
|
|
38
|
-
1. Enable rules.
|
|
39
|
-
|
|
40
|
-
<!-- -->
|
|
41
|
-
|
|
42
|
-
```json
|
|
43
|
-
{
|
|
44
|
-
"plugins": [
|
|
45
|
-
"zod"
|
|
46
|
-
],
|
|
47
|
-
"rules": {
|
|
48
|
-
"zod/prefer-enum": 2,
|
|
49
|
-
"zod/require-strict": 2
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
61
|
+
```shell
|
|
62
|
+
yarn add --dev eslint eslint-plugin-zod
|
|
53
63
|
```
|
|
54
64
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
<!-- Rules are sorted alphabetically. -->
|
|
60
|
-
|
|
61
|
-
<a name="user-content-eslint-plugin-zod-rules-prefer-enum"></a>
|
|
62
|
-
<a name="eslint-plugin-zod-rules-prefer-enum"></a>
|
|
63
|
-
### <code>prefer-enum</code>
|
|
64
|
-
|
|
65
|
-
_The `--fix` option on the command line automatically fixes problems reported by this rule._
|
|
65
|
+
```shell
|
|
66
|
+
pnpm add --save-dev eslint eslint-plugin-zod
|
|
67
|
+
```
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
## Configuration
|
|
68
70
|
|
|
71
|
+
1. Import the plugin
|
|
69
72
|
|
|
73
|
+
```ts
|
|
74
|
+
import eslintPluginZod from 'eslint-plugin-zod';
|
|
75
|
+
```
|
|
70
76
|
|
|
71
|
-
|
|
72
|
-
<a name="eslint-plugin-zod-rules-require-strict"></a>
|
|
73
|
-
### <code>require-strict</code>
|
|
77
|
+
2. Add `recommended` config to your ESLint setup
|
|
74
78
|
|
|
75
|
-
|
|
79
|
+
```ts
|
|
80
|
+
eslintPluginZod.configs.recommended,
|
|
81
|
+
```
|
|
76
82
|
|
|
77
|
-
|
|
83
|
+
Here’s a minimal example using the flat config format:
|
|
78
84
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
```ts
|
|
86
|
+
// eslint.config.js
|
|
87
|
+
import { defineConfig } from 'eslint/config';
|
|
88
|
+
import eslint from '@eslint/js';
|
|
89
|
+
import eslintPluginZod from 'eslint-plugin-zod';
|
|
82
90
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
91
|
+
export default defineConfig(
|
|
92
|
+
eslint.configs.recommended,
|
|
93
|
+
eslintPluginZod.configs.recommended,
|
|
94
|
+
);
|
|
95
|
+
```
|
|
86
96
|
|
|
97
|
+
## Zod peer dependency version
|
|
87
98
|
|
|
99
|
+
`eslint-plugin-zod` is designed for projects that use `zod@^4`.
|
|
100
|
+
While the plugin analyzes Zod schemas in your code,
|
|
101
|
+
it doesn’t import or depend on Zod at runtime.
|
|
102
|
+
To document this relationship without forcing installation,
|
|
103
|
+
Zod is declared as an optional peer dependency in the plugin’s `package.json`.
|
|
88
104
|
|
|
105
|
+
If your project uses Zod v4, the plugin will automatically lint your schemas.
|
|
106
|
+
If you’re not using Zod (for example, in a separate ESLint workspace), you don’t need to install it.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const meta_js_1 = require("./meta.cjs");
|
|
4
|
+
const array_style_js_1 = require("./rules/array-style.cjs");
|
|
5
|
+
const consistent_import_source_js_1 = require("./rules/consistent-import-source.cjs");
|
|
6
|
+
const consistent_object_schema_type_js_1 = require("./rules/consistent-object-schema-type.cjs");
|
|
7
|
+
const no_any_schema_js_1 = require("./rules/no-any-schema.cjs");
|
|
8
|
+
const no_empty_custom_schema_js_1 = require("./rules/no-empty-custom-schema.cjs");
|
|
9
|
+
const no_number_schema_with_int_js_1 = require("./rules/no-number-schema-with-int.cjs");
|
|
10
|
+
const no_optional_and_default_together_js_1 = require("./rules/no-optional-and-default-together.cjs");
|
|
11
|
+
const no_throw_in_refine_js_1 = require("./rules/no-throw-in-refine.cjs");
|
|
12
|
+
const no_unknown_schema_js_1 = require("./rules/no-unknown-schema.cjs");
|
|
13
|
+
const prefer_enum_over_literal_union_js_1 = require("./rules/prefer-enum-over-literal-union.cjs");
|
|
14
|
+
const prefer_meta_last_js_1 = require("./rules/prefer-meta-last.cjs");
|
|
15
|
+
const prefer_meta_js_1 = require("./rules/prefer-meta.cjs");
|
|
16
|
+
const prefer_namespace_import_js_1 = require("./rules/prefer-namespace-import.cjs");
|
|
17
|
+
const require_brand_type_parameter_js_1 = require("./rules/require-brand-type-parameter.cjs");
|
|
18
|
+
const require_error_message_js_1 = require("./rules/require-error-message.cjs");
|
|
19
|
+
const require_schema_suffix_js_1 = require("./rules/require-schema-suffix.cjs");
|
|
20
|
+
const schema_error_property_style_js_1 = require("./rules/schema-error-property-style.cjs");
|
|
21
|
+
const eslintPluginZod = {
|
|
22
|
+
meta: {
|
|
23
|
+
name: meta_js_1.PLUGIN_NAME,
|
|
24
|
+
version: meta_js_1.PLUGIN_VERSION,
|
|
25
|
+
},
|
|
26
|
+
rules: {
|
|
27
|
+
'array-style': array_style_js_1.arrayStyle,
|
|
28
|
+
'consistent-import-source': consistent_import_source_js_1.consistentImportSource,
|
|
29
|
+
'consistent-object-schema-type': consistent_object_schema_type_js_1.consistentObjectSchemaType,
|
|
30
|
+
'no-any-schema': no_any_schema_js_1.noAnySchema,
|
|
31
|
+
'no-empty-custom-schema': no_empty_custom_schema_js_1.noEmptyCustomSchema,
|
|
32
|
+
'no-number-schema-with-int': no_number_schema_with_int_js_1.noNumberSchemaWithInt,
|
|
33
|
+
'no-optional-and-default-together': no_optional_and_default_together_js_1.noOptionalAndDefaultTogether,
|
|
34
|
+
'no-throw-in-refine': no_throw_in_refine_js_1.noThrowInRefine,
|
|
35
|
+
'no-unknown-schema': no_unknown_schema_js_1.noUnknownSchema,
|
|
36
|
+
'prefer-enum-over-literal-union': prefer_enum_over_literal_union_js_1.preferEnumOverLiteralUnion,
|
|
37
|
+
'prefer-meta': prefer_meta_js_1.preferMeta,
|
|
38
|
+
'prefer-meta-last': prefer_meta_last_js_1.preferMetaLast,
|
|
39
|
+
'prefer-namespace-import': prefer_namespace_import_js_1.preferNamespaceImport,
|
|
40
|
+
'require-brand-type-parameter': require_brand_type_parameter_js_1.requireBrandTypeParameter,
|
|
41
|
+
'require-error-message': require_error_message_js_1.requireErrorMessage,
|
|
42
|
+
'require-schema-suffix': require_schema_suffix_js_1.requireSchemaSuffix,
|
|
43
|
+
'schema-error-property-style': schema_error_property_style_js_1.schemaErrorPropertyStyle,
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
const recommendedConfig = {
|
|
47
|
+
name: `${meta_js_1.PLUGIN_NAME}/recommended`,
|
|
48
|
+
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
|
|
49
|
+
plugins: {
|
|
50
|
+
zod: eslintPluginZod,
|
|
51
|
+
},
|
|
52
|
+
rules: {
|
|
53
|
+
'zod/array-style': 'error',
|
|
54
|
+
'zod/no-any-schema': 'error',
|
|
55
|
+
'zod/no-empty-custom-schema': 'error',
|
|
56
|
+
'zod/no-number-schema-with-int': 'error',
|
|
57
|
+
'zod/no-optional-and-default-together': 'error',
|
|
58
|
+
'zod/no-throw-in-refine': 'error',
|
|
59
|
+
'zod/prefer-enum-over-literal-union': 'error',
|
|
60
|
+
'zod/prefer-meta': 'error',
|
|
61
|
+
'zod/prefer-meta-last': 'error',
|
|
62
|
+
'zod/prefer-namespace-import': 'error',
|
|
63
|
+
'zod/require-brand-type-parameter': 'error',
|
|
64
|
+
'zod/require-error-message': 'error',
|
|
65
|
+
'zod/require-schema-suffix': 'error',
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
exports.default = Object.assign(Object.assign({}, eslintPluginZod), { configs: {
|
|
69
|
+
recommended: recommendedConfig,
|
|
70
|
+
} });
|
|
71
|
+
module.exports = exports.default;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface CompatibleConfig {
|
|
2
|
+
name?: string;
|
|
3
|
+
rules?: object;
|
|
4
|
+
plugins?: Record<string, CompatiblePlugin>;
|
|
5
|
+
}
|
|
6
|
+
interface CompatiblePlugin {
|
|
7
|
+
meta: {
|
|
8
|
+
name: string;
|
|
9
|
+
version: string;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
declare const _default: {
|
|
13
|
+
configs: {
|
|
14
|
+
recommended: CompatibleConfig;
|
|
15
|
+
};
|
|
16
|
+
meta: {
|
|
17
|
+
name: string;
|
|
18
|
+
version: string;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export = _default;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface CompatibleConfig {
|
|
2
|
+
name?: string;
|
|
3
|
+
rules?: object;
|
|
4
|
+
plugins?: Record<string, CompatiblePlugin>;
|
|
5
|
+
}
|
|
6
|
+
interface CompatiblePlugin {
|
|
7
|
+
meta: {
|
|
8
|
+
name: string;
|
|
9
|
+
version: string;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
declare const _default: {
|
|
13
|
+
configs: {
|
|
14
|
+
recommended: CompatibleConfig;
|
|
15
|
+
};
|
|
16
|
+
meta: {
|
|
17
|
+
name: string;
|
|
18
|
+
version: string;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { PLUGIN_NAME, PLUGIN_VERSION } from "./meta.js";
|
|
2
|
+
import { arrayStyle } from "./rules/array-style.js";
|
|
3
|
+
import { consistentImportSource } from "./rules/consistent-import-source.js";
|
|
4
|
+
import { consistentObjectSchemaType } from "./rules/consistent-object-schema-type.js";
|
|
5
|
+
import { noAnySchema } from "./rules/no-any-schema.js";
|
|
6
|
+
import { noEmptyCustomSchema } from "./rules/no-empty-custom-schema.js";
|
|
7
|
+
import { noNumberSchemaWithInt } from "./rules/no-number-schema-with-int.js";
|
|
8
|
+
import { noOptionalAndDefaultTogether } from "./rules/no-optional-and-default-together.js";
|
|
9
|
+
import { noThrowInRefine } from "./rules/no-throw-in-refine.js";
|
|
10
|
+
import { noUnknownSchema } from "./rules/no-unknown-schema.js";
|
|
11
|
+
import { preferEnumOverLiteralUnion } from "./rules/prefer-enum-over-literal-union.js";
|
|
12
|
+
import { preferMetaLast } from "./rules/prefer-meta-last.js";
|
|
13
|
+
import { preferMeta } from "./rules/prefer-meta.js";
|
|
14
|
+
import { preferNamespaceImport } from "./rules/prefer-namespace-import.js";
|
|
15
|
+
import { requireBrandTypeParameter } from "./rules/require-brand-type-parameter.js";
|
|
16
|
+
import { requireErrorMessage } from "./rules/require-error-message.js";
|
|
17
|
+
import { requireSchemaSuffix } from "./rules/require-schema-suffix.js";
|
|
18
|
+
import { schemaErrorPropertyStyle } from "./rules/schema-error-property-style.js";
|
|
19
|
+
const eslintPluginZod = {
|
|
20
|
+
meta: {
|
|
21
|
+
name: PLUGIN_NAME,
|
|
22
|
+
version: PLUGIN_VERSION,
|
|
23
|
+
},
|
|
24
|
+
rules: {
|
|
25
|
+
'array-style': arrayStyle,
|
|
26
|
+
'consistent-import-source': consistentImportSource,
|
|
27
|
+
'consistent-object-schema-type': consistentObjectSchemaType,
|
|
28
|
+
'no-any-schema': noAnySchema,
|
|
29
|
+
'no-empty-custom-schema': noEmptyCustomSchema,
|
|
30
|
+
'no-number-schema-with-int': noNumberSchemaWithInt,
|
|
31
|
+
'no-optional-and-default-together': noOptionalAndDefaultTogether,
|
|
32
|
+
'no-throw-in-refine': noThrowInRefine,
|
|
33
|
+
'no-unknown-schema': noUnknownSchema,
|
|
34
|
+
'prefer-enum-over-literal-union': preferEnumOverLiteralUnion,
|
|
35
|
+
'prefer-meta': preferMeta,
|
|
36
|
+
'prefer-meta-last': preferMetaLast,
|
|
37
|
+
'prefer-namespace-import': preferNamespaceImport,
|
|
38
|
+
'require-brand-type-parameter': requireBrandTypeParameter,
|
|
39
|
+
'require-error-message': requireErrorMessage,
|
|
40
|
+
'require-schema-suffix': requireSchemaSuffix,
|
|
41
|
+
'schema-error-property-style': schemaErrorPropertyStyle,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
const recommendedConfig = {
|
|
45
|
+
name: `${PLUGIN_NAME}/recommended`,
|
|
46
|
+
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
|
|
47
|
+
plugins: {
|
|
48
|
+
zod: eslintPluginZod,
|
|
49
|
+
},
|
|
50
|
+
rules: {
|
|
51
|
+
'zod/array-style': 'error',
|
|
52
|
+
'zod/no-any-schema': 'error',
|
|
53
|
+
'zod/no-empty-custom-schema': 'error',
|
|
54
|
+
'zod/no-number-schema-with-int': 'error',
|
|
55
|
+
'zod/no-optional-and-default-together': 'error',
|
|
56
|
+
'zod/no-throw-in-refine': 'error',
|
|
57
|
+
'zod/prefer-enum-over-literal-union': 'error',
|
|
58
|
+
'zod/prefer-meta': 'error',
|
|
59
|
+
'zod/prefer-meta-last': 'error',
|
|
60
|
+
'zod/prefer-namespace-import': 'error',
|
|
61
|
+
'zod/require-brand-type-parameter': 'error',
|
|
62
|
+
'zod/require-error-message': 'error',
|
|
63
|
+
'zod/require-schema-suffix': 'error',
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
export default Object.assign(Object.assign({}, eslintPluginZod), { configs: {
|
|
67
|
+
recommended: recommendedConfig,
|
|
68
|
+
} });
|
package/dist/meta.cjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PLUGIN_VERSION = exports.PLUGIN_NAME = void 0;
|
|
4
|
+
exports.getRuleURL = getRuleURL;
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const packageJSON = JSON.parse((0, node_fs_1.readFileSync)(new URL('../package.json', require("url").pathToFileURL(__filename)), 'utf8'));
|
|
7
|
+
const { name: PLUGIN_NAME, version: PLUGIN_VERSION, homepage, } = packageJSON;
|
|
8
|
+
exports.PLUGIN_NAME = PLUGIN_NAME;
|
|
9
|
+
exports.PLUGIN_VERSION = PLUGIN_VERSION;
|
|
10
|
+
const PLUGIN_HOMEPAGE = homepage.replace(/#[^#]*$/, '');
|
|
11
|
+
function getRuleURL(ruleID) {
|
|
12
|
+
return `${PLUGIN_HOMEPAGE}/blob/HEAD/docs/rules/${ruleID}.md`;
|
|
13
|
+
}
|
package/dist/meta.d.cts
ADDED
package/dist/meta.d.ts
ADDED
package/dist/meta.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
const packageJSON = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
3
|
+
const { name: PLUGIN_NAME, version: PLUGIN_VERSION, homepage, } = packageJSON;
|
|
4
|
+
export { PLUGIN_NAME, PLUGIN_VERSION };
|
|
5
|
+
const PLUGIN_HOMEPAGE = homepage.replace(/#[^#]*$/, '');
|
|
6
|
+
export function getRuleURL(ruleID) {
|
|
7
|
+
return `${PLUGIN_HOMEPAGE}/blob/HEAD/docs/rules/${ruleID}.md`;
|
|
8
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.arrayStyle = void 0;
|
|
4
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
5
|
+
const meta_js_1 = require("../meta.cjs");
|
|
6
|
+
const is_zod_expression_js_1 = require("../utils/is-zod-expression.cjs");
|
|
7
|
+
const track_zod_schema_imports_js_1 = require("../utils/track-zod-schema-imports.cjs");
|
|
8
|
+
const ZOD_ARRAY_STYLES = ['function', 'method'];
|
|
9
|
+
const defaultOptions = { style: 'function' };
|
|
10
|
+
exports.arrayStyle = utils_1.ESLintUtils.RuleCreator(meta_js_1.getRuleURL)({
|
|
11
|
+
name: 'array-style',
|
|
12
|
+
meta: {
|
|
13
|
+
type: 'suggestion',
|
|
14
|
+
fixable: 'code',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Enforce consistent Zod array style',
|
|
17
|
+
},
|
|
18
|
+
messages: {
|
|
19
|
+
useFunction: 'Use z.array(schema) instead of schema.array().',
|
|
20
|
+
useMethod: 'Use schema.array() instead of z.array(schema).',
|
|
21
|
+
},
|
|
22
|
+
schema: [
|
|
23
|
+
{
|
|
24
|
+
type: 'object',
|
|
25
|
+
properties: {
|
|
26
|
+
style: {
|
|
27
|
+
description: 'Decides which style for zod array function',
|
|
28
|
+
type: 'string',
|
|
29
|
+
enum: ZOD_ARRAY_STYLES,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
additionalProperties: false,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
defaultOptions: [defaultOptions],
|
|
37
|
+
create(context, [{ style }]) {
|
|
38
|
+
const { sourceCode } = context;
|
|
39
|
+
const { importDeclarationListener, detectZodSchemaRootNode, collectZodChainMethods, } = (0, track_zod_schema_imports_js_1.trackZodSchemaImports)();
|
|
40
|
+
return {
|
|
41
|
+
ImportDeclaration: importDeclarationListener,
|
|
42
|
+
CallExpression(node) {
|
|
43
|
+
const zodSchema = detectZodSchemaRootNode(node);
|
|
44
|
+
if (!zodSchema) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const { schemaDecl, schemaType } = zodSchema;
|
|
48
|
+
if (style === 'method') {
|
|
49
|
+
if (schemaType === 'array') {
|
|
50
|
+
if (schemaDecl === 'namespace') {
|
|
51
|
+
context.report({
|
|
52
|
+
node,
|
|
53
|
+
messageId: 'useMethod',
|
|
54
|
+
fix(fixer) {
|
|
55
|
+
const chain = collectZodChainMethods(node);
|
|
56
|
+
const arrayCall = chain.find((c) => c.name === 'array');
|
|
57
|
+
if (!arrayCall) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const arg = arrayCall.node.arguments.at(0);
|
|
61
|
+
if (!arg) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const argText = sourceCode.getText(arg);
|
|
65
|
+
return fixer.replaceText(arrayCall.node, `${argText}.array()`);
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
context.report({
|
|
71
|
+
node,
|
|
72
|
+
messageId: 'useMethod',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const { callee } = node;
|
|
78
|
+
if ((0, is_zod_expression_js_1.isZodExpressionEndingWithMethod)(callee, 'array') &&
|
|
79
|
+
node.arguments.length === 0) {
|
|
80
|
+
if (schemaDecl === 'namespace') {
|
|
81
|
+
context.report({
|
|
82
|
+
node,
|
|
83
|
+
messageId: 'useFunction',
|
|
84
|
+
fix(fixer) {
|
|
85
|
+
const objText = sourceCode.getText(callee.object);
|
|
86
|
+
return fixer.replaceText(node, `z.array(${objText})`);
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
context.report({
|
|
92
|
+
node,
|
|
93
|
+
messageId: 'useFunction',
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
declare const ZOD_ARRAY_STYLES: string[];
|
|
3
|
+
interface Options {
|
|
4
|
+
style: (typeof ZOD_ARRAY_STYLES)[number];
|
|
5
|
+
}
|
|
6
|
+
type MessageIds = 'useFunction' | 'useMethod';
|
|
7
|
+
export declare const arrayStyle: ESLintUtils.RuleModule<MessageIds, [Options], unknown, ESLintUtils.RuleListener> & {
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
declare const ZOD_ARRAY_STYLES: string[];
|
|
3
|
+
interface Options {
|
|
4
|
+
style: (typeof ZOD_ARRAY_STYLES)[number];
|
|
5
|
+
}
|
|
6
|
+
type MessageIds = 'useFunction' | 'useMethod';
|
|
7
|
+
export declare const arrayStyle: ESLintUtils.RuleModule<MessageIds, [Options], unknown, ESLintUtils.RuleListener> & {
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
10
|
+
export {};
|