@brutalway/oxlint-config-react-hook-form 0.2.0 → 0.2.1
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 +16 -4
- package/dist/plugin.js +23 -16
- package/package.json +34 -14
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# @brutalway/oxlint-config-react-hook-form
|
|
2
2
|
|
|
3
|
-
Oxlint config for react-hook-form with Zod. A form validates in one place, its schema
|
|
3
|
+
Oxlint config for react-hook-form with Zod. A form validates in one place, its schema.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
4
6
|
|
|
5
7
|
```sh
|
|
6
8
|
npm install --save-dev oxlint @brutalway/oxlint-config-react-hook-form
|
|
@@ -16,7 +18,13 @@ export default defineConfig({
|
|
|
16
18
|
});
|
|
17
19
|
```
|
|
18
20
|
|
|
19
|
-
##
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
When `form-conventions/validation-in-schema` fires, move the rule into the Zod schema, where `.min(1, 'The title is blank')` also carries the message shown to the user.
|
|
24
|
+
|
|
25
|
+
## Rules
|
|
26
|
+
|
|
27
|
+
### `form-conventions/zod-resolver`
|
|
20
28
|
|
|
21
29
|
`useForm` takes `resolver: zodResolver(XSchema)`. A `useForm()` without options, or with another resolver, is reported:
|
|
22
30
|
|
|
@@ -24,6 +32,10 @@ export default defineConfig({
|
|
|
24
32
|
useForm({ resolver: zodResolver(TodoDraftSchema), defaultValues: { title: '' } });
|
|
25
33
|
```
|
|
26
34
|
|
|
27
|
-
|
|
35
|
+
### `form-conventions/validation-in-schema`
|
|
36
|
+
|
|
37
|
+
No validation outside the schema: `register('title', { required: true })`, `useController({ rules })` and `<Controller rules={...} />` are reported.
|
|
38
|
+
|
|
39
|
+
## Why
|
|
28
40
|
|
|
29
|
-
|
|
41
|
+
With the schema as the only place that validates, the type of the values the form hands over is the type the schema checked.
|
package/dist/plugin.js
CHANGED
|
@@ -1,37 +1,44 @@
|
|
|
1
1
|
import { definePlugin, defineRule } from "@oxlint/plugins";
|
|
2
|
+
// src/messages.json
|
|
3
|
+
var outsideSchema = "Validation lives in the Zod schema of the form: move this rule into it, so there is one place to read what a field accepts.";
|
|
4
|
+
var noSchema = "useForm takes resolver: zodResolver(XSchema), so the type of the form is the type its schema validates.";
|
|
2
5
|
// src/features/validation-in-schema/validationInSchema.ts
|
|
3
6
|
function calleeName(callee) {
|
|
4
7
|
if (callee.type === "Identifier") return callee.name;
|
|
5
8
|
if (callee.type === "MemberExpression" && callee.property.type === "Identifier") return callee.property.name;
|
|
6
9
|
}
|
|
10
|
+
function isRulesProperty(property) {
|
|
11
|
+
return property.type === "Property" && property.key.type === "Identifier" && property.key.name === "rules";
|
|
12
|
+
}
|
|
13
|
+
function validationOutsideSchema(call) {
|
|
14
|
+
const name = calleeName(call.callee);
|
|
15
|
+
if (name === "register") return call.arguments.length > 1 ? [call.arguments[1] ?? call] : [];
|
|
16
|
+
if (name !== "useController") return [];
|
|
17
|
+
const [options] = call.arguments;
|
|
18
|
+
if (options?.type !== "ObjectExpression") return [];
|
|
19
|
+
return options.properties.filter((property) => isRulesProperty(property));
|
|
20
|
+
}
|
|
21
|
+
function isControllerRules(attribute) {
|
|
22
|
+
const element = attribute.parent;
|
|
23
|
+
return attribute.name.type === "JSXIdentifier" && attribute.name.name === "rules" && element.type === "JSXOpeningElement" && element.name.type === "JSXIdentifier" && element.name.name === "Controller";
|
|
24
|
+
}
|
|
7
25
|
const validationInSchema = defineRule({
|
|
8
26
|
meta: {
|
|
9
27
|
type: "problem",
|
|
10
28
|
docs: { description: "No validation passed to register, useController or a Controller." },
|
|
11
|
-
messages: { outsideSchema
|
|
29
|
+
messages: { outsideSchema },
|
|
12
30
|
schema: []
|
|
13
31
|
},
|
|
14
32
|
create(context) {
|
|
15
33
|
return {
|
|
16
34
|
CallExpression(call) {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
node: call.arguments[1] ?? call,
|
|
20
|
-
messageId: "outsideSchema"
|
|
21
|
-
});
|
|
22
|
-
if (name !== "useController") return;
|
|
23
|
-
const [options] = call.arguments;
|
|
24
|
-
if (options?.type !== "ObjectExpression") return;
|
|
25
|
-
for (const property of options.properties) if (property.type === "Property" && property.key.type === "Identifier" && property.key.name === "rules") context.report({
|
|
26
|
-
node: property,
|
|
35
|
+
for (const node of validationOutsideSchema(call)) context.report({
|
|
36
|
+
node,
|
|
27
37
|
messageId: "outsideSchema"
|
|
28
38
|
});
|
|
29
39
|
},
|
|
30
40
|
JSXAttribute(attribute) {
|
|
31
|
-
|
|
32
|
-
if (attribute.name.type !== "JSXIdentifier" || attribute.name.name !== "rules") return;
|
|
33
|
-
if (element.type !== "JSXOpeningElement" || element.name.type !== "JSXIdentifier" || element.name.name !== "Controller") return;
|
|
34
|
-
context.report({
|
|
41
|
+
if (isControllerRules(attribute)) context.report({
|
|
35
42
|
node: attribute,
|
|
36
43
|
messageId: "outsideSchema"
|
|
37
44
|
});
|
|
@@ -48,7 +55,7 @@ const zodResolver = defineRule({
|
|
|
48
55
|
meta: {
|
|
49
56
|
type: "problem",
|
|
50
57
|
docs: { description: "Every useForm validates through zodResolver." },
|
|
51
|
-
messages: { noSchema
|
|
58
|
+
messages: { noSchema },
|
|
52
59
|
schema: []
|
|
53
60
|
},
|
|
54
61
|
create(context) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brutalway/oxlint-config-react-hook-form",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Oxlint config for react-hook-form: every useForm validates through zodResolver, and no validation is passed to register or a Controller.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"oxlint",
|
|
@@ -34,11 +34,13 @@
|
|
|
34
34
|
"build": "tsdown",
|
|
35
35
|
"typecheck": "tsc -p .",
|
|
36
36
|
"lint": "oxlint --type-aware src oxlint.config.ts tsdown.config.ts",
|
|
37
|
-
"deps": "depcruise src",
|
|
38
|
-
"tree": "check-tree src
|
|
37
|
+
"deps": "depcruise --output-type err-long src",
|
|
38
|
+
"tree": "check-tree src",
|
|
39
39
|
"duplicates": "jscpd src",
|
|
40
|
-
"check": "npm run typecheck && npm run lint && npm run deps && npm run tree && npm run duplicates",
|
|
41
|
-
"test": "node --test 'src/**/*.test.ts'"
|
|
40
|
+
"check": "prettier --check . && npm run typecheck && npm run lint && npm run deps && npm run tree && npm run duplicates && npm run unused",
|
|
41
|
+
"test": "node --test 'src/**/*.test.ts'",
|
|
42
|
+
"format": "prettier --write .",
|
|
43
|
+
"unused": "knip"
|
|
42
44
|
},
|
|
43
45
|
"dependencies": {
|
|
44
46
|
"@oxlint/plugins": "^1.91.0"
|
|
@@ -47,17 +49,35 @@
|
|
|
47
49
|
"oxlint": "^1.91.0"
|
|
48
50
|
},
|
|
49
51
|
"devDependencies": {
|
|
50
|
-
"@brutalway/check-tree": "0.
|
|
51
|
-
"@brutalway/dependency-cruiser-config": "0.
|
|
52
|
-
"@brutalway/lint-utils": "0.
|
|
53
|
-
"@brutalway/oxlint-config-node": "0.2.
|
|
52
|
+
"@brutalway/check-tree": "0.10.0",
|
|
53
|
+
"@brutalway/dependency-cruiser-config": "0.10.0",
|
|
54
|
+
"@brutalway/lint-utils": "0.2.0",
|
|
55
|
+
"@brutalway/oxlint-config-node": "0.2.1",
|
|
54
56
|
"@brutalway/oxlint-config-node-test": "0.1.0",
|
|
55
|
-
"@brutalway/
|
|
57
|
+
"@brutalway/oxlint-config-typescript": "0.10.0",
|
|
58
|
+
"@brutalway/prettier-config": "0.3.0",
|
|
59
|
+
"@brutalway/tsconfig": "0.4.0",
|
|
60
|
+
"@swc/core": "1.16.22",
|
|
61
|
+
"@types/node": "26.6.6",
|
|
56
62
|
"dependency-cruiser": "18.7.2",
|
|
57
|
-
"jscpd": "5.4.
|
|
58
|
-
"
|
|
59
|
-
"oxlint
|
|
60
|
-
"
|
|
63
|
+
"jscpd": "5.4.3",
|
|
64
|
+
"knip": "6.44.2",
|
|
65
|
+
"oxlint": "1.92.0",
|
|
66
|
+
"oxlint-tsgolint": "7.0.2006",
|
|
67
|
+
"prettier": "3.9.10",
|
|
68
|
+
"tsdown": "0.23.7",
|
|
61
69
|
"typescript": "7.0.2"
|
|
70
|
+
},
|
|
71
|
+
"prettier": "@brutalway/prettier-config",
|
|
72
|
+
"check-tree": {
|
|
73
|
+
"rootEntries": true
|
|
74
|
+
},
|
|
75
|
+
"allowScripts": {
|
|
76
|
+
"@swc/core": false
|
|
77
|
+
},
|
|
78
|
+
"overrides": {
|
|
79
|
+
"eslint-plugin-no-comments": {
|
|
80
|
+
"minimatch": "^11.0.1"
|
|
81
|
+
}
|
|
62
82
|
}
|
|
63
83
|
}
|