validation.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import { FormRequiredFields, Report, StepKey } from '../../types';
  2. const arrayFieldIsValid = (field: Report[keyof Report]) => {
  3. if (Array.isArray(field)) {
  4. return field?.length;
  5. }
  6. return true;
  7. };
  8. export const getMissingFields = (report: Report, id?: StepKey) => {
  9. const requiredFields: FormRequiredFields = [
  10. { step: StepKey.SelectDashboard, fields: ['dashboards'] },
  11. { step: StepKey.FormatReport, fields: ['formats'] },
  12. { step: StepKey.Share, fields: ['name', 'recipients'] },
  13. ];
  14. // Validate dashboards separately
  15. const hasMissingDashboard = !report.dashboards?.[0]?.dashboard?.name;
  16. if (!id) {
  17. // Validate all fields
  18. const requiredFieldNames = requiredFields.flatMap((field) => field.fields);
  19. return (
  20. requiredFieldNames?.some((field) => !report[field] || !arrayFieldIsValid(report[field])) || hasMissingDashboard
  21. );
  22. }
  23. if (id === StepKey.SelectDashboard) {
  24. return hasMissingDashboard;
  25. }
  26. const requiredFieldNames = requiredFields.find((field) => field.step === id)?.fields;
  27. return requiredFieldNames?.some((field) => !report[field] || !arrayFieldIsValid(report[field])); // Validate array fields also for display in stepper
  28. };