Skip to content

Commit

Permalink
fix: print all validation errors (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 authored Nov 17, 2022
1 parent 2517ce0 commit 374ff3e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
15 changes: 15 additions & 0 deletions packages/e2e/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,21 @@ rendering chunks...
../dist/page1.html 0.26 KiB"
`;

exports[`Vite Plugin Web Extension > should print validation errors when the manifest is invalid 1`] = `
[Error: Invalid manifest:
[
{
"instancePath": "",
"schemaPath": "#/required",
"keyword": "required",
"params": {
"missingProperty": "manifest_version"
},
"message": "must have required property 'manifest_version'"
}
]]
`;

exports[`Vite Plugin Web Extension > should respect the browser flags, removing fields other than chrome (defaults to chrome) 1`] = `
"vite v2.7.9 building for production...
[vite-plugin-web-extension]
Expand Down
18 changes: 18 additions & 0 deletions packages/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,22 @@ describe("Vite Plugin Web Extension", () => {
},
["dist/16.png", "dist/48.png", "dist/128.png"]
));

it("should print validation errors when the manifest is invalid", async () => {
const build = testBuild({
build: {
outDir: DIST_DIRECTORY,
emptyOutDir: true,
},
publicDir: "extension/assets",
plugins: [
WebExtension({
assets: "extension/assets",
manifest: () => ({}),
}),
],
});

await expect(build).rejects.toMatchSnapshot();
});
});
13 changes: 3 additions & 10 deletions packages/vite-plugin-web-extension/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@ export async function validateManifest(
throw Error(`Manifest must be an object, got ${typeof manifest}`);

const schema = await get(SCHEMA_URL);
const data = ajv.validate(schema, manifest);
if (!data) {
const success = ajv.validate(schema, manifest);
if (!success) {
log(JSON.stringify(manifest, null, 2));
const errors = (ajv.errors ?? [])
?.filter((err) => !!err.instancePath)
.map(
(err) =>
`- manifest${err.instancePath.replace(/\//g, ".")} ${err.message}`
)
.join("\n");
throw Error(`Invalid manifest:\n${errors}`);
throw Error(`Invalid manifest:\n${JSON.stringify(ajv.errors, null, 2)}`);
}
}

Expand Down

0 comments on commit 374ff3e

Please sign in to comment.