Merge branch 'main' into main

This commit is contained in:
William Harrison
2025-01-13 20:34:02 +11:00
committed by GitHub
2 changed files with 32 additions and 12 deletions
+32 -2
View File
@@ -35,7 +35,7 @@ const reservedDomains = require("../util/reserved-domains.json");
const domainsPath = path.resolve("domains");
const files = fs.readdirSync(domainsPath);
const expandReservedDomains = (reserved) => {
function expandReservedDomains(reserved) {
const expandedList = [...reserved];
reserved.forEach((item) => {
@@ -58,10 +58,30 @@ const expandReservedDomains = (reserved) => {
});
return expandedList;
};
}
const expandedReservedDomains = expandReservedDomains(reservedDomains);
function findDuplicateKeys(jsonString) {
const keyPattern = /"([^"]+)"(?=\s*:)/g;
const keys = [];
let match;
// Find all keys in the JSON string
while ((match = keyPattern.exec(jsonString)) !== null) {
keys.push(match[1]);
}
// Count occurrences of each key
const keyCount = {};
keys.forEach((key) => {
keyCount[key] = (keyCount[key] || 0) + 1;
});
// Return keys that occur more than once
return Object.keys(keyCount).filter((key) => keyCount[key] > 1);
}
function validateFields(t, obj, fields, file, prefix = "") {
Object.keys(fields).forEach((key) => {
const fieldPath = prefix ? `${prefix}.${key}` : key;
@@ -109,6 +129,16 @@ t("All files should be valid JSON", (t) => {
});
});
t("All files should not have duplicate keys", (t) => {
files.forEach((file) => {
// Parse JSON as a string because JS automatically gets the last key if there are duplicates
const rawData = fs.readFileSync(`${domainsPath}/${file}`, "utf8");
const duplicateKeys = findDuplicateKeys(rawData);
t.true(!duplicateKeys.length, `${file}: Duplicate keys found: ${duplicateKeys.join(", ")}`);
});
});
t("All files should have valid file names", (t) => {
files.forEach((file) => {
validateFileName(t, file);
-10
View File
@@ -275,13 +275,3 @@ t("All files should have valid record types", (t) => {
t.pass();
});
t("All files should not have duplicate record keys", (t) => {
files.forEach((file) => {
const data = getDomainData(file);
const recordKeys = Object.keys(data.record);
const uniqueRecordKeys = new Set(recordKeys);
t.is(recordKeys.length, uniqueRecordKeys.size, `${file}: Duplicate record keys found`);
});
});