@typed-policy/eval 0.2.0 → 0.3.0

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.
Files changed (2) hide show
  1. package/dist/index.js +56 -4
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,7 +1,15 @@
1
1
  // src/evaluate.ts
2
- function resolveValue(path, resources) {
2
+ function resolveValue(path, resources, actor) {
3
3
  const keys = path.split(".");
4
- let current = resources;
4
+ const firstKey = keys[0];
5
+ let current;
6
+ if (firstKey && firstKey in resources) {
7
+ current = resources;
8
+ } else if (firstKey && firstKey in actor) {
9
+ current = actor;
10
+ } else {
11
+ current = resources;
12
+ }
5
13
  for (const key of keys) {
6
14
  if (current === null || current === void 0) {
7
15
  return void 0;
@@ -31,10 +39,54 @@ function evaluate(expr, options) {
31
39
  return evaluate(result, { actor, resources });
32
40
  }
33
41
  case "eq": {
34
- const leftValue = resolveValue(expr.left, resources);
35
- const rightValue = typeof expr.right === "string" && expr.right.includes(".") ? resolveValue(expr.right, resources) : expr.right;
42
+ const leftValue = resolveValue(expr.left, resources, actor);
43
+ const rightValue = typeof expr.right === "string" && expr.right.includes(".") ? resolveValue(expr.right, resources, actor) : expr.right;
36
44
  return leftValue === rightValue;
37
45
  }
46
+ case "neq": {
47
+ const leftValue = resolveValue(expr.left, resources, actor);
48
+ const rightValue = typeof expr.right === "string" && expr.right.includes(".") ? resolveValue(expr.right, resources, actor) : expr.right;
49
+ return leftValue !== rightValue;
50
+ }
51
+ case "gt": {
52
+ const leftValue = resolveValue(expr.left, resources, actor);
53
+ const rightValue = typeof expr.right === "string" && expr.right.includes(".") ? resolveValue(expr.right, resources, actor) : expr.right;
54
+ if (leftValue == null || rightValue == null) return false;
55
+ return leftValue > rightValue;
56
+ }
57
+ case "lt": {
58
+ const leftValue = resolveValue(expr.left, resources, actor);
59
+ const rightValue = typeof expr.right === "string" && expr.right.includes(".") ? resolveValue(expr.right, resources, actor) : expr.right;
60
+ if (leftValue == null || rightValue == null) return false;
61
+ return leftValue < rightValue;
62
+ }
63
+ case "gte": {
64
+ const leftValue = resolveValue(expr.left, resources, actor);
65
+ const rightValue = typeof expr.right === "string" && expr.right.includes(".") ? resolveValue(expr.right, resources, actor) : expr.right;
66
+ if (leftValue == null || rightValue == null) return false;
67
+ return leftValue >= rightValue;
68
+ }
69
+ case "lte": {
70
+ const leftValue = resolveValue(expr.left, resources, actor);
71
+ const rightValue = typeof expr.right === "string" && expr.right.includes(".") ? resolveValue(expr.right, resources, actor) : expr.right;
72
+ if (leftValue == null || rightValue == null) return false;
73
+ return leftValue <= rightValue;
74
+ }
75
+ case "inArray": {
76
+ const pathValue = resolveValue(expr.path, resources, actor);
77
+ return expr.values.includes(pathValue);
78
+ }
79
+ case "isNull": {
80
+ const pathValue = resolveValue(expr.path, resources, actor);
81
+ return pathValue === null;
82
+ }
83
+ case "isNotNull": {
84
+ const pathValue = resolveValue(expr.path, resources, actor);
85
+ return pathValue !== null;
86
+ }
87
+ case "not": {
88
+ return !evaluate(expr.expr, { actor, resources });
89
+ }
38
90
  case "and": {
39
91
  return expr.rules.every((rule) => evaluate(rule, { actor, resources }));
40
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typed-policy/eval",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Frontend policy evaluator",
5
5
  "author": "Ihsan VP <m.ihsan.vp@gmail.com>",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@typed-policy/core": "0.2.0"
35
+ "@typed-policy/core": "0.3.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@biomejs/biome": "^1.9.4",