@beabee/beabee-common 1.7.0 → 1.7.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,5 +1,4 @@
1
1
  "use strict";
2
- // *** Definitions for rules ***
3
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
3
  if (k2 === undefined) k2 = k;
5
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -14,8 +13,14 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
14
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
15
  };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
17
19
  Object.defineProperty(exports, "__esModule", { value: true });
18
20
  exports.convertFiltersToRuleGroup = exports.convertRuleGroupToFilters = exports.validateRuleGroup = exports.validateRule = exports.isRuleGroup = exports.operatorsByType = exports.nullableOperators = exports.ruleOperators = void 0;
21
+ const isValid_1 = __importDefault(require("date-fns/isValid"));
22
+ const parseISO_1 = __importDefault(require("date-fns/parseISO"));
23
+ // *** Definitions for rules ***
19
24
  exports.ruleOperators = [
20
25
  "equal",
21
26
  "not_equal",
@@ -34,8 +39,10 @@ exports.ruleOperators = [
34
39
  "is_empty",
35
40
  "is_not_empty",
36
41
  ];
37
- const equal = { args: 1 };
38
- const equalityOperators = { equal, not_equal: { args: 1 } };
42
+ const equalityOperators = {
43
+ equal: { args: 1 },
44
+ not_equal: { args: 1 },
45
+ };
39
46
  const numericOperators = {
40
47
  ...equalityOperators,
41
48
  between: { args: 2 },
@@ -49,6 +56,7 @@ const arrayOperators = {
49
56
  contains: { args: 1 },
50
57
  not_contains: { args: 1 },
51
58
  };
59
+ // Special operator can be applied across all fields if they are nullable
52
60
  exports.nullableOperators = {
53
61
  is_empty: { args: 0 },
54
62
  is_not_empty: { args: 0 },
@@ -64,11 +72,13 @@ exports.operatorsByType = {
64
72
  },
65
73
  date: numericOperators,
66
74
  number: numericOperators,
67
- boolean: { equal },
75
+ boolean: { equal: equalityOperators.equal },
68
76
  array: arrayOperators,
69
77
  enum: equalityOperators,
70
78
  contact: equalityOperators,
71
79
  };
80
+ // More general type to allow mapping while maintaining full type above
81
+ const operatorsByTypeMap = exports.operatorsByType;
72
82
  // *** Helper methods ***
73
83
  function isRuleGroup(ruleOrGroup) {
74
84
  return "condition" in ruleOrGroup;
@@ -79,12 +89,22 @@ function validateRule(filters, rule) {
79
89
  if (!filter) {
80
90
  return false; // Invalid field
81
91
  }
82
- const operator = exports.operatorsByType[filter.type][rule.operator];
83
- if (!operator) {
84
- return false; // Invalid operator
92
+ if (rule.operator in exports.nullableOperators) {
93
+ if (!filter.nullable && filter.type !== "text") {
94
+ return false; // Field cannot be empty (text can always be empty)
95
+ }
96
+ if (rule.value.length !== 0) {
97
+ return false; // Should have no args
98
+ }
85
99
  }
86
- if (operator.args !== rule.value.length) {
87
- return false; // Invalid number of args
100
+ else {
101
+ const operator = operatorsByTypeMap[filter.type][rule.operator];
102
+ if (!operator) {
103
+ return false; // Invalid operator
104
+ }
105
+ if (operator.args !== rule.value.length) {
106
+ return false; // Invalid number of args
107
+ }
88
108
  }
89
109
  const expectedType = filter.type === "boolean" || filter.type === "number"
90
110
  ? filter.type
@@ -93,7 +113,7 @@ function validateRule(filters, rule) {
93
113
  return false; // Invalid value type
94
114
  }
95
115
  if (filter.type === "date" &&
96
- rule.value.some((v) => isNaN(+new Date(v)))) {
116
+ rule.value.some((v) => (0, isValid_1.default)((0, parseISO_1.default)(v)))) {
97
117
  return false; // Invalid date
98
118
  }
99
119
  return {
@@ -1,3 +1,5 @@
1
+ import isValid from "date-fns/isValid";
2
+ import parseISO from "date-fns/parseISO";
1
3
  // *** Definitions for rules ***
2
4
  export const ruleOperators = [
3
5
  "equal",
@@ -17,8 +19,10 @@ export const ruleOperators = [
17
19
  "is_empty",
18
20
  "is_not_empty",
19
21
  ];
20
- const equal = { args: 1 };
21
- const equalityOperators = { equal, not_equal: { args: 1 } };
22
+ const equalityOperators = {
23
+ equal: { args: 1 },
24
+ not_equal: { args: 1 },
25
+ };
22
26
  const numericOperators = {
23
27
  ...equalityOperators,
24
28
  between: { args: 2 },
@@ -32,6 +36,7 @@ const arrayOperators = {
32
36
  contains: { args: 1 },
33
37
  not_contains: { args: 1 },
34
38
  };
39
+ // Special operator can be applied across all fields if they are nullable
35
40
  export const nullableOperators = {
36
41
  is_empty: { args: 0 },
37
42
  is_not_empty: { args: 0 },
@@ -47,11 +52,13 @@ export const operatorsByType = {
47
52
  },
48
53
  date: numericOperators,
49
54
  number: numericOperators,
50
- boolean: { equal },
55
+ boolean: { equal: equalityOperators.equal },
51
56
  array: arrayOperators,
52
57
  enum: equalityOperators,
53
58
  contact: equalityOperators,
54
59
  };
60
+ // More general type to allow mapping while maintaining full type above
61
+ const operatorsByTypeMap = operatorsByType;
55
62
  // *** Helper methods ***
56
63
  export function isRuleGroup(ruleOrGroup) {
57
64
  return "condition" in ruleOrGroup;
@@ -61,12 +68,22 @@ export function validateRule(filters, rule) {
61
68
  if (!filter) {
62
69
  return false; // Invalid field
63
70
  }
64
- const operator = operatorsByType[filter.type][rule.operator];
65
- if (!operator) {
66
- return false; // Invalid operator
71
+ if (rule.operator in nullableOperators) {
72
+ if (!filter.nullable && filter.type !== "text") {
73
+ return false; // Field cannot be empty (text can always be empty)
74
+ }
75
+ if (rule.value.length !== 0) {
76
+ return false; // Should have no args
77
+ }
67
78
  }
68
- if (operator.args !== rule.value.length) {
69
- return false; // Invalid number of args
79
+ else {
80
+ const operator = operatorsByTypeMap[filter.type][rule.operator];
81
+ if (!operator) {
82
+ return false; // Invalid operator
83
+ }
84
+ if (operator.args !== rule.value.length) {
85
+ return false; // Invalid number of args
86
+ }
70
87
  }
71
88
  const expectedType = filter.type === "boolean" || filter.type === "number"
72
89
  ? filter.type
@@ -75,7 +92,7 @@ export function validateRule(filters, rule) {
75
92
  return false; // Invalid value type
76
93
  }
77
94
  if (filter.type === "date" &&
78
- rule.value.some((v) => isNaN(+new Date(v)))) {
95
+ rule.value.some((v) => isValid(parseISO(v)))) {
79
96
  return false; // Invalid date
80
97
  }
81
98
  return {
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.2",
4
4
  "description": "",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -28,5 +28,8 @@
28
28
  "prettier": "^2.7.1",
29
29
  "rimraf": "^3.0.2",
30
30
  "typescript": "^4.8.4"
31
+ },
32
+ "dependencies": {
33
+ "date-fns": "^2.29.3"
31
34
  }
32
35
  }