Skip to content

Commit 82a844c

Browse files
authored
feat(valid-contributors): add new rule for validating contributors (#1387)
<!-- πŸ‘‹ Hi, thanks for sending a PR to eslint-plugin-package-json! πŸ—‚ Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #1372 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/blob/main/.github/CONTRIBUTING.md) were taken ## Overview This change adds a new `valid-contributors` rule that uses `validateContributors` from package-json-validator to identify errors. It's included in the `recommended` config.
1 parent c26616a commit 82a844c

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ The default settings don't conflict, and Prettier plugins can quickly fix up ord
215215
| [valid-bin](docs/rules/valid-bin.md) | Enforce that the `bin` property is valid. | βœ”οΈ βœ… | | | |
216216
| [valid-bundleDependencies](docs/rules/valid-bundleDependencies.md) | Enforce that the `bundleDependencies` (also: `bundledDependencies`) property is valid. | βœ”οΈ βœ… | | | |
217217
| [valid-config](docs/rules/valid-config.md) | Enforce that the `config` property is valid. | βœ”οΈ βœ… | | | |
218+
| [valid-contributors](docs/rules/valid-contributors.md) | Enforce that the `contributors` property is valid. | βœ”οΈ βœ… | | | |
218219
| [valid-cpu](docs/rules/valid-cpu.md) | Enforce that the `cpu` property is valid. | βœ”οΈ βœ… | | | |
219220
| [valid-dependencies](docs/rules/valid-dependencies.md) | Enforce that the `dependencies` property is valid. | βœ”οΈ βœ… | | | |
220221
| [valid-description](docs/rules/valid-description.md) | Enforce that the `description` property is valid. | βœ”οΈ βœ… | | | |
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# valid-contributors
2+
3+
πŸ’Ό This rule is enabled in the following configs: βœ”οΈ `legacy-recommended`, βœ… `recommended`.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
This rule does the following checks on the value of the `contributors` property:
8+
9+
- It must be an array of objects.
10+
- Each object should have at least a `name`, and optionally `email` and `url`.
11+
- `email` and `url`, if present, should be valid email and url formats.
12+
13+
Example of **incorrect** code for this rule:
14+
15+
```json
16+
{
17+
"contributors": "Trent Reznor"
18+
}
19+
```
20+
21+
Example of **correct** code for this rule:
22+
23+
```json
24+
{
25+
"contributors": [
26+
{
27+
"name": "Trent Reznor",
28+
"email": "treznor@nin.com",
29+
"url": "https://nin.com"
30+
}
31+
]
32+
}
33+
```

β€Žsrc/rules/valid-properties.tsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
validateBin,
44
validateBundleDependencies,
55
validateConfig,
6+
validateContributors,
67
validateCpu,
78
validateDependencies,
89
validateDescription,
@@ -42,6 +43,7 @@ const properties = [
4243
},
4344
],
4445
["config", validateConfig],
46+
["contributors", validateContributors],
4547
["cpu", validateCpu],
4648
["description", validateDescription],
4749
["dependencies", validateDependencies],
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import { rules } from "../../rules/valid-properties.ts";
2+
import { ruleTester } from "./ruleTester.ts";
3+
4+
ruleTester.run("valid-contributors", rules["valid-contributors"], {
5+
invalid: [
6+
{
7+
code: `{
8+
"contributors": null
9+
}
10+
`,
11+
errors: [
12+
{
13+
data: {
14+
error: "the type should be an `Array` of objects with at least a `name` property, and optionally `email` and `url`",
15+
},
16+
line: 2,
17+
messageId: "validationError",
18+
},
19+
],
20+
},
21+
{
22+
code: `{
23+
"contributors": 123
24+
}
25+
`,
26+
errors: [
27+
{
28+
data: {
29+
error: "the type should be an `Array` of objects with at least a `name` property, and optionally `email` and `url`",
30+
},
31+
line: 2,
32+
messageId: "validationError",
33+
},
34+
],
35+
},
36+
{
37+
code: `{
38+
"contributors": "./script.js"
39+
}
40+
`,
41+
errors: [
42+
{
43+
data: {
44+
error: "the type should be an `Array` of objects with at least a `name` property, and optionally `email` and `url`",
45+
},
46+
line: 2,
47+
messageId: "validationError",
48+
},
49+
],
50+
},
51+
{
52+
code: `{
53+
"contributors": {}
54+
}
55+
`,
56+
errors: [
57+
{
58+
data: {
59+
error: "the type should be an `Array` of objects with at least a `name` property, and optionally `email` and `url`",
60+
},
61+
line: 2,
62+
messageId: "validationError",
63+
},
64+
],
65+
},
66+
{
67+
code: `{
68+
"contributors": [
69+
"string",
70+
true,
71+
123,
72+
{},
73+
[]
74+
]
75+
}
76+
`,
77+
errors: [
78+
{
79+
data: {
80+
error: "item 0 is invalid; it should be a person object with at least a `name`",
81+
},
82+
line: 3,
83+
messageId: "validationError",
84+
},
85+
{
86+
data: {
87+
error: "item 1 is invalid; it should be a person object with at least a `name`",
88+
},
89+
line: 4,
90+
messageId: "validationError",
91+
},
92+
{
93+
data: {
94+
error: "item 2 is invalid; it should be a person object with at least a `name`",
95+
},
96+
line: 5,
97+
messageId: "validationError",
98+
},
99+
{
100+
data: {
101+
error: "item 3 is invalid; it should be a person object with at least a `name`",
102+
},
103+
line: 6,
104+
messageId: "validationError",
105+
},
106+
{
107+
data: {
108+
error: "item 4 is invalid; it should be a person object with at least a `name`",
109+
},
110+
line: 7,
111+
messageId: "validationError",
112+
},
113+
],
114+
},
115+
{
116+
code: `{
117+
"contributors": [
118+
{
119+
"name": "",
120+
"email": "barney",
121+
"url": "rubble"
122+
},
123+
{
124+
"name": "fred",
125+
"email": "flintstone",
126+
"web": ".com"
127+
}
128+
]
129+
}
130+
`,
131+
errors: [
132+
{
133+
data: {
134+
error: "name should not be empty",
135+
},
136+
line: 4,
137+
messageId: "validationError",
138+
},
139+
{
140+
data: {
141+
error: "email is not valid: barney",
142+
},
143+
line: 5,
144+
messageId: "validationError",
145+
},
146+
{
147+
data: {
148+
error: "url is not valid: rubble",
149+
},
150+
line: 6,
151+
messageId: "validationError",
152+
},
153+
{
154+
data: {
155+
error: "email is not valid: flintstone",
156+
},
157+
line: 10,
158+
messageId: "validationError",
159+
},
160+
{
161+
data: {
162+
error: "url is not valid: .com",
163+
},
164+
line: 11,
165+
messageId: "validationError",
166+
},
167+
],
168+
},
169+
],
170+
valid: [
171+
"{}",
172+
`{ "contributors": [] }`,
173+
`{ "contributors": [
174+
{
175+
"name": "Fred Flintston",
176+
"email": "f@flintstone.com",
177+
"web": "https://flintstone.com"
178+
}
179+
] }`,
180+
],
181+
});

0 commit comments

Comments
Β (0)