Skip to content

Commit

Permalink
fix: all tests and lint errors with ajv v7
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Feb 22, 2021
1 parent c16fc04 commit 559b51f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
22 changes: 11 additions & 11 deletions packages/plugin-manifest-validator/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ describe("validator", () => {
valid: false,
errors: [
{
dataPath: ".version",
dataPath: "",
keyword: "required",
message: "is a required property",
message: "should have required property 'version'",
params: {
missingProperty: "version",
},
Expand All @@ -39,7 +39,7 @@ describe("validator", () => {
valid: false,
errors: [
{
dataPath: ".version",
dataPath: "/version",
keyword: "type",
message: "should be integer",
params: {
Expand All @@ -56,12 +56,12 @@ describe("validator", () => {
valid: false,
errors: [
{
dataPath: ".version",
dataPath: "/version",
keyword: "minimum",
message: "should be >= 1",
params: {
comparison: ">=",
exclusive: false,
// exclusive: false,
limit: 1,
},
schemaPath: "#/properties/version/minimum",
Expand All @@ -75,7 +75,7 @@ describe("validator", () => {
valid: false,
errors: [
{
dataPath: ".type",
dataPath: "/type",
keyword: "enum",
message: "should be equal to one of the allowed values",
params: {
Expand All @@ -92,9 +92,9 @@ describe("validator", () => {
valid: false,
errors: [
{
dataPath: ".description.en",
dataPath: "/description",
keyword: "required",
message: "is a required property",
message: "should have required property 'en'",
params: {
missingProperty: "en",
},
Expand Down Expand Up @@ -164,7 +164,7 @@ describe("validator", () => {
assert(actual.valid === false);
assert(actual.errors?.length === 1);
assert.deepStrictEqual(actual.errors[0], {
dataPath: ".icon",
dataPath: "/icon",
keyword: "maxFileSize",
message: "file size should be <= 20MB",
params: {
Expand All @@ -190,7 +190,7 @@ describe("validator", () => {
assert(actual.valid === false);
assert(actual.errors?.length === 3);
assert.deepStrictEqual(actual.errors[1], {
dataPath: ".desktop.js[0]",
dataPath: "/desktop/js/0",
keyword: "maxFileSize",
message: "file size should be <= 20MB",
params: {
Expand All @@ -216,7 +216,7 @@ describe("validator", () => {
assert(actual.valid === false);
assert(actual.errors?.length === 3);
assert.deepStrictEqual(actual.errors[1], {
dataPath: ".desktop.css[0]",
dataPath: "/desktop/css/0",
keyword: "maxFileSize",
message: "file size should be <= 20MB",
params: {
Expand Down
21 changes: 12 additions & 9 deletions packages/plugin-manifest-validator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

import Ajv from "ajv";
import Ajv, { ErrorObject } from "ajv";
import bytes from "bytes";
import jsonSchema from "../manifest-schema.json";
import validateUrl from "./validate-https-url";

type ValidateResult = {
valid: boolean | PromiseLike<any>;
errors: null | Ajv.ErrorObject[];
errors: null | ErrorObject[];
};

/**
Expand All @@ -29,23 +29,25 @@ export = function (
}
const ajv = new Ajv({
allErrors: true,
unknownFormats: true,
errorDataPath: "property",
// unknownFormats: true,
// errorDataPath: "property",
formats: {
"http-url": (str: string) => validateUrl(str, true),
"https-url": (str: string) => validateUrl(str),
"relative-path": relativePath,
},
});

/*
ajv.removeKeyword("propertyNames");
ajv.removeKeyword("contains");
ajv.removeKeyword("const");
ajv.removeKeyword("if");
ajv.removeKeyword("then");
ajv.removeKeyword("else");
*/

const validateMaxFileSize: Ajv.SchemaValidateFunction = (
const validateMaxFileSize: (...args: any[]) => boolean = (
schema: string,
data: string
) => {
Expand All @@ -54,8 +56,8 @@ export = function (
const maxBytes = bytes.parse(schema);
const valid = maxFileSize(maxBytes, data);
if (!valid) {
// @ts-expect-error
validateMaxFileSize.errors = [
// @ts-expect-error TODO: Ajv.ErrorObject has need fixing.
{
keyword: "maxFileSize",
params: {
Expand All @@ -68,7 +70,8 @@ export = function (
return valid;
};

ajv.addKeyword("maxFileSize", {
ajv.addKeyword({
keyword: "maxFileSize",
validate: validateMaxFileSize,
});

Expand All @@ -82,8 +85,8 @@ export = function (
* @return {null|Array<Object>} shallow copy of the input or null
*/
function transformErrors(
errors: undefined | null | Ajv.ErrorObject[]
): null | Ajv.ErrorObject[] {
errors: undefined | null | ErrorObject[]
): null | ErrorObject[] {
if (!errors) {
return null;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/plugin-packer/test/packer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ describe("packer", () => {
it("throws an error if the contents.zip is invalid", (done) => {
const contentsZip = fs.readFileSync(invalidMaxFileSizeContentsZipPath);
packer(contentsZip).catch((error) => {
expect(
error.message.indexOf('".icon" file size should be <= 20MB')
).not.toBe(-1);
expect(error.message).toBe('"/icon" file size should be <= 20MB');
done();
});
});
Expand Down

0 comments on commit 559b51f

Please sign in to comment.