@beabee/beabee-common 1.7.0 → 1.7.1

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.
@@ -34,8 +34,10 @@ exports.ruleOperators = [
34
34
  "is_empty",
35
35
  "is_not_empty",
36
36
  ];
37
- const equal = { args: 1 };
38
- const equalityOperators = { equal, not_equal: { args: 1 } };
37
+ const equalityOperators = {
38
+ equal: { args: 1 },
39
+ not_equal: { args: 1 },
40
+ };
39
41
  const numericOperators = {
40
42
  ...equalityOperators,
41
43
  between: { args: 2 },
@@ -49,6 +51,7 @@ const arrayOperators = {
49
51
  contains: { args: 1 },
50
52
  not_contains: { args: 1 },
51
53
  };
54
+ // Special operator can be applied across all fields if they are nullable
52
55
  exports.nullableOperators = {
53
56
  is_empty: { args: 0 },
54
57
  is_not_empty: { args: 0 },
@@ -64,11 +67,13 @@ exports.operatorsByType = {
64
67
  },
65
68
  date: numericOperators,
66
69
  number: numericOperators,
67
- boolean: { equal },
70
+ boolean: { equal: equalityOperators.equal },
68
71
  array: arrayOperators,
69
72
  enum: equalityOperators,
70
73
  contact: equalityOperators,
71
74
  };
75
+ // More general type to allow mapping while maintaining full type above
76
+ const operatorsByTypeMap = exports.operatorsByType;
72
77
  // *** Helper methods ***
73
78
  function isRuleGroup(ruleOrGroup) {
74
79
  return "condition" in ruleOrGroup;
@@ -79,12 +84,22 @@ function validateRule(filters, rule) {
79
84
  if (!filter) {
80
85
  return false; // Invalid field
81
86
  }
82
- const operator = exports.operatorsByType[filter.type][rule.operator];
83
- if (!operator) {
84
- return false; // Invalid operator
87
+ if (rule.operator in exports.nullableOperators) {
88
+ if (!filter.nullable && filter.type !== "text") {
89
+ return false; // Field cannot be empty (text can always be empty)
90
+ }
91
+ if (rule.value.length !== 0) {
92
+ return false; // Should have no args
93
+ }
85
94
  }
86
- if (operator.args !== rule.value.length) {
87
- return false; // Invalid number of args
95
+ else {
96
+ const operator = operatorsByTypeMap[filter.type][rule.operator];
97
+ if (!operator) {
98
+ return false; // Invalid operator
99
+ }
100
+ if (operator.args !== rule.value.length) {
101
+ return false; // Invalid number of args
102
+ }
88
103
  }
89
104
  const expectedType = filter.type === "boolean" || filter.type === "number"
90
105
  ? filter.type
@@ -17,8 +17,10 @@ export const ruleOperators = [
17
17
  "is_empty",
18
18
  "is_not_empty",
19
19
  ];
20
- const equal = { args: 1 };
21
- const equalityOperators = { equal, not_equal: { args: 1 } };
20
+ const equalityOperators = {
21
+ equal: { args: 1 },
22
+ not_equal: { args: 1 },
23
+ };
22
24
  const numericOperators = {
23
25
  ...equalityOperators,
24
26
  between: { args: 2 },
@@ -32,6 +34,7 @@ const arrayOperators = {
32
34
  contains: { args: 1 },
33
35
  not_contains: { args: 1 },
34
36
  };
37
+ // Special operator can be applied across all fields if they are nullable
35
38
  export const nullableOperators = {
36
39
  is_empty: { args: 0 },
37
40
  is_not_empty: { args: 0 },
@@ -47,11 +50,13 @@ export const operatorsByType = {
47
50
  },
48
51
  date: numericOperators,
49
52
  number: numericOperators,
50
- boolean: { equal },
53
+ boolean: { equal: equalityOperators.equal },
51
54
  array: arrayOperators,
52
55
  enum: equalityOperators,
53
56
  contact: equalityOperators,
54
57
  };
58
+ // More general type to allow mapping while maintaining full type above
59
+ const operatorsByTypeMap = operatorsByType;
55
60
  // *** Helper methods ***
56
61
  export function isRuleGroup(ruleOrGroup) {
57
62
  return "condition" in ruleOrGroup;
@@ -61,12 +66,22 @@ export function validateRule(filters, rule) {
61
66
  if (!filter) {
62
67
  return false; // Invalid field
63
68
  }
64
- const operator = operatorsByType[filter.type][rule.operator];
65
- if (!operator) {
66
- return false; // Invalid operator
69
+ if (rule.operator in nullableOperators) {
70
+ if (!filter.nullable && filter.type !== "text") {
71
+ return false; // Field cannot be empty (text can always be empty)
72
+ }
73
+ if (rule.value.length !== 0) {
74
+ return false; // Should have no args
75
+ }
67
76
  }
68
- if (operator.args !== rule.value.length) {
69
- return false; // Invalid number of args
77
+ else {
78
+ const operator = operatorsByTypeMap[filter.type][rule.operator];
79
+ if (!operator) {
80
+ return false; // Invalid operator
81
+ }
82
+ if (operator.args !== rule.value.length) {
83
+ return false; // Invalid number of args
84
+ }
70
85
  }
71
86
  const expectedType = filter.type === "boolean" || filter.type === "number"
72
87
  ? filter.type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beabee/beabee-common",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "description": "",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",