@truedat/df 4.28.1 → 4.28.5

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.
@@ -0,0 +1,152 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`<MandatoryConditional /> matches the latest snapshot 1`] = `
4
+ <div>
5
+ <div
6
+ class="field"
7
+ >
8
+ <label>
9
+ depends
10
+ </label>
11
+ <div
12
+ class="equal width fields"
13
+ >
14
+ <div
15
+ class="field"
16
+ >
17
+ <div
18
+ class="field"
19
+ >
20
+ <div
21
+ aria-disabled="false"
22
+ aria-expanded="false"
23
+ class="ui fluid search selection dropdown"
24
+ name="fields[0].name.mandatory.on"
25
+ role="combobox"
26
+ >
27
+ <input
28
+ aria-autocomplete="list"
29
+ autocomplete="off"
30
+ class="search"
31
+ tabindex="0"
32
+ type="text"
33
+ value=""
34
+ />
35
+ <div
36
+ aria-atomic="true"
37
+ aria-live="polite"
38
+ class="divider text"
39
+ role="alert"
40
+ >
41
+ Bar
42
+ </div>
43
+ <i
44
+ aria-hidden="true"
45
+ class="dropdown icon clear"
46
+ />
47
+ <div
48
+ class="menu transition"
49
+ role="listbox"
50
+ >
51
+ <div
52
+ aria-checked="false"
53
+ aria-selected="false"
54
+ class="item"
55
+ role="option"
56
+ style="pointer-events: all;"
57
+ >
58
+ <span
59
+ class="text"
60
+ >
61
+ Foo
62
+ </span>
63
+ </div>
64
+ <div
65
+ aria-checked="true"
66
+ aria-selected="true"
67
+ class="active selected item"
68
+ role="option"
69
+ style="pointer-events: all;"
70
+ >
71
+ <span
72
+ class="text"
73
+ >
74
+ Bar
75
+ </span>
76
+ </div>
77
+ </div>
78
+ </div>
79
+ </div>
80
+ </div>
81
+ <div
82
+ class="field"
83
+ >
84
+ <div
85
+ class="field"
86
+ >
87
+ <div
88
+ aria-disabled="false"
89
+ aria-expanded="false"
90
+ class="ui fluid multiple search selection dropdown"
91
+ name="fields[0].name.mandatory.to_be"
92
+ role="combobox"
93
+ >
94
+ <a
95
+ class="ui label"
96
+ value="3"
97
+ >
98
+ 3
99
+ <i
100
+ aria-hidden="true"
101
+ class="delete icon"
102
+ />
103
+ </a>
104
+ <input
105
+ aria-autocomplete="list"
106
+ autocomplete="off"
107
+ class="search"
108
+ tabindex="0"
109
+ type="text"
110
+ value=""
111
+ />
112
+ <span
113
+ class="sizer"
114
+ />
115
+ <div
116
+ aria-atomic="true"
117
+ aria-live="polite"
118
+ class="divider text"
119
+ role="alert"
120
+ >
121
+ to be
122
+ </div>
123
+ <i
124
+ aria-hidden="true"
125
+ class="dropdown icon clear"
126
+ />
127
+ <div
128
+ aria-multiselectable="true"
129
+ class="menu transition"
130
+ role="listbox"
131
+ >
132
+ <div
133
+ aria-checked="false"
134
+ aria-selected="true"
135
+ class="selected item"
136
+ role="option"
137
+ style="pointer-events: all;"
138
+ >
139
+ <span
140
+ class="text"
141
+ >
142
+ 4
143
+ </span>
144
+ </div>
145
+ </div>
146
+ </div>
147
+ </div>
148
+ </div>
149
+ </div>
150
+ </div>
151
+ </div>
152
+ `;
@@ -5,4 +5,10 @@ export { filterValues, validValues } from "./filterValues";
5
5
  export { filterFields } from "./filterFields";
6
6
  export { filterSwitches, isSwitch, isSwitchValid } from "./filterSwitches";
7
7
  export { flattenFields } from "./flattenFields";
8
- export { validateContent, validateRequired } from "./validateContent";
8
+ export {
9
+ enrichRequired,
10
+ dependentRequired,
11
+ isRequired,
12
+ validateContent,
13
+ validateRequired,
14
+ } from "./validateContent";
@@ -1,14 +1,16 @@
1
1
  import _ from "lodash/fp";
2
2
  import { flattenFields } from "./flattenFields";
3
3
 
4
- const fieldIsHiddenVerifier = content => ({ depends }) => {
5
- if (!depends) return false;
6
- const { on, to_be } = depends;
7
- if (_.isArray(to_be)) return _.indexOf(content[on])(to_be) < 0;
8
- else return content[on] != to_be;
9
- };
4
+ const fieldIsHiddenVerifier =
5
+ (content) =>
6
+ ({ depends }) => {
7
+ if (!depends) return false;
8
+ const { on, to_be } = depends;
9
+ if (_.isArray(to_be)) return _.indexOf(content[on])(to_be) < 0;
10
+ else return content[on] != to_be;
11
+ };
10
12
 
11
- export const validateContent = template => content => {
13
+ export const validateContent = (template) => (content) => {
12
14
  const fieldIsHidden = fieldIsHiddenVerifier(content);
13
15
  const templateContent = _.flow(
14
16
  flattenFields,
@@ -17,18 +19,43 @@ export const validateContent = template => content => {
17
19
  return _.flow(validateRequired(templateContent), _.head)([[], content]);
18
20
  };
19
21
 
20
- const isEmptyNumberOrObject = value =>
22
+ const isEmptyNumberOrObject = (value) =>
21
23
  (_.isNumber(value) && _.isNil(value)) ||
22
24
  (!_.isNumber(value) && _.isEmpty(value));
23
25
 
24
- export const validateRequired = templateContent => ([validations, content]) => {
25
- const newValidations = _.flow([
26
- _.filter(x => _.includes(x.cardinality)(["1", "+"])),
27
- _.map("name"),
28
- _.filter(r => isEmptyNumberOrObject(_.prop(r)(content))),
29
- _.map(field => ({ error: "missing required", field }))
30
- ])(templateContent);
31
- return [[...validations, ...newValidations], content];
26
+ export const validateRequired =
27
+ (templateContent) =>
28
+ ([validations, content]) => {
29
+ const newValidations = _.flow([
30
+ _.filter((field) => isRequired(field, content)),
31
+ _.map("name"),
32
+ _.filter((r) => isEmptyNumberOrObject(_.prop(r)(content))),
33
+ _.map((field) => ({ error: "missing required", field })),
34
+ ])(templateContent);
35
+ return [[...validations, ...newValidations], content];
36
+ };
37
+
38
+ export const dependentRequired = (field, content) => {
39
+ if (_.has("mandatory")(field)) {
40
+ const on = field?.mandatory?.on;
41
+ const dependent = content[on];
42
+ const target = field?.mandatory?.to_be || [];
43
+ if (_.isArray(dependent)) {
44
+ return !_.isEmpty(_.intersection(dependent)(target));
45
+ } else {
46
+ return _.includes(dependent)(target);
47
+ }
48
+ }
49
+ return false;
50
+ };
51
+
52
+ export const isRequired = (field, content) =>
53
+ _.includes(field?.cardinality)(["1", "+"]) ||
54
+ dependentRequired(field, content);
55
+
56
+ export const enrichRequired = (field, content) => {
57
+ const required = isRequired(field, content);
58
+ return { ...field, required };
32
59
  };
33
60
 
34
61
  export default validateContent;