Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import Registry from "../Registry";
import messages from "../../utils/messages";
 
export const errorBuilder = {
  /**
   * Occurrs when attribute is not provided at all
   */
  required(modelName, field) {
    return {
      modelName,
      field,
      error: "ERR_REQUIRED",
    };
  },
 
  /**
   * Occurrs when tag is not in our Registry
   */
  unknownTag(modelName, field, value) {
    return {
      modelName,
      field,
      value,
      error: "ERR_UNKNOWN_TAG",
    };
  },
 
  /**
   * Occurrs when tag is not on the tree
   */
  tagNotFound(modelName, field, value) {
    return {
      modelName,
      field,
      value,
      error: "ERR_TAG_NOT_FOUND",
    };
  },
 
  /**
   * Occurrs when referenced tag cannot be controlled by particular control tag
   */
  tagUnsupported(modelName, field, value, validType) {
    return {
      modelName,
      field,
      value,
      validType,
      error: "ERR_TAG_UNSUPPORTED",
    };
  },
 
  /**
   * Occurrs when tag has not expected parent tag at any level
   */
  parentTagUnexpected(modelName, field, value, validType) {
    return {
      modelName,
      field,
      value,
      validType,
      error: "ERR_PARENT_TAG_UNEXPECTED",
    };
  },
 
  /**
   * Occurrs when attribute value has wrong type
   */
  badAttributeValueType(modelName, field, value, validType) {
    return {
      modelName,
      field,
      value,
      validType,
      error: "ERR_BAD_TYPE",
    };
  },
 
  internalError(error) {
    return {
      error: "ERR_INTERNAL",
      value: String(error).substr(0, 1000),
      field: String(error.code),
      modelName: "",
    };
  },
 
  generalError(error) {
    return {
      error: "ERR_GENERAL",
      value: String(error).substr(0, 1000),
      field: String(error.code),
      modelName: "",
    };
  },
 
  loadingError(error, url, attrWithUrl, message = messages.ERR_LOADING_HTTP) {
    console.log("ERR", error, error.code);
    return {
      error: "ERR_GENERAL",
      value: message({ attr: attrWithUrl, error: String(error), url }),
      field: attrWithUrl,
      modelName: "",
    };
  },
};
 
/**
 * Transforms MST `describe()` to a human-readable value
 * @param {import("mobx-state-tree").IType} type
 * @param {boolean} withNullType
 */
const getTypeDescription = (type, withNullType = true) => {
  const description = type
    .describe()
    .match(/([a-z0-9?|]+)/gi)
    .join("")
    .split("|");
 
  // Remove optional null
  if (withNullType === false) {
    const index = description.indexOf("null?");
 
    if (index >= 0) description.splice(index, 1);
  }
 
  return description;
};
 
/**
 * Flatten config tree for faster iterations and searches
 * @param {object} tree
 * @param {string | null;;} parent
 * @param {string[]} parentParentTypes
 * @param {object[]} result
 * @returns {object[]}
 */
const flattenTree = (tree, parent = null, parentParentTypes = ["view"], result) => {
  if (!tree.children) return [];
 
  const children = tree.type === "pagedview" ? tree.children.slice(0, 1) : tree.children;
 
  for (const child of children) {
    /* Create a child without children and
    assign id of the parent for quick mathcing */
    const parentTypes = [...parentParentTypes, ...(parent?.type ? [parent?.type] : [])];
    const flatChild = { ...child, parent: parent?.id ?? null, parentTypes };
 
    delete flatChild.children;
 
    result.push(flatChild);
 
    if (Array.isArray(child.children)) {
      flattenTree(child, child, parentTypes, result);
    }
  }
 
  return result;
};
 
/**
 * Validates presence and format of the name attribute
 * @param {Object} child
 * @param {Object} model
 */
const validateNameTag = (child, model) => {
  const { name } = model.properties;
 
  // HyperText can be used for mark-up, without name, so name is optional type there
  if (name && !name.optionalValues && child.name === undefined) {
    return errorBuilder.required(model.name, "name");
  }
 
  return null;
};
 
/**
 * Validates toName attribute
 * Checks that connected tag is existing tag, it present in the tree
 * and can be controlled by current Object Tag
 * @param {Object} element
 * @param {Object} model
 * @param {Object[]} flatTree
 */
const validateToNameTag = (element, model, flatTree) => {
  const { controlledTags } = model.properties;
 
  if (!element.toname) return null;
 
  const names = element.toname.split(","); // for pairwise
 
  for (const name of names) {
    // Find referenced tag in the tree
    const controlledTag = flatTree.find((item) => item.name === name);
 
    if (controlledTag === undefined) {
      return errorBuilder.tagNotFound(model.name, "toname", name);
    }
 
    if (controlledTags && controlledTags.validate(controlledTag.tagName).length) {
      return errorBuilder.tagUnsupported(model.name, "toname", controlledTag.tagName, controlledTags);
    }
  }
 
  return null;
};
 
/**
 * Validates parent of tag
 * Checks that parent tag has the right type
 * @param {Object} element
 * @param {Object} model
 * @param {Object[]} flatTree
 */
const validateParentTag = (element, model) => {
  const parentTypes = model.properties.parentTypes?.value;
 
  if (
    !parentTypes ||
    element.parentTypes.find((elementParentType) =>
      parentTypes.find((type) => elementParentType === type.toLowerCase()),
    )
  ) {
    return null;
  }
  return errorBuilder.parentTagUnexpected(model.name, "parent", element.tagName, model.properties.parentTypes);
};
 
/**
 * Validates if visual tags have name attribute
 * @param {Object} element
 */
const validateVisualTags = (element) => {
  const visualTags = ["Collapse", "Filter", "Header", "Style", "View"];
  const { tagName } = element;
 
  if (visualTags.includes(tagName) && element.name) {
    return errorBuilder.generalError(`Attribute <b>name</b> is not allowed for tag <b>${tagName}</b>.`);
  }
 
  return null;
};
 
/**
 * Validate other tag attributes other than name and toName
 * @param {Object} child
 * @param {import("mobx-state-tree").IModelType} model
 * @param {string[]} fieldsToSkip
 */
const validateAttributes = (child, model, fieldsToSkip) => {
  const result = [];
  const properties = Object.keys(model.properties);
 
  for (const key of properties) {
    if (!{}.hasOwnProperty.call(child, key)) continue;
    if (fieldsToSkip.includes(key)) continue;
    const value = child[key];
    const modelProperty = model.properties[key.toLowerCase()];
    const mstValidationResult = modelProperty.validate(value, modelProperty);
 
    if (mstValidationResult.length === 0) continue;
 
    result.push(errorBuilder.badAttributeValueType(model.name, key, value, modelProperty));
  }
 
  return result;
};
 
/**
 * Validate perRegion restrictions
 * @param {Object} child
 */
const validatePerRegion = (child) => {
  const validationResult = [];
 
  // PerItem and PerRegion are incompatible but PerRegion is more prioritized mode
  if (child.perregion && child.peritem) {
    validationResult.push(
      errorBuilder.generalError(
        "Attribute <b>perItem</b> is incompatible with attribute <b>perRegion</b>. " +
          "They define two different modes. However <b>perRegion</b> works fine even with multi-item mode of object tags.",
      ),
    );
  }
 
  return validationResult;
};
 
/**
 * Convert MST type to a human-readable string
 * @param {import("mobx-state-tree").IType} type
 */
const humanizeTypeName = (type) => {
  return type ? getTypeDescription(type, false) : null;
};
 
export class ConfigValidator {
  /**
   * Validate node attributes and compatibility with other nodes
   * @param {*} node
   */
  static validate(root) {
    const flatTree = [];
 
    flattenTree(root, null, [], flatTree);
    const propertiesToSkip = ["id", "children", "name", "toname", "controlledTags", "parentTypes"];
    const validationResult = [];
 
    for (const child of flatTree) {
      try {
        const model = Registry.getModelByTag(child.type);
        // Validate name attribute
        const nameValidation = validateNameTag(child, model);
 
        if (nameValidation !== null) validationResult.push(nameValidation);
 
        // Validate toName attribute
        const toNameValidation = validateToNameTag(child, model, flatTree);
 
        if (toNameValidation !== null) validationResult.push(toNameValidation);
 
        // Validate by parentUnexpected parent tag
        const parentValidation = validateParentTag(child, model);
 
        if (parentValidation !== null) validationResult.push(parentValidation);
 
        validationResult.push(...validatePerRegion(child));
 
        validationResult.push(...validateAttributes(child, model, propertiesToSkip));
      } catch (e) {
        validationResult.push(errorBuilder.unknownTag(child.type, child.name, child.type));
      }
    }
 
    if (validationResult.length) {
      return validationResult.map((error) => ({
        ...error,
        validType: humanizeTypeName(error.validType),
      }));
    }
 
    return [];
  }
}