validate-basic.cjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. const icon_defaults = require('../icon/defaults.cjs');
  3. const optionalPropertyDefaults = {
  4. provider: "",
  5. aliases: {},
  6. not_found: {},
  7. ...icon_defaults.defaultIconDimensions
  8. };
  9. function checkOptionalProps(item, defaults) {
  10. for (const prop in defaults) {
  11. if (prop in item && typeof item[prop] !== typeof defaults[prop]) {
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. function quicklyValidateIconSet(obj) {
  18. if (typeof obj !== "object" || obj === null) {
  19. return null;
  20. }
  21. const data = obj;
  22. if (typeof data.prefix !== "string" || !obj.icons || typeof obj.icons !== "object") {
  23. return null;
  24. }
  25. if (!checkOptionalProps(obj, optionalPropertyDefaults)) {
  26. return null;
  27. }
  28. const icons = data.icons;
  29. for (const name in icons) {
  30. const icon = icons[name];
  31. if (
  32. // Name cannot be empty
  33. !name || // Must have body
  34. typeof icon.body !== "string" || // Check other props
  35. !checkOptionalProps(
  36. icon,
  37. icon_defaults.defaultExtendedIconProps
  38. )
  39. ) {
  40. return null;
  41. }
  42. }
  43. const aliases = data.aliases || /* @__PURE__ */ Object.create(null);
  44. for (const name in aliases) {
  45. const icon = aliases[name];
  46. const parent = icon.parent;
  47. if (
  48. // Name cannot be empty
  49. !name || // Parent must be set and point to existing icon
  50. typeof parent !== "string" || !icons[parent] && !aliases[parent] || // Check other props
  51. !checkOptionalProps(
  52. icon,
  53. icon_defaults.defaultExtendedIconProps
  54. )
  55. ) {
  56. return null;
  57. }
  58. }
  59. return data;
  60. }
  61. exports.quicklyValidateIconSet = quicklyValidateIconSet;