@openframe-org/criteria-set-protocol 2.6.0 → 2.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { Color, CriteriaTree, CriteriaTreeElement, Theme, Criterion, Task, TaskGroup, TaskItem, ThemeOptions, CriterionOptions, TaskOptions, CertificationDefinition, TaskItemValue } from "./types";
1
+ import { Color, CriteriaTree, CriteriaTreeElement, Theme, Criterion, Task, TaskGroup, TaskItem, ThemeOptions, CriterionOptions, TaskOptions, CertificationDefinition, TaskItemValue, TaskItemData, ElementData, TreeResult } from "./types";
2
2
  interface ApplyTextFormattingPlaceholdersForThemeFunction {
3
3
  (options: ThemeOptions, element: Pick<Task, "title" | "code">): string;
4
4
  }
@@ -50,4 +50,14 @@ export declare const isNil: (value: unknown) => value is null | undefined;
50
50
  * Given a list of certification definitions, return the ones for which the given value is valid
51
51
  */
52
52
  export declare const getCertificationsByValue: (certificationDefinitions: CertificationDefinition[], value?: TaskItemValue) => CertificationDefinition[] | undefined;
53
+ /**
54
+ * Given a tree element or a data object, validate that it contains valid data. If it is a tree element,
55
+ * extract the data object from it and validate it:
56
+ * - If the data is a number/<undefined> type, ensure that it has a numeric value and total
57
+ * - If the data is a percentage type, ensure that it has a numeric value
58
+ * - If the data is a boolean type, ensure that it has a boolean value
59
+ * - If it is a different type, throw an error
60
+ * In all cases, the data should contain 'text' and 'readOnly' properties
61
+ */
62
+ export declare const validateData: (dataOrTreeElement: CriteriaTreeElement | TaskItemData | ElementData | TreeResult) => void;
53
63
  export {};
package/dist/v1/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCertificationsByValue = exports.isNil = exports.applyReportTitleTextFormatFormattingPlaceholders = exports.applyCriteriaTreeElementTextFormatFormattingPlaceholders = exports.applyDocumentTreeFolderTextFormattingPlaceholders = exports.applyBreadcrumbTextFormattingPlaceholders = exports.findInTree = exports.toColorHexString = exports.isTaskItem = exports.isTask = exports.isTaskGroup = exports.isCriterion = exports.isTheme = void 0;
3
+ exports.validateData = exports.getCertificationsByValue = exports.isNil = exports.applyReportTitleTextFormatFormattingPlaceholders = exports.applyCriteriaTreeElementTextFormatFormattingPlaceholders = exports.applyDocumentTreeFolderTextFormattingPlaceholders = exports.applyBreadcrumbTextFormattingPlaceholders = exports.findInTree = exports.toColorHexString = exports.isTaskItem = exports.isTask = exports.isTaskGroup = exports.isCriterion = exports.isTheme = void 0;
4
4
  const isTheme = (element) => element.type === "theme";
5
5
  exports.isTheme = isTheme;
6
6
  const isCriterion = (element) => element.type === "criterion";
@@ -91,3 +91,53 @@ const getCertificationsByValue = (certificationDefinitions, value) => {
91
91
  });
92
92
  };
93
93
  exports.getCertificationsByValue = getCertificationsByValue;
94
+ /**
95
+ * Given a tree element or a data object, validate that it contains valid data. If it is a tree element,
96
+ * extract the data object from it and validate it:
97
+ * - If the data is a number/<undefined> type, ensure that it has a numeric value and total
98
+ * - If the data is a percentage type, ensure that it has a numeric value
99
+ * - If the data is a boolean type, ensure that it has a boolean value
100
+ * - If it is a different type, throw an error
101
+ * In all cases, the data should contain 'text' and 'readOnly' properties
102
+ */
103
+ const validateData = (dataOrTreeElement) => {
104
+ if (("type" in dataOrTreeElement) && dataOrTreeElement.type === "task-item" && !("readOnly" in dataOrTreeElement)) {
105
+ throw new Error("Data is missing 'readOnly' property");
106
+ }
107
+ const data = "data" in dataOrTreeElement ? dataOrTreeElement.data : dataOrTreeElement;
108
+ if (!data) {
109
+ throw new Error("Invalid data object");
110
+ }
111
+ ["value", "text"].forEach((property) => {
112
+ if (!(property in data)) {
113
+ throw new Error(`Data is missing '${property}' property`);
114
+ }
115
+ });
116
+ switch (data.type) {
117
+ case "number":
118
+ case undefined: {
119
+ if (typeof data.value !== "number") {
120
+ throw new Error(`Data value is not numeric: ${data.value}`);
121
+ }
122
+ if (!("total" in data) || typeof data.total !== "number") {
123
+ throw new Error(`Data total is missing or not numeric: ${data.total}`);
124
+ }
125
+ break;
126
+ }
127
+ case "percentage": {
128
+ if (typeof data.value !== "number") {
129
+ throw new Error(`Data value is not numeric: ${data.value}`);
130
+ }
131
+ break;
132
+ }
133
+ case "boolean": {
134
+ if (typeof data.value !== "boolean") {
135
+ throw new Error(`Data value is not a boolean: ${data.value}`);
136
+ }
137
+ break;
138
+ }
139
+ default:
140
+ throw new Error(`Data has an invalid 'type' property: ${data.type}`);
141
+ }
142
+ };
143
+ exports.validateData = validateData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openframe-org/criteria-set-protocol",
3
- "version": "2.6.0",
3
+ "version": "2.6.2",
4
4
  "description": "A protocol and tools for defining and working with criteria sets",
5
5
  "private": false,
6
6
  "author": "Andrés Angulo <aa@openframe.org>",