validatePrimaryOption.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const _ = require('lodash');
  2. module.exports = function validatePrimaryOption(actualOptions) {
  3. // Begin checking array options
  4. if (!Array.isArray(actualOptions)) {
  5. return false;
  6. }
  7. // Every item in the array must be a string or an object
  8. // with a "properties" property
  9. if (
  10. !actualOptions.every((item) => {
  11. if (_.isString(item)) {
  12. return true;
  13. }
  14. return _.isPlainObject(item) && !_.isUndefined(item.properties);
  15. })
  16. ) {
  17. return false;
  18. }
  19. const objectItems = actualOptions.filter(_.isPlainObject);
  20. // Every object-item's "properties" should be an array with no items, or with strings
  21. if (
  22. !objectItems.every((item) => {
  23. if (!Array.isArray(item.properties)) {
  24. return false;
  25. }
  26. return item.properties.every((property) => _.isString(property));
  27. })
  28. ) {
  29. return false;
  30. }
  31. // Every object-item's "emptyLineBefore" must be "always" or "never"
  32. if (
  33. !objectItems.every((item) => {
  34. if (_.isUndefined(item.emptyLineBefore)) {
  35. return true;
  36. }
  37. return ['always', 'never', 'threshold'].includes(item.emptyLineBefore);
  38. })
  39. ) {
  40. return false;
  41. }
  42. // Every object-item's "noEmptyLineBetween" must be a boolean
  43. if (
  44. !objectItems.every((item) => {
  45. if (_.isUndefined(item.noEmptyLineBetween)) {
  46. return true;
  47. }
  48. return _.isBoolean(item.noEmptyLineBetween);
  49. })
  50. ) {
  51. return false;
  52. }
  53. return true;
  54. };