Skip to content

Commit f3c5fa2

Browse files
authored
Fix JSON Schema (#6973)
## What's the problem this PR addresses? `"type": "mixed"` is not valid JSON Schema. This results in errors in features such as VS Code's automatic validation: ``` Schema 'Yarn Config (.yarnrc.yml) - Yarnrc configuration files (yarnrc.json)' is not valid: /properties/httpTimeout/type : must be equal to one of the allowed values /properties/httpTimeout/type : must be array /properties/httpTimeout/type : must match a schema in anyOfYAML(768) ``` Resolves #6972 ## How did you fix it? `type` could be omitted completely (it's redundant with `oneOf`) but is required by the Docusaurus configuration (via https://github.com/arcanis/react-json-doc?), so I added it as an array. To prevent further regressions, I added validation of the JSON Schemas using [AJV](https://ajv.js.org/). If there's a better place for this test to reside, please let me know. ## Checklist <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent b65d527 commit f3c5fa2

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

.pnp.cjs

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/docusaurus/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"build": "docusaurus build",
77
"swizzle": "docusaurus swizzle",
88
"clear": "docusaurus clear",
9-
"serve": "docusaurus serve"
9+
"serve": "docusaurus serve",
10+
"test:schema": "run test:unit packages/docusaurus"
1011
},
1112
"dependencies": {
1213
"@docusaurus/core": "^3.7.0",
@@ -79,6 +80,7 @@
7980
"@docusaurus/types": "^3.7.0",
8081
"@types/dompurify": "^3",
8182
"@types/git-url-parse": "^9.0.0",
83+
"@types/jest": "^28.1.6",
8284
"@types/markdown-it": "^12.2.3",
8385
"@types/marked": "^5.0.0",
8486
"@types/mdast": "^4.0.3",
@@ -91,6 +93,7 @@
9193
"@types/resolve": "^1.20.0",
9294
"@types/semver": "^7.1.0",
9395
"@types/three": "^0.144.0",
96+
"ajv": "^8.17.1",
9497
"clipanion": "^4.0.0-rc.2",
9598
"mdast-util-mdx-jsx": "^3.0.0",
9699
"unified": "^11.0.4"

packages/docusaurus/static/configuration/yarnrc.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
"httpTimeout": {
274274
"_package": "@yarnpkg/core",
275275
"title": "Amount of time to wait before cancelling pending HTTP requests.",
276-
"type": "mixed",
276+
"type": ["number", "string"],
277277
"oneOf": [
278278
{ "type": "number" },
279279
{ "type": "string", "pattern": "^(\\d*\\.?\\d+)(ms|s|m|h|d|w)?$" }
@@ -487,7 +487,7 @@
487487
"_package": "@yarnpkg/core",
488488
"title": "Minimum age of a package version according to the publish date on the npm registry to be considered for installation.",
489489
"description": "If a package version is newer than the minimal age gate, it will not be considered for installation. This can be used to reduce the likelihood of installing compromised packages, or to avoid relying on packages that could still be unpublished (e.g. the npm registry has specific rules for packages less than 3 days old).",
490-
"type": "mixed",
490+
"type": ["number", "string"],
491491
"oneOf": [
492492
{ "type": "number" },
493493
{ "type": "string", "pattern": "^(\\d*\\.?\\d+)(ms|s|m|h|d|w)?$" }
@@ -861,7 +861,7 @@
861861
"_package": "@yarnpkg/core",
862862
"title": "Define the minimal amount of time between two telemetry events.",
863863
"description": "By default we only send one request per week, making it impossible for us to track your usage with a lower granularity.",
864-
"type": "mixed",
864+
"type": ["number", "string"],
865865
"oneOf": [
866866
{ "type": "number" },
867867
{ "type": "string", "pattern": "^(\\d*\\.?\\d+)(ms|s|m|h|d|w)?$" }
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Ajv2019 from 'ajv/dist/2019';
2+
import draft2019Schema from 'ajv/dist/refs/json-schema-2019-09/schema.json';
3+
4+
import manifestSchema from '../static/configuration/manifest.json';
5+
import yarnRcSchema from '../static/configuration/yarnrc.json';
6+
7+
const ajv = new Ajv2019();
8+
const validate = ajv.compile(draft2019Schema);
9+
10+
describe(`manifest.json`, () => {
11+
it(`is a valid JSON Schema`, () => {
12+
const result = validate(manifestSchema);
13+
expect(validate.errors).toBeNull();
14+
expect(result).toBe(true);
15+
});
16+
});
17+
18+
describe(`yarnrc.json`, () => {
19+
it(`is a valid JSON Schema`, () => {
20+
const result = validate(yarnRcSchema);
21+
expect(validate.errors).toBeNull();
22+
expect(result).toBe(true);
23+
});
24+
});

yarn.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5957,6 +5957,7 @@ __metadata:
59575957
"@tanstack/react-query": "npm:^5.74.7"
59585958
"@types/dompurify": "npm:^3"
59595959
"@types/git-url-parse": "npm:^9.0.0"
5960+
"@types/jest": "npm:^28.1.6"
59605961
"@types/markdown-it": "npm:^12.2.3"
59615962
"@types/marked": "npm:^5.0.0"
59625963
"@types/mdast": "npm:^4.0.3"
@@ -5982,6 +5983,7 @@ __metadata:
59825983
"@yarnpkg/pnpify": "workspace:^"
59835984
"@yarnpkg/sdks": "workspace:^"
59845985
"@yarnpkg/shell": "workspace:^"
5986+
ajv: "npm:^8.17.1"
59855987
algoliasearch: "npm:^4.14.2"
59865988
ansi-to-html: "npm:^0.7.2"
59875989
clipanion: "npm:^4.0.0-rc.2"
@@ -6848,7 +6850,7 @@ __metadata:
68486850
languageName: node
68496851
linkType: hard
68506852

6851-
"ajv@npm:^8.0.0, ajv@npm:^8.9.0":
6853+
"ajv@npm:^8.0.0, ajv@npm:^8.17.1, ajv@npm:^8.9.0":
68526854
version: 8.17.1
68536855
resolution: "ajv@npm:8.17.1"
68546856
dependencies:

0 commit comments

Comments
 (0)