1. /**
  2. * Surfaces potential data errors for staging environments.
  3. *
  4. * @param {object} data The user data to validate.
  5. * @param {object} options Optional configuration.
  6. * @param {boolean} options.enable Whether to enable validation. Defaults to true.
  7. * @param {string[]} options.requiredFields An array of required field names.
  8. * @param {function} options.customValidation A custom validation function.
  9. * @returns {string[]} An array of error messages. Empty array if no errors.
  10. */
  11. function validateStagingData(data, options = {}) {
  12. const { enable = true, requiredFields = [], customValidation = () => [] } = options;
  13. const errors = [];
  14. if (!enable) {
  15. return errors; // Return empty array if validation is disabled
  16. }
  17. // Required fields validation
  18. for (const field of requiredFields) {
  19. if (!data.hasOwnProperty(field) || data[field] === null || data[field] === undefined || data[field] === "") {
  20. errors.push(`Field "${field}" is required.`);
  21. }
  22. }
  23. // Custom validation
  24. const customErrors = customValidation(data);
  25. errors.push(...customErrors);
  26. return errors;
  27. }

Add your comment