-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvalidator.ts
More file actions
125 lines (98 loc) · 3.18 KB
/
validator.ts
File metadata and controls
125 lines (98 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { isElement, isTag } from "./helpers/nodeMatchers";
import { Document } from "./types";
const assertIsString = (str: unknown, name: string): string => {
if (typeof str === "undefined") {
throw new TypeError(`Microformats parser: ${name} not provided`);
}
if (typeof str !== "string") {
throw new TypeError(`Microformats parser: ${name} is not a string`);
}
if (str === "") {
throw new TypeError(`Microformats parser: ${name} cannot be empty`);
}
return str;
};
const assertIsBoolean = (bool: unknown, name: string): boolean => {
if (typeof bool !== "boolean") {
throw new TypeError(`Microformats parser: ${name} is not a boolean`);
}
return bool;
};
const assertIsObject = (
obj: unknown,
allowedKeys: string[],
name: string,
): Record<string, unknown> => {
if (typeof obj === "undefined") {
throw new TypeError(`Microformats parser: ${name} is not provided`);
}
if (typeof obj !== "object") {
throw new TypeError(`Microformats parser: ${name} is not an object`);
}
if (Array.isArray(obj)) {
throw new TypeError(`Microformats parser: ${name} is not an object`);
}
if (obj === null) {
throw new TypeError(`Microformats parser: ${name} cannot be null`);
}
const unknownKeys = Object.keys(obj).filter(
(key) => !allowedKeys.includes(key),
);
if (unknownKeys.length) {
throw new TypeError(
`Microformats parser: ${name} contains unknown properties: ${unknownKeys.join(
", ",
)}`,
);
}
return obj as Record<string, unknown>;
};
export const validator = (
unknownHtml: unknown,
unknownOptions: unknown,
): void => {
assertIsString(unknownHtml, "HTML");
const options = assertIsObject(
unknownOptions,
["baseUrl", "experimental"],
"options",
);
const baseUrl = assertIsString(options.baseUrl, "baseUrl");
// verify the url provided is valid
new URL(baseUrl);
if ("experimental" in options) {
const experimental = assertIsObject(
options.experimental,
["lang", "textContent", "metaformats", "authorship"],
"experimental",
);
if ("lang" in experimental) {
assertIsBoolean(experimental.lang, "experimental.lang");
}
if ("textContent" in experimental) {
assertIsBoolean(experimental.textContent, "experimental.textContent");
}
if ("metaformats" in experimental) {
assertIsBoolean(experimental.metaformats, "experimental.metaformats");
}
if ("authorship" in experimental) {
assertIsBoolean(experimental.authorship, "experimental.authorship");
}
}
};
export const validateParsedHtml = (doc: Document): void => {
// <html> and <body> are always defined (based on tests)
// Provide error handling in the event they are ever not defined
const html = doc.childNodes.find(isTag("html"));
if (!html) {
throw new Error("Microformats parser: No <html> element found");
}
const body = html.childNodes.find(isTag("body"));
if (!body) {
throw new Error("Microformats parser: No <body> element found");
}
// if we have no body children, it's the result of invalid HTML
if (!body.childNodes.filter(isElement).length) {
throw new Error("Microformats parser: unable to parse HTML");
}
};