-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.js
105 lines (93 loc) · 2.69 KB
/
validate.js
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
import { schema } from "@uniswap/token-lists";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import fetch from "node-fetch";
import fs from "fs/promises";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { createLogger, format, transports } from "winston";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const logger = createLogger({
level: "info",
format: format.combine(format.timestamp(), format.json()),
defaultMeta: { service: "token-list-validator" },
transports: [
new transports.Console({ level: "info", format: format.simple() }),
new transports.File({ filename: "error.log", level: "error" }),
],
});
const ajv = new Ajv({ allErrors: true, verbose: true });
addFormats(ajv);
const validator = ajv.compile(schema);
const isUrl = (source) =>
source.startsWith("http://") || source.startsWith("https://");
const fetchData = async (url) => {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Failed to fetch data from ${url}. Status: ${response.status}`
);
}
return response.json();
} catch (error) {
logger.error(`Failed to fetch data from ${url}. Error: ${error.message}`);
throw error;
}
};
const readData = async (filePath) => {
try {
const absolutePath = join(__dirname, filePath);
const data = await fs.readFile(absolutePath, "utf8");
return JSON.parse(data);
} catch (error) {
logger.error(
`Failed to read data from ${filePath}. Error: ${error.message}`
);
throw error;
}
};
const getData = async (source) =>
isUrl(source) ? fetchData(source) : readData(source);
const validate = async (data) => {
const valid = validator(data);
if (valid) return valid;
if (validator.errors) {
const errors = validator.errors.map(
({ dataPath, keyword, message, params, schemaPath }) => ({
dataPath,
keyword,
message,
params,
schemaPath,
})
);
const errorMessage = `Validation failed: ${JSON.stringify(
errors,
null,
2
)}`;
logger.error(errorMessage);
throw new Error(errorMessage);
}
};
const main = async () => {
try {
const source = process.argv[2];
if (!source) {
throw new Error("Usage: node validate.js <source>");
}
logger.info(`Validating token list from source: ${source}`);
const data = await getData(source);
await validate(data);
logger.info("Token list is valid.");
} catch (error) {
logger.error(`Validation failed: ${error.message}`);
process.exit(1);
}
};
if (import.meta.url === `file://${process.argv[1]}`) {
main();
}
export { getData, validate };