validate.mjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { defaultExtendedIconProps } from '../icon/defaults.mjs';
  2. import { getIconsTree } from './tree.mjs';
  3. const matchChar = /^[a-f0-9]+(-[a-f0-9]+)*$/;
  4. function validateIconProps(item, fix, checkOtherProps) {
  5. for (const key in item) {
  6. const attr = key;
  7. const value = item[attr];
  8. const type = typeof value;
  9. if (type === "undefined") {
  10. delete item[attr];
  11. continue;
  12. }
  13. const expectedType = typeof defaultExtendedIconProps[attr];
  14. if (expectedType !== "undefined") {
  15. if (type !== expectedType) {
  16. if (fix) {
  17. delete item[attr];
  18. continue;
  19. }
  20. return attr;
  21. }
  22. continue;
  23. }
  24. if (checkOtherProps && type === "object") {
  25. if (fix) {
  26. delete item[attr];
  27. } else {
  28. return key;
  29. }
  30. }
  31. }
  32. return null;
  33. }
  34. function validateIconSet(obj, options) {
  35. const fix = !!(options && options.fix);
  36. if (typeof obj !== "object" || obj === null || typeof obj.icons !== "object" || !obj.icons) {
  37. throw new Error("Bad icon set");
  38. }
  39. const data = obj;
  40. if (options && typeof options.prefix === "string") {
  41. data.prefix = options.prefix;
  42. } else if (
  43. // Prefix must be a string and not empty
  44. typeof data.prefix !== "string" || !data.prefix
  45. ) {
  46. throw new Error("Invalid prefix");
  47. }
  48. if (options && typeof options.provider === "string") {
  49. data.provider = options.provider;
  50. } else if (data.provider !== void 0) {
  51. const value = data.provider;
  52. if (typeof value !== "string") {
  53. if (fix) {
  54. delete data.provider;
  55. } else {
  56. throw new Error("Invalid provider");
  57. }
  58. }
  59. }
  60. if (data.aliases !== void 0) {
  61. if (typeof data.aliases !== "object" || data.aliases === null) {
  62. if (fix) {
  63. delete data.aliases;
  64. } else {
  65. throw new Error("Invalid aliases list");
  66. }
  67. }
  68. }
  69. const tree = getIconsTree(data);
  70. const icons = data.icons;
  71. const aliases = data.aliases || /* @__PURE__ */ Object.create(null);
  72. for (const name in tree) {
  73. const treeItem = tree[name];
  74. const isAlias = !icons[name];
  75. const parentObj = isAlias ? aliases : icons;
  76. if (!treeItem) {
  77. if (fix) {
  78. delete parentObj[name];
  79. continue;
  80. }
  81. throw new Error(`Invalid alias: ${name}`);
  82. }
  83. if (!name) {
  84. if (fix) {
  85. delete parentObj[name];
  86. continue;
  87. }
  88. throw new Error(`Invalid icon name: "${name}"`);
  89. }
  90. const item = parentObj[name];
  91. if (!isAlias) {
  92. if (typeof item.body !== "string") {
  93. if (fix) {
  94. delete parentObj[name];
  95. continue;
  96. }
  97. throw new Error(`Invalid icon: "${name}"`);
  98. }
  99. }
  100. const requiredProp = isAlias ? "parent" : "body";
  101. const key = typeof item[requiredProp] !== "string" ? requiredProp : validateIconProps(item, fix, true);
  102. if (key !== null) {
  103. throw new Error(`Invalid property "${key}" in "${name}"`);
  104. }
  105. }
  106. if (data.not_found !== void 0 && !(data.not_found instanceof Array)) {
  107. if (fix) {
  108. delete data.not_found;
  109. } else {
  110. throw new Error("Invalid not_found list");
  111. }
  112. }
  113. if (!Object.keys(data.icons).length && !(data.not_found && data.not_found.length)) {
  114. throw new Error("Icon set is empty");
  115. }
  116. if (fix && !Object.keys(aliases).length) {
  117. delete data.aliases;
  118. }
  119. const failedOptionalProp = validateIconProps(data, false, false);
  120. if (failedOptionalProp) {
  121. throw new Error(`Invalid value type for "${failedOptionalProp}"`);
  122. }
  123. if (data.chars !== void 0) {
  124. if (typeof data.chars !== "object" || data.chars === null) {
  125. if (fix) {
  126. delete data.chars;
  127. } else {
  128. throw new Error("Invalid characters map");
  129. }
  130. }
  131. }
  132. if (typeof data.chars === "object") {
  133. const chars = data.chars;
  134. Object.keys(chars).forEach((char) => {
  135. if (!matchChar.exec(char) || typeof chars[char] !== "string") {
  136. if (fix) {
  137. delete chars[char];
  138. return;
  139. }
  140. throw new Error(`Invalid character "${char}"`);
  141. }
  142. const target = chars[char];
  143. if (!data.icons[target] && (!data.aliases || !data.aliases[target])) {
  144. if (fix) {
  145. delete chars[char];
  146. return;
  147. }
  148. throw new Error(
  149. `Character "${char}" points to missing icon "${target}"`
  150. );
  151. }
  152. });
  153. if (fix && !Object.keys(data.chars).length) {
  154. delete data.chars;
  155. }
  156. }
  157. return data;
  158. }
  159. export { matchChar, validateIconSet };