basant307/AI_Governance_Project
048
1import { defaultExtendedIconProps } from "../icon/defaults.js";2import { getIconsTree } from "./tree.js";3/**4* Match character5*/6const matchChar = /^[a-f0-9]+(-[a-f0-9]+)*$/;7/**8* Validate icon9*10* Returns name of property that failed validation or null on success11*/12function validateIconProps(item, fix, checkOtherProps) {13 for (const key in item) {14 const attr = key;15 const type = typeof item[attr];16 if (type === "undefined") {17 delete item[attr];18 continue;19 }20 const expectedType = typeof defaultExtendedIconProps[attr];21 if (expectedType !== "undefined") {22 if (type !== expectedType) {23 if (fix) {24 delete item[attr];25 continue;26 }27 return attr;28 }29 continue;30 }31 if (checkOtherProps && type === "object") if (fix) delete item[attr];32 else return key;33 }34 return null;35}36/**37* Validate icon set38* @returns param obj as IconifyJSON type on success, throw error on failure39*/40function validateIconSet(obj, options) {41 const fix = !!(options && options.fix);42 if (typeof obj !== "object" || obj === null || typeof obj.icons !== "object" || !obj.icons) throw new Error("Bad icon set");43 const data = obj;44 if (options && typeof options.prefix === "string") data.prefix = options.prefix;45 else if (typeof data.prefix !== "string" || !data.prefix) throw new Error("Invalid prefix");46 if (options && typeof options.provider === "string") data.provider = options.provider;47 else if (data.provider !== void 0) {48 if (typeof data.provider !== "string") if (fix) delete data.provider;49 else throw new Error("Invalid provider");50 }51 if (data.aliases !== void 0) {52 if (typeof data.aliases !== "object" || data.aliases === null) if (fix) delete data.aliases;53 else throw new Error("Invalid aliases list");54 }55 const tree = getIconsTree(data);56 const icons = data.icons;57 const aliases = data.aliases || Object.create(null);58 for (const name in tree) {59 const treeItem = tree[name];60 const isAlias = !icons[name];61 const parentObj = isAlias ? aliases : icons;62 if (!treeItem) {63 if (fix) {64 delete parentObj[name];65 continue;66 }67 throw new Error(`Invalid alias: ${name}`);68 }69 if (!name) {70 if (fix) {71 delete parentObj[name];72 continue;73 }74 throw new Error(`Invalid icon name: "${name}"`);75 }76 const item = parentObj[name];77 if (!isAlias) {78 if (typeof item.body !== "string") {79 if (fix) {80 delete parentObj[name];81 continue;82 }83 throw new Error(`Invalid icon: "${name}"`);84 }85 }86 const requiredProp = isAlias ? "parent" : "body";87 const key = typeof item[requiredProp] !== "string" ? requiredProp : validateIconProps(item, fix, true);88 if (key !== null) throw new Error(`Invalid property "${key}" in "${name}"`);89 }90 if (data.not_found !== void 0 && !(data.not_found instanceof Array)) if (fix) delete data.not_found;91 else throw new Error("Invalid not_found list");92 if (!Object.keys(data.icons).length && !(data.not_found && data.not_found.length)) throw new Error("Icon set is empty");93 if (fix && !Object.keys(aliases).length) delete data.aliases;94 const failedOptionalProp = validateIconProps(data, false, false);95 if (failedOptionalProp) throw new Error(`Invalid value type for "${failedOptionalProp}"`);96 if (data.chars !== void 0) {97 if (typeof data.chars !== "object" || data.chars === null) if (fix) delete data.chars;98 else throw new Error("Invalid characters map");99 }100 if (typeof data.chars === "object") {101 const chars = data.chars;102 Object.keys(chars).forEach((char) => {103 if (!matchChar.exec(char) || typeof chars[char] !== "string") {104 if (fix) {105 delete chars[char];106 return;107 }108 throw new Error(`Invalid character "${char}"`);109 }110 const target = chars[char];111 if (!data.icons[target] && (!data.aliases || !data.aliases[target])) {112 if (fix) {113 delete chars[char];114 return;115 }116 throw new Error(`Character "${char}" points to missing icon "${target}"`);117 }118 });119 if (fix && !Object.keys(data.chars).length) delete data.chars;120 }121 return data;122}123export { matchChar, validateIconSet };124 