Skip to content

Commit

Permalink
Use 'anyOf' in schema and add to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkarRuikar committed Apr 27, 2023
1 parent cd41efc commit b8c2eba
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 20 deletions.
73 changes: 53 additions & 20 deletions front-matter-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
}
}
},
"allOf": [
"anyOf": [
{
"if": {
"properties": {
Expand All @@ -67,7 +67,8 @@
"enum": ["glossary-definition", "glossary-disambiguation"]
}
}
}
},
"else": false
},
{
"if": {
Expand All @@ -83,7 +84,8 @@
]
}
}
}
},
"else": false
},
{
"if": {
Expand All @@ -95,7 +97,8 @@
"enum": ["guide", "landing-page", "svg-attribute", "svg-element"]
}
}
}
},
"else": false
},
{
"if": {
Expand All @@ -115,7 +118,8 @@
]
}
}
}
},
"else": false
},
{
"if": {
Expand All @@ -141,7 +145,8 @@
]
}
}
}
},
"else": false
},
{
"if": {
Expand Down Expand Up @@ -169,7 +174,8 @@
]
}
}
}
},
"else": false
},
{
"if": {
Expand Down Expand Up @@ -197,7 +203,8 @@
]
}
}
}
},
"else": false
},
{
"if": {
Expand Down Expand Up @@ -229,7 +236,8 @@
]
}
}
}
},
"else": false
},
{
"if": {
Expand All @@ -242,7 +250,8 @@
},
"then": {
"properties": { "page-type": { "enum": ["http-csp-directive"] } }
}
},
"else": false
},
{
"if": {
Expand All @@ -257,20 +266,22 @@
"properties": {
"page-type": { "enum": ["http-permissions-policy-directive"] }
}
}
},
"else": false
},
{
"if": {
"properties": {
"slug": {
"type": "string",
"pattern": "^Web/HTTP/Headers/(?!Content-Security-Policy|Permissions-Policy)"
"pattern": "^Web/HTTP/Headers/"
}
}
},
"then": {
"properties": { "page-type": { "enum": ["guide", "http-header"] } }
}
},
"else": false
},
{
"if": {
Expand All @@ -280,7 +291,8 @@
},
"then": {
"properties": { "page-type": { "enum": ["http-method"] } }
}
},
"else": false
},
{
"if": {
Expand All @@ -290,7 +302,8 @@
},
"then": {
"properties": { "page-type": { "enum": ["http-status-code"] } }
}
},
"else": false
},
{
"if": {
Expand All @@ -300,7 +313,8 @@
},
"then": {
"properties": { "page-type": { "enum": ["mathml-element"] } }
}
},
"else": false
},
{
"if": {
Expand All @@ -313,7 +327,8 @@
},
"then": {
"properties": { "page-type": { "enum": ["mathml-attribute"] } }
}
},
"else": false
},
{
"if": {
Expand All @@ -326,7 +341,8 @@
},
"then": {
"properties": { "page-type": { "enum": ["aria-attribute"] } }
}
},
"else": false
},
{
"if": {
Expand All @@ -339,7 +355,8 @@
},
"then": {
"properties": { "page-type": { "enum": ["aria-role"] } }
}
},
"else": false
},
{
"if": {
Expand All @@ -349,7 +366,23 @@
},
"then": {
"properties": { "page-type": { "enum": ["http-cors-error"] } }
}
},
"else": false
},
{
"if": {
"properties": {
"slug": { "type": "string", "pattern": ".*" }
}
},
"then": {
"properties": {
"page-type": {
"enum": ["guide", "landing-page"]
}
}
},
"else": false
}
]
},
Expand Down
21 changes: 21 additions & 0 deletions tests/front-matter_linter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,25 @@ describe("Test front-matter linter", () => {
"property 'title' must not have more than 120 characters";
await expect(result).toEqual([expected, null, validContent]);
});

it("should flag and remove unknown attribute", async () => {
const filePath = fileURLToPath(
new URL("./unknown_attribute.md", SAMPLES_DIRECTORY)
);
const validPath = fileURLToPath(
new URL("./unknown_attribute.valid.md", SAMPLES_DIRECTORY)
);
const validContent = fs.readFileSync(validPath, "utf-8");

options.fix = false;
let result = await checkFrontMatter(filePath, options);
const expected =
"Error: tests/front-matter_test_files/unknown_attribute.md\n" +
"'tags' property is not expected to be here";
await expect(result).toEqual([expected, null, null]);

options.fix = true;
result = await checkFrontMatter(filePath, options);
await expect(result).toEqual([expected, null, validContent]);
});
});
11 changes: 11 additions & 0 deletions tests/front-matter_test_files/unknown_attribute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: some api method()
slug: Web/api/method
tags:
- api
- method
- experimental
browser-compat: api.method
---

Content
7 changes: 7 additions & 0 deletions tests/front-matter_test_files/unknown_attribute.valid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: some api method()
slug: Web/api/method
browser-compat: api.method
---

Content

0 comments on commit b8c2eba

Please sign in to comment.