Skip to content

Commit

Permalink
added required headers validation & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Oaphi committed Aug 11, 2021
1 parent ae468d6 commit 5e7aa06
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { valid } from "semver";
import validator from "validator";
import { PackageInfo } from "./package";
import { RequiredOnly } from "./types";

export const validateMatchHeaders = (matches: string[]) => {
const validationRegex =
/^((?:https?|file|ftp|\*)(?=:\/\/)|(?:urn(?=:))):(?:\/\/)?(?:((?:\*||.+?)(?=\/|$)))?(\/\*|(?:.+?\*?)+)?|<all_urls>/;
Expand All @@ -8,3 +13,30 @@ export const validateMatchHeaders = (matches: string[]) => {
valid: matches.filter((m) => !invalid.includes(m)),
};
};

export const validateRequiredHeaders = (packageInfo: PackageInfo) => {
const required: (keyof RequiredOnly<PackageInfo>)[] = [
"author",
"name",
"homepage",
"version",
"description",
];
const missing = required.filter((p) => !(p in packageInfo));

const { homepage, version } = packageInfo;

const isValidVersion = !!valid(version);
const isValidHomepage = validator.isURL(homepage);

const status = [isValidVersion, isValidHomepage, !missing.length].reduce(
(a, c) => a && c
);

return {
status,
isValidVersion,
isValidHomepage,
missing,
};
};
45 changes: 44 additions & 1 deletion test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { expect } from "chai";
import { validateMatchHeaders } from "../src/utils/validators";
import { join } from "path";
import { getPackage } from "../src/utils/package";
import {
validateMatchHeaders,
validateRequiredHeaders,
} from "../src/utils/validators";

describe("validators", () => {
const base = process.cwd();
const pkg = join(base, "/package.json");

describe("@match headers validator", () => {
const sampleMatches: string[] = [
"http://*/foo*",
Expand Down Expand Up @@ -31,4 +39,39 @@ describe("validators", () => {
expect(invalid).to.contain(typo);
});
});

describe("required package headers validator", () => {
it("should correctly validate package with required fields present", async () => {
const validPackage = await getPackage(pkg);

const { status, missing } = validateRequiredHeaders(validPackage!);

expect(status).to.be.true;
expect(missing).to.be.empty;
});

it("should correctly validate package with required fields absent", async () => {
const validPackage = await getPackage(pkg);

const testInvalid = { ...validPackage };
delete testInvalid.author;
delete testInvalid.name;
delete testInvalid.description;

//@ts-expect-error
testInvalid.version = "gobblygook5.0";
testInvalid.homepage = "alice in wonderland";

const { status, missing, isValidVersion, isValidHomepage } =
//@ts-expect-error
validateRequiredHeaders(testInvalid);

expect(status).to.be.false;
expect(isValidVersion).to.be.false;
expect(isValidHomepage).to.be.false;
expect(missing).to.contain("author");
expect(missing).to.contain("name");
expect(missing).to.contain("description");
});
});
});

0 comments on commit 5e7aa06

Please # to comment.