File size: 7,789 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { isNotEmptyObject, type ValidationError } from 'class-validator';

import { AggregateChartConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/aggregate-chart-configuration.dto';
import { BarChartConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/bar-chart-configuration.dto';
import { FieldConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/field-configuration.dto';
import { FrontComponentConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/front-component-configuration.dto';
import { IframeConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/iframe-configuration.dto';
import { LineChartConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/line-chart-configuration.dto';
import { PieChartConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/pie-chart-configuration.dto';
import { RecordTableConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/record-table-configuration.dto';
import { StandaloneRichTextConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/standalone-rich-text-configuration.dto';
import { WidgetConfigurationType } from 'src/engine/metadata-modules/page-layout-widget/enums/widget-configuration-type.type';
import {
  PageLayoutWidgetException,
  PageLayoutWidgetExceptionCode,
} from 'src/engine/metadata-modules/page-layout-widget/exceptions/page-layout-widget.exception';
import { validateWidgetConfigurationByDto } from 'src/engine/metadata-modules/page-layout-widget/utils/validate-widget-configuration-by-dto.util';

const formatValidationErrors = (
  errors: ValidationError[],
  parentProperty?: string,
): string => {
  return errors
    .map((err) => {
      const propertyPath = parentProperty
        ? `${parentProperty}.${err.property}`
        : err.property;

      if (err.constraints) {
        const constraints = Object.values(err.constraints).join(', ');

        return `${propertyPath}: ${constraints}`;
      }

      if (err.children && err.children.length > 0) {
        return formatValidationErrors(err.children, propertyPath);
      }

      return `${propertyPath}: Unknown error`;
    })
    .join('; ');
};

export const validateWidgetConfigurationInput = ({
  configuration,
}: {
  configuration: unknown;
}): void => {
  if (!isNotEmptyObject(configuration)) {
    throw new PageLayoutWidgetException(
      'Invalid configuration: not an object',
      PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
    );
  }

  const configurationRecord = configuration as Record<string, unknown>;

  if (
    !Object.prototype.hasOwnProperty.call(
      configurationRecord,
      'configurationType',
    )
  ) {
    throw new PageLayoutWidgetException(
      'Invalid configuration: missing configuration type',
      PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
    );
  }

  const configurationType =
    configurationRecord.configurationType as WidgetConfigurationType;

  let errors: ValidationError[] = [];

  switch (configurationType) {
    case WidgetConfigurationType.BAR_CHART:
      errors = validateWidgetConfigurationByDto(
        BarChartConfigurationDTO,
        configuration,
      );
      break;
    case WidgetConfigurationType.LINE_CHART:
      errors = validateWidgetConfigurationByDto(
        LineChartConfigurationDTO,
        configuration,
      );
      break;
    case WidgetConfigurationType.PIE_CHART:
      errors = validateWidgetConfigurationByDto(
        PieChartConfigurationDTO,
        configuration,
      );
      break;
    case WidgetConfigurationType.AGGREGATE_CHART:
      errors = validateWidgetConfigurationByDto(
        AggregateChartConfigurationDTO,
        configuration,
      );
      break;
    case WidgetConfigurationType.IFRAME:
      errors = validateWidgetConfigurationByDto(
        IframeConfigurationDTO,
        configuration,
      );
      break;
    case WidgetConfigurationType.STANDALONE_RICH_TEXT:
      errors = validateWidgetConfigurationByDto(
        StandaloneRichTextConfigurationDTO,
        configuration,
      );
      break;
    case WidgetConfigurationType.FRONT_COMPONENT:
      errors = validateWidgetConfigurationByDto(
        FrontComponentConfigurationDTO,
        configuration,
      );
      break;
    case WidgetConfigurationType.RECORD_TABLE:
      errors = validateWidgetConfigurationByDto(
        RecordTableConfigurationDTO,
        configuration,
      );
      break;
    case WidgetConfigurationType.VIEW:
      throw new PageLayoutWidgetException(
        'View configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.FIELD:
      errors = validateWidgetConfigurationByDto(
        FieldConfigurationDTO,
        configuration,
      );
      break;
    case WidgetConfigurationType.FIELDS:
      throw new PageLayoutWidgetException(
        'Fields configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.TIMELINE:
      throw new PageLayoutWidgetException(
        'Timeline configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.TASKS:
      throw new PageLayoutWidgetException(
        'Tasks configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.NOTES:
      throw new PageLayoutWidgetException(
        'Notes configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.FILES:
      throw new PageLayoutWidgetException(
        'Files configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.EMAILS:
      throw new PageLayoutWidgetException(
        'Emails configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.CALENDAR:
      throw new PageLayoutWidgetException(
        'Calendar configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.FIELD_RICH_TEXT:
      throw new PageLayoutWidgetException(
        'Field rich text configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.WORKFLOW:
      throw new PageLayoutWidgetException(
        'Workflow configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.WORKFLOW_VERSION:
      throw new PageLayoutWidgetException(
        'Workflow version configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    case WidgetConfigurationType.WORKFLOW_RUN:
      throw new PageLayoutWidgetException(
        'Workflow run configuration is not supported yet',
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
    default:
      throw new PageLayoutWidgetException(
        `Invalid configuration type: ${configurationType}`,
        PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
      );
  }

  if (errors.length > 0) {
    throw new PageLayoutWidgetException(
      formatValidationErrors(errors),
      PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
    );
  }
};