validatePrimaryOption.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const _ = require('lodash');
  2. module.exports = function validatePrimaryOption(actualOptions) {
  3. // Otherwise, begin checking array options
  4. if (!Array.isArray(actualOptions)) {
  5. return false;
  6. }
  7. // Every item in the array must be a certain string or an object
  8. // with a "type" property
  9. if (
  10. !actualOptions.every((item) => {
  11. if (_.isString(item)) {
  12. return [
  13. 'custom-properties',
  14. 'dollar-variables',
  15. 'at-variables',
  16. 'declarations',
  17. 'rules',
  18. 'at-rules',
  19. 'less-mixins',
  20. ].includes(item);
  21. }
  22. return _.isPlainObject(item) && !_.isUndefined(item.type);
  23. })
  24. ) {
  25. return false;
  26. }
  27. const objectItems = actualOptions.filter(_.isPlainObject);
  28. if (
  29. !objectItems.every((item) => {
  30. let result = true;
  31. if (item.type !== 'at-rule' && item.type !== 'rule') {
  32. return false;
  33. }
  34. if (item.type === 'at-rule') {
  35. // if parameter is specified, name should be specified also
  36. if (!_.isUndefined(item.parameter) && _.isUndefined(item.name)) {
  37. return false;
  38. }
  39. if (!_.isUndefined(item.hasBlock)) {
  40. result = item.hasBlock === true || item.hasBlock === false;
  41. }
  42. if (!_.isUndefined(item.name)) {
  43. result = _.isString(item.name) && item.name.length;
  44. }
  45. if (!_.isUndefined(item.parameter)) {
  46. result =
  47. (_.isString(item.parameter) && item.parameter.length) ||
  48. _.isRegExp(item.parameter);
  49. }
  50. }
  51. if (item.type === 'rule') {
  52. if (!_.isUndefined(item.selector)) {
  53. result =
  54. (_.isString(item.selector) && item.selector.length) ||
  55. _.isRegExp(item.selector);
  56. }
  57. if (result && !_.isUndefined(item.name)) {
  58. result = _.isString(item.name) && item.name.length;
  59. }
  60. }
  61. return result;
  62. })
  63. ) {
  64. return false;
  65. }
  66. return true;
  67. };