validate-basic.mjs 1.6 KB

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