@api-client/core 0.20.2 → 0.20.4
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.
- package/build/src/modeling/Semantics.d.ts +0 -252
- package/build/src/modeling/Semantics.d.ts.map +1 -1
- package/build/src/modeling/Semantics.js +0 -356
- package/build/src/modeling/Semantics.js.map +1 -1
- package/build/src/modeling/ai/tools/Semantic.tools.d.ts +0 -3
- package/build/src/modeling/ai/tools/Semantic.tools.d.ts.map +1 -1
- package/build/src/modeling/types.d.ts +50 -0
- package/build/src/modeling/types.d.ts.map +1 -1
- package/build/src/modeling/types.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -4
- package/src/modeling/Semantics.ts +40 -520
- package/src/modeling/types.ts +52 -0
- package/tests/unit/modeling/client_ip_address_semantic.spec.ts +0 -22
- package/tests/unit/modeling/semantics.spec.ts +0 -65
- package/tests/unit/modeling/username_semantic.spec.ts +0 -32
- package/build/src/runtime/modeling/Semantics.d.ts +0 -84
- package/build/src/runtime/modeling/Semantics.d.ts.map +0 -1
- package/build/src/runtime/modeling/Semantics.js +0 -124
- package/build/src/runtime/modeling/Semantics.js.map +0 -1
- package/src/runtime/modeling/Semantics.ts +0 -196
- package/tests/unit/modeling/semantic_runtime.spec.ts +0 -113
package/src/modeling/types.ts
CHANGED
|
@@ -127,6 +127,58 @@ export interface SchemaDefaultValue {
|
|
|
127
127
|
* It is always a string and it has to be casted to the property's data type.
|
|
128
128
|
*/
|
|
129
129
|
value: string | FunctionValue
|
|
130
|
+
/**
|
|
131
|
+
* When the `value` is a function, the `config` property is used to configure the function.
|
|
132
|
+
* The type of the config depends on the function and the property data type.
|
|
133
|
+
*/
|
|
134
|
+
config?: FunctionGeneratorConfig
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A union of all possible function generator configurations.
|
|
139
|
+
*/
|
|
140
|
+
export type FunctionGeneratorConfig = StringDefaultRandomGenerator
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Configuration for the `random` function for string properties.
|
|
144
|
+
*/
|
|
145
|
+
export interface StringDefaultRandomGenerator {
|
|
146
|
+
/**
|
|
147
|
+
* When set the `max` and `min` are used.
|
|
148
|
+
* Otherwise a random string of fixed length (the `fixed` property) is generated.
|
|
149
|
+
*/
|
|
150
|
+
varyingLength?: boolean
|
|
151
|
+
/**
|
|
152
|
+
* The minimum length of the random string.
|
|
153
|
+
*
|
|
154
|
+
* When not specified, it defaults to `10`.
|
|
155
|
+
* The `varyingLength` must be `true` to use this property.
|
|
156
|
+
*/
|
|
157
|
+
min?: number
|
|
158
|
+
/**
|
|
159
|
+
* The maximum length of the random string.
|
|
160
|
+
*
|
|
161
|
+
* When not specified, it defaults to `30`.
|
|
162
|
+
* The `varyingLength` must be `true` to use this property.
|
|
163
|
+
*/
|
|
164
|
+
max?: number
|
|
165
|
+
/**
|
|
166
|
+
* The fixed length of the random string.
|
|
167
|
+
*
|
|
168
|
+
* When not specified, it defaults to `10`.
|
|
169
|
+
* The `varyingLength` must be `false` to use this property.
|
|
170
|
+
*/
|
|
171
|
+
fixed?: number
|
|
172
|
+
/**
|
|
173
|
+
* The prefix to add to the random string.
|
|
174
|
+
* The prefix adds to the length of the random string.
|
|
175
|
+
*/
|
|
176
|
+
prefix?: string
|
|
177
|
+
/**
|
|
178
|
+
* The suffix to add to the random string.
|
|
179
|
+
* The suffix adds to the length of the random string.
|
|
180
|
+
*/
|
|
181
|
+
suffix?: string
|
|
130
182
|
}
|
|
131
183
|
|
|
132
184
|
/**
|
|
@@ -5,8 +5,6 @@ import {
|
|
|
5
5
|
isPropertySemantic,
|
|
6
6
|
SemanticCategory,
|
|
7
7
|
SemanticScope,
|
|
8
|
-
SemanticTiming,
|
|
9
|
-
SemanticOperation,
|
|
10
8
|
} from '../../../src/modeling/Semantics.js'
|
|
11
9
|
|
|
12
10
|
test.group('ClientIPAddress Semantic', () => {
|
|
@@ -48,24 +46,4 @@ test.group('ClientIPAddress Semantic', () => {
|
|
|
48
46
|
const semantic = DataSemantics[SemanticType.ClientIPAddress]
|
|
49
47
|
assert.isFalse(semantic.hasConfig)
|
|
50
48
|
})
|
|
51
|
-
|
|
52
|
-
test('should have correct runtime configuration', ({ assert }) => {
|
|
53
|
-
const semantic = DataSemantics[SemanticType.ClientIPAddress]
|
|
54
|
-
const runtime = semantic.runtime
|
|
55
|
-
|
|
56
|
-
assert.equal(runtime.timing, SemanticTiming.Before)
|
|
57
|
-
assert.deepEqual(runtime.operations, [SemanticOperation.Create, SemanticOperation.Update])
|
|
58
|
-
assert.equal(runtime.priority, 95) // Low priority, populate after other validations
|
|
59
|
-
assert.equal(runtime.timeoutMs, 100) // Very fast operation
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
test('should have correct execution conditions', ({ assert }) => {
|
|
63
|
-
const semantic = DataSemantics[SemanticType.ClientIPAddress]
|
|
64
|
-
const conditions = semantic.runtime.conditions
|
|
65
|
-
|
|
66
|
-
assert.isDefined(conditions)
|
|
67
|
-
assert.lengthOf(conditions!, 1)
|
|
68
|
-
assert.equal(conditions![0].expression, 'entity[semantics.ClientIPAddress] == null')
|
|
69
|
-
assert.equal(conditions![0].description, 'Only set IP address if not already provided')
|
|
70
|
-
})
|
|
71
49
|
})
|
|
@@ -3,8 +3,6 @@ import {
|
|
|
3
3
|
SemanticType,
|
|
4
4
|
SemanticScope,
|
|
5
5
|
SemanticCategory,
|
|
6
|
-
SemanticTiming,
|
|
7
|
-
SemanticOperation,
|
|
8
6
|
isEntitySemantic,
|
|
9
7
|
isPropertySemantic,
|
|
10
8
|
isAssociationSemantic,
|
|
@@ -52,10 +50,6 @@ test.group('Semantics', () => {
|
|
|
52
50
|
scope: SemanticScope.Entity,
|
|
53
51
|
category: SemanticCategory.Identity,
|
|
54
52
|
hasConfig: false,
|
|
55
|
-
runtime: {
|
|
56
|
-
timing: SemanticTiming.None,
|
|
57
|
-
operations: [],
|
|
58
|
-
},
|
|
59
53
|
}
|
|
60
54
|
const propertySemantic: PropertySemantic = {
|
|
61
55
|
id: SemanticType.CreatedTimestamp,
|
|
@@ -65,11 +59,6 @@ test.group('Semantics', () => {
|
|
|
65
59
|
category: SemanticCategory.Lifecycle,
|
|
66
60
|
hasConfig: false,
|
|
67
61
|
applicableDataTypes: ['datetime'],
|
|
68
|
-
runtime: {
|
|
69
|
-
timing: SemanticTiming.Before,
|
|
70
|
-
operations: [SemanticOperation.Create],
|
|
71
|
-
priority: 90,
|
|
72
|
-
},
|
|
73
62
|
}
|
|
74
63
|
const associationSemantic: AssociationSemantic = {
|
|
75
64
|
id: SemanticType.ResourceOwnerIdentifier,
|
|
@@ -78,18 +67,6 @@ test.group('Semantics', () => {
|
|
|
78
67
|
scope: SemanticScope.Association,
|
|
79
68
|
category: SemanticCategory.Identity,
|
|
80
69
|
hasConfig: false,
|
|
81
|
-
runtime: {
|
|
82
|
-
timing: SemanticTiming.Before,
|
|
83
|
-
operations: [
|
|
84
|
-
SemanticOperation.Create,
|
|
85
|
-
SemanticOperation.Read,
|
|
86
|
-
SemanticOperation.Update,
|
|
87
|
-
SemanticOperation.Delete,
|
|
88
|
-
SemanticOperation.List,
|
|
89
|
-
],
|
|
90
|
-
priority: 5,
|
|
91
|
-
canDisable: false,
|
|
92
|
-
},
|
|
93
70
|
}
|
|
94
71
|
|
|
95
72
|
assert.isTrue(isEntitySemantic(entitySemantic))
|
|
@@ -105,10 +82,6 @@ test.group('Semantics', () => {
|
|
|
105
82
|
scope: SemanticScope.Entity,
|
|
106
83
|
category: SemanticCategory.Identity,
|
|
107
84
|
hasConfig: false,
|
|
108
|
-
runtime: {
|
|
109
|
-
timing: SemanticTiming.None,
|
|
110
|
-
operations: [],
|
|
111
|
-
},
|
|
112
85
|
}
|
|
113
86
|
const propertySemantic: PropertySemantic = {
|
|
114
87
|
id: SemanticType.CreatedTimestamp,
|
|
@@ -118,11 +91,6 @@ test.group('Semantics', () => {
|
|
|
118
91
|
category: SemanticCategory.Lifecycle,
|
|
119
92
|
hasConfig: false,
|
|
120
93
|
applicableDataTypes: ['datetime'],
|
|
121
|
-
runtime: {
|
|
122
|
-
timing: SemanticTiming.Before,
|
|
123
|
-
operations: [SemanticOperation.Create],
|
|
124
|
-
priority: 90,
|
|
125
|
-
},
|
|
126
94
|
}
|
|
127
95
|
const associationSemantic: AssociationSemantic = {
|
|
128
96
|
id: SemanticType.ResourceOwnerIdentifier,
|
|
@@ -131,18 +99,6 @@ test.group('Semantics', () => {
|
|
|
131
99
|
scope: SemanticScope.Association,
|
|
132
100
|
category: SemanticCategory.Identity,
|
|
133
101
|
hasConfig: false,
|
|
134
|
-
runtime: {
|
|
135
|
-
timing: SemanticTiming.Before,
|
|
136
|
-
operations: [
|
|
137
|
-
SemanticOperation.Create,
|
|
138
|
-
SemanticOperation.Read,
|
|
139
|
-
SemanticOperation.Update,
|
|
140
|
-
SemanticOperation.Delete,
|
|
141
|
-
SemanticOperation.List,
|
|
142
|
-
],
|
|
143
|
-
priority: 5,
|
|
144
|
-
canDisable: false,
|
|
145
|
-
},
|
|
146
102
|
}
|
|
147
103
|
|
|
148
104
|
assert.isFalse(isPropertySemantic(entitySemantic))
|
|
@@ -158,10 +114,6 @@ test.group('Semantics', () => {
|
|
|
158
114
|
scope: SemanticScope.Entity,
|
|
159
115
|
category: SemanticCategory.Identity,
|
|
160
116
|
hasConfig: false,
|
|
161
|
-
runtime: {
|
|
162
|
-
timing: SemanticTiming.None,
|
|
163
|
-
operations: [],
|
|
164
|
-
},
|
|
165
117
|
}
|
|
166
118
|
const propertySemantic: PropertySemantic = {
|
|
167
119
|
id: SemanticType.CreatedTimestamp,
|
|
@@ -171,11 +123,6 @@ test.group('Semantics', () => {
|
|
|
171
123
|
category: SemanticCategory.Lifecycle,
|
|
172
124
|
hasConfig: false,
|
|
173
125
|
applicableDataTypes: ['datetime'],
|
|
174
|
-
runtime: {
|
|
175
|
-
timing: SemanticTiming.Before,
|
|
176
|
-
operations: [SemanticOperation.Create],
|
|
177
|
-
priority: 90,
|
|
178
|
-
},
|
|
179
126
|
}
|
|
180
127
|
const associationSemantic: AssociationSemantic = {
|
|
181
128
|
id: SemanticType.ResourceOwnerIdentifier,
|
|
@@ -184,18 +131,6 @@ test.group('Semantics', () => {
|
|
|
184
131
|
scope: SemanticScope.Association,
|
|
185
132
|
category: SemanticCategory.Identity,
|
|
186
133
|
hasConfig: false,
|
|
187
|
-
runtime: {
|
|
188
|
-
timing: SemanticTiming.Before,
|
|
189
|
-
operations: [
|
|
190
|
-
SemanticOperation.Create,
|
|
191
|
-
SemanticOperation.Read,
|
|
192
|
-
SemanticOperation.Update,
|
|
193
|
-
SemanticOperation.Delete,
|
|
194
|
-
SemanticOperation.List,
|
|
195
|
-
],
|
|
196
|
-
priority: 5,
|
|
197
|
-
canDisable: false,
|
|
198
|
-
},
|
|
199
134
|
}
|
|
200
135
|
|
|
201
136
|
assert.isFalse(isAssociationSemantic(entitySemantic))
|
|
@@ -5,8 +5,6 @@ import {
|
|
|
5
5
|
isPropertySemantic,
|
|
6
6
|
SemanticCategory,
|
|
7
7
|
SemanticScope,
|
|
8
|
-
SemanticTiming,
|
|
9
|
-
SemanticOperation,
|
|
10
8
|
} from '../../../src/modeling/Semantics.js'
|
|
11
9
|
|
|
12
10
|
test.group('Username Semantic', () => {
|
|
@@ -48,34 +46,4 @@ test.group('Username Semantic', () => {
|
|
|
48
46
|
const semantic = DataSemantics[SemanticType.Username]
|
|
49
47
|
assert.isFalse(semantic.hasConfig)
|
|
50
48
|
})
|
|
51
|
-
|
|
52
|
-
test('should not be disableable for security reasons', ({ assert }) => {
|
|
53
|
-
const semantic = DataSemantics[SemanticType.Username]
|
|
54
|
-
assert.isFalse(semantic.runtime.canDisable)
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
test('should have correct runtime configuration', ({ assert }) => {
|
|
58
|
-
const semantic = DataSemantics[SemanticType.Username]
|
|
59
|
-
const runtime = semantic.runtime
|
|
60
|
-
|
|
61
|
-
assert.equal(runtime.timing, SemanticTiming.Before)
|
|
62
|
-
assert.deepEqual(runtime.operations, [SemanticOperation.Create, SemanticOperation.Update, SemanticOperation.Read])
|
|
63
|
-
assert.equal(runtime.priority, 15) // High priority for authentication
|
|
64
|
-
assert.equal(runtime.timeoutMs, 100) // Fast operation
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
test('should have higher priority than general user properties but lower than password', ({ assert }) => {
|
|
68
|
-
const usernameSemantic = DataSemantics[SemanticType.Username]
|
|
69
|
-
const passwordSemantic = DataSemantics[SemanticType.Password]
|
|
70
|
-
const userRoleSemantic = DataSemantics[SemanticType.UserRole]
|
|
71
|
-
|
|
72
|
-
const usernameP = usernameSemantic.runtime.priority ?? 100
|
|
73
|
-
const passwordP = passwordSemantic.runtime.priority ?? 100
|
|
74
|
-
const userRoleP = userRoleSemantic.runtime.priority ?? 100
|
|
75
|
-
|
|
76
|
-
// Username should have lower priority number (higher priority) than UserRole
|
|
77
|
-
assert.isAtMost(usernameP, userRoleP)
|
|
78
|
-
// But higher priority number (lower priority) than Password
|
|
79
|
-
assert.isAtLeast(usernameP, passwordP)
|
|
80
|
-
})
|
|
81
49
|
})
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { Jexl } from '@pawel-up/jexl/Jexl.js';
|
|
2
|
-
import type { DomainEntity } from '../../modeling/DomainEntity.js';
|
|
3
|
-
import { type AppliedDataSemantic, type SemanticCondition, SemanticOperation, SemanticType } from '../../modeling/Semantics.js';
|
|
4
|
-
import type { TransformFunction } from '@pawel-up/jexl';
|
|
5
|
-
/**
|
|
6
|
-
* Get or create a JEXL instance with optional custom transforms
|
|
7
|
-
*/
|
|
8
|
-
export declare function getJexlInstance(transforms?: Record<string, TransformFunction>): Jexl;
|
|
9
|
-
/**
|
|
10
|
-
* Context object passed to semantic condition evaluation
|
|
11
|
-
*/
|
|
12
|
-
export interface SemanticExecutionContext {
|
|
13
|
-
/**
|
|
14
|
-
* The entity data being processed
|
|
15
|
-
*/
|
|
16
|
-
entity: Record<string, unknown>;
|
|
17
|
-
/**
|
|
18
|
-
* Current user context (if authenticated)
|
|
19
|
-
*/
|
|
20
|
-
user?: {
|
|
21
|
-
id?: string;
|
|
22
|
-
authenticated?: boolean;
|
|
23
|
-
roles?: string[];
|
|
24
|
-
[key: string]: unknown;
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* The current database operation
|
|
28
|
-
*/
|
|
29
|
-
operation: SemanticOperation;
|
|
30
|
-
/**
|
|
31
|
-
* Applied semantics with their field mappings
|
|
32
|
-
*/
|
|
33
|
-
appliedSemantics: AppliedDataSemantic[];
|
|
34
|
-
/**
|
|
35
|
-
* Configuration for the current semantic being evaluated
|
|
36
|
-
*/
|
|
37
|
-
config?: Record<string, unknown>;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Builds a semantics object for JEXL evaluation that maps semantic types to actual field names.
|
|
41
|
-
*
|
|
42
|
-
* The resulting map should be used in JEXL expressions to reference semantic fields.
|
|
43
|
-
*
|
|
44
|
-
* @returns A map where keys are semantic type identifiers and values are the field names.
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* const semanticFieldMap = buildSemanticFieldMap(entity);
|
|
48
|
-
* const result = await jexl.eval("semantics.CreatedTimestamp == null", {
|
|
49
|
-
* semantics: semanticFieldMap,
|
|
50
|
-
* ...
|
|
51
|
-
* }
|
|
52
|
-
*/
|
|
53
|
-
export declare function buildSemanticFieldMap(entity: DomainEntity): Record<string, string>;
|
|
54
|
-
/**
|
|
55
|
-
* Evaluates a semantic condition against the execution context
|
|
56
|
-
* In a real implementation, this would use JEXL to evaluate the expression
|
|
57
|
-
*
|
|
58
|
-
* @param condition The semantic condition to evaluate
|
|
59
|
-
* @param context The execution context containing entity data, user info, etc.
|
|
60
|
-
* @param semanticFieldMap A map of semantic field names to their actual field names.
|
|
61
|
-
* Use the `buildSemanticFieldMap()` function to create this map.
|
|
62
|
-
* @param jexl Optional JEXL instance to use for evaluation, defaults to a cached instance.
|
|
63
|
-
* @returns A promise that resolves to true if the condition is met, false otherwise
|
|
64
|
-
*/
|
|
65
|
-
export declare function evaluateSemanticCondition(condition: SemanticCondition, context: SemanticExecutionContext, semanticFieldMap: Record<string, string>, jexl?: Jexl): Promise<boolean>;
|
|
66
|
-
/**
|
|
67
|
-
* Check if semantic should execute based on all its conditions
|
|
68
|
-
*
|
|
69
|
-
* @param semantic The semantic to check
|
|
70
|
-
* @param context The execution context containing entity data, user info, etc.
|
|
71
|
-
* @param semanticFieldMap A map of semantic field names to their actual field names.
|
|
72
|
-
* Use the `buildSemanticFieldMap()` function to create this map.
|
|
73
|
-
* @return A promise that resolves to true if all conditions are met, false otherwise
|
|
74
|
-
*/
|
|
75
|
-
export declare function shouldSemanticExecute(semantic: AppliedDataSemantic, context: SemanticExecutionContext, semanticFieldMap: Record<string, string>): Promise<boolean>;
|
|
76
|
-
/**
|
|
77
|
-
* Helper to get the actual field name for a semantic type
|
|
78
|
-
*/
|
|
79
|
-
export declare function getFieldNameForSemantic(semanticType: SemanticType, semanticFieldMap: Record<string, string>): string | undefined;
|
|
80
|
-
/**
|
|
81
|
-
* Validates that all semantic references in conditions are available
|
|
82
|
-
*/
|
|
83
|
-
export declare function validateSemanticConditions(semantic: AppliedDataSemantic, semanticFieldMap: Record<string, string>): string[];
|
|
84
|
-
//# sourceMappingURL=Semantics.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Semantics.d.ts","sourceRoot":"","sources":["../../../../src/runtime/modeling/Semantics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAA;AAClE,OAAO,EACL,KAAK,mBAAmB,EAExB,KAAK,iBAAiB,EACtB,iBAAiB,EACjB,YAAY,EACb,MAAM,6BAA6B,CAAA;AACpC,OAAO,KAAK,EAAE,iBAAiB,E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B,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACvC,MAAM,GAAG,SAAS,CAGpB;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CACxC,QAAQ,EAAE,mBAAmB,EAC7B,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACvC,MAAM,EAAE,CAuBV"}
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { Jexl } from '@pawel-up/jexl/Jexl.js';
|
|
2
|
-
import { DataSemantics, } from '../../modeling/Semantics.js';
|
|
3
|
-
/**
|
|
4
|
-
* Cache for JEXL instances to avoid recreation overhead
|
|
5
|
-
*/
|
|
6
|
-
const jexlCache = new Map();
|
|
7
|
-
/**
|
|
8
|
-
* Get or create a JEXL instance with optional custom transforms
|
|
9
|
-
*/
|
|
10
|
-
export function getJexlInstance(transforms) {
|
|
11
|
-
const cacheKey = transforms ? JSON.stringify(Object.keys(transforms).sort()) : 'default';
|
|
12
|
-
if (!jexlCache.has(cacheKey)) {
|
|
13
|
-
const jexl = new Jexl();
|
|
14
|
-
if (transforms) {
|
|
15
|
-
Object.entries(transforms).forEach(([name, fn]) => {
|
|
16
|
-
jexl.addTransform(name, fn);
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
jexlCache.set(cacheKey, jexl);
|
|
20
|
-
}
|
|
21
|
-
return jexlCache.get(cacheKey);
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Builds a semantics object for JEXL evaluation that maps semantic types to actual field names.
|
|
25
|
-
*
|
|
26
|
-
* The resulting map should be used in JEXL expressions to reference semantic fields.
|
|
27
|
-
*
|
|
28
|
-
* @returns A map where keys are semantic type identifiers and values are the field names.
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* const semanticFieldMap = buildSemanticFieldMap(entity);
|
|
32
|
-
* const result = await jexl.eval("semantics.CreatedTimestamp == null", {
|
|
33
|
-
* semantics: semanticFieldMap,
|
|
34
|
-
* ...
|
|
35
|
-
* }
|
|
36
|
-
*/
|
|
37
|
-
export function buildSemanticFieldMap(entity) {
|
|
38
|
-
const semanticFieldMap = {};
|
|
39
|
-
for (const property of entity.properties) {
|
|
40
|
-
if (!property.info.name) {
|
|
41
|
-
continue; // Skip properties without a name
|
|
42
|
-
}
|
|
43
|
-
for (const semantic of property.semantics) {
|
|
44
|
-
// We use the truncated `semantic.id` as the key, e.g. 'Password' so that it is possible to do something like:
|
|
45
|
-
// `entity[semantics.Password] != null && !entity[semantics.Password].startsWith('$')`
|
|
46
|
-
// Where the `semantics` object is the object returned by this function and
|
|
47
|
-
// passed to the JEXL context.
|
|
48
|
-
const semanticKey = semantic.id.replace('Semantic#', '');
|
|
49
|
-
semanticFieldMap[semanticKey] = property.info.name;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return semanticFieldMap;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Evaluates a semantic condition against the execution context
|
|
56
|
-
* In a real implementation, this would use JEXL to evaluate the expression
|
|
57
|
-
*
|
|
58
|
-
* @param condition The semantic condition to evaluate
|
|
59
|
-
* @param context The execution context containing entity data, user info, etc.
|
|
60
|
-
* @param semanticFieldMap A map of semantic field names to their actual field names.
|
|
61
|
-
* Use the `buildSemanticFieldMap()` function to create this map.
|
|
62
|
-
* @param jexl Optional JEXL instance to use for evaluation, defaults to a cached instance.
|
|
63
|
-
* @returns A promise that resolves to true if the condition is met, false otherwise
|
|
64
|
-
*/
|
|
65
|
-
export function evaluateSemanticCondition(condition, context, semanticFieldMap, jexl = getJexlInstance()) {
|
|
66
|
-
// Build the evaluation context
|
|
67
|
-
const evalContext = {
|
|
68
|
-
entity: context.entity,
|
|
69
|
-
user: context.user,
|
|
70
|
-
operation: context.operation,
|
|
71
|
-
config: context.config,
|
|
72
|
-
semantics: semanticFieldMap,
|
|
73
|
-
};
|
|
74
|
-
return jexl.eval(condition.expression, evalContext);
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Check if semantic should execute based on all its conditions
|
|
78
|
-
*
|
|
79
|
-
* @param semantic The semantic to check
|
|
80
|
-
* @param context The execution context containing entity data, user info, etc.
|
|
81
|
-
* @param semanticFieldMap A map of semantic field names to their actual field names.
|
|
82
|
-
* Use the `buildSemanticFieldMap()` function to create this map.
|
|
83
|
-
* @return A promise that resolves to true if all conditions are met, false otherwise
|
|
84
|
-
*/
|
|
85
|
-
export async function shouldSemanticExecute(semantic, context, semanticFieldMap) {
|
|
86
|
-
const definition = DataSemantics[semantic.id];
|
|
87
|
-
const conditions = definition?.runtime?.conditions;
|
|
88
|
-
if (!conditions || conditions.length === 0) {
|
|
89
|
-
return true;
|
|
90
|
-
}
|
|
91
|
-
const results = await Promise.all(conditions.map((condition) => evaluateSemanticCondition(condition, context, semanticFieldMap)));
|
|
92
|
-
// All conditions must evaluate to true
|
|
93
|
-
return results.every((result) => result === true);
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Helper to get the actual field name for a semantic type
|
|
97
|
-
*/
|
|
98
|
-
export function getFieldNameForSemantic(semanticType, semanticFieldMap) {
|
|
99
|
-
const semanticKey = semanticType.replace('Semantic#', '');
|
|
100
|
-
return semanticFieldMap[semanticKey];
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Validates that all semantic references in conditions are available
|
|
104
|
-
*/
|
|
105
|
-
export function validateSemanticConditions(semantic, semanticFieldMap) {
|
|
106
|
-
const definition = DataSemantics[semantic.id];
|
|
107
|
-
const conditions = definition?.runtime?.conditions || [];
|
|
108
|
-
const errors = [];
|
|
109
|
-
conditions.forEach((condition, index) => {
|
|
110
|
-
// Extract semantic references from the condition expression
|
|
111
|
-
// Look for patterns like "semantics.CreatedTimestamp" or "entity[semantics.Password]"
|
|
112
|
-
const semanticMatches = condition.expression.match(/semantics\.(\w+)/g);
|
|
113
|
-
if (semanticMatches) {
|
|
114
|
-
semanticMatches.forEach((match) => {
|
|
115
|
-
const semanticKey = match.replace('semantics.', '');
|
|
116
|
-
if (!semanticFieldMap[semanticKey]) {
|
|
117
|
-
errors.push(`Condition ${index + 1} references semantic "${semanticKey}" but no field with that semantic was found`);
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
return errors;
|
|
123
|
-
}
|
|
124
|
-
//# sourceMappingURL=Semantics.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Semantics.js","sourceRoot":"","sources":["../../../../src/runtime/modeling/Semantics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAE7C,OAAO,EAEL,aAAa,GAId,MAAM,6BAA6B,CAAA;AAGpC;;GAEG;AACH,MAAM,SAAS,GAAG,IAAI,GAAG,EAAgB,CAAA;AAEzC;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,UAA8C;IAC5E,MAAM,QAAQ,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;IAExF,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE,CAAA;QACvB,IAAI,UAAU,EAAE,CAAC;YACf,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,EAAE;gBAChD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;YAC7B,CAAC,CAAC,CAAA;QACJ,CAAC;QACD,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;IAC/B,CAAC;IAED,OAAO,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAS,CAAA;AACxC,CAAC;AAiCD;;;;;;;;;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,IAAI,CAAC,gBAAgB,CAAC,WAAW,CAAC,EAAE,CAAC;oBACnC,MAAM,CAAC,IAAI,CACT,aAAa,KAAK,GAAG,CAAC,yBAAyB,WAAW,6CAA6C,CACxG,CAAA;gBACH,CAAC;YACH,CAAC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import { Jexl } from '@pawel-up/jexl/Jexl.js'\nimport type { DomainEntity } from '../../modeling/DomainEntity.js'\nimport {\n type AppliedDataSemantic,\n DataSemantics,\n type SemanticCondition,\n SemanticOperation,\n SemanticType,\n} from '../../modeling/Semantics.js'\nimport type { TransformFunction } from '@pawel-up/jexl'\n\n/**\n * Cache for JEXL instances to avoid recreation overhead\n */\nconst jexlCache = new Map<string, Jexl>()\n\n/**\n * Get or create a JEXL instance with optional custom transforms\n */\nexport function getJexlInstance(transforms?: Record<string, TransformFunction>): Jexl {\n const cacheKey = transforms ? JSON.stringify(Object.keys(transforms).sort()) : 'default'\n\n if (!jexlCache.has(cacheKey)) {\n const jexl = new Jexl()\n if (transforms) {\n Object.entries(transforms).forEach(([name, fn]) => {\n jexl.addTransform(name, fn)\n })\n }\n jexlCache.set(cacheKey, jexl)\n }\n\n return jexlCache.get(cacheKey) as Jexl\n}\n\n/**\n * Context object passed to semantic condition evaluation\n */\nexport interface SemanticExecutionContext {\n /**\n * The entity data being processed\n */\n entity: Record<string, unknown>\n /**\n * Current user context (if authenticated)\n */\n user?: {\n id?: string\n authenticated?: boolean\n roles?: string[]\n [key: string]: unknown\n }\n /**\n * The current database operation\n */\n operation: SemanticOperation\n /**\n * Applied semantics with their field mappings\n */\n appliedSemantics: AppliedDataSemantic[]\n /**\n * Configuration for the current semantic being evaluated\n */\n config?: Record<string, unknown>\n}\n\n/**\n * Builds a semantics object for JEXL evaluation that maps semantic types to actual field names.\n *\n * The resulting map should be used in JEXL expressions to reference semantic fields.\n *\n * @returns A map where keys are semantic type identifiers and values are the field names.\n *\n * @example\n * const semanticFieldMap = buildSemanticFieldMap(entity);\n * const result = await jexl.eval(\"semantics.CreatedTimestamp == null\", {\n * semantics: semanticFieldMap,\n * ...\n * }\n */\nexport function buildSemanticFieldMap(entity: DomainEntity): Record<string, string> {\n const semanticFieldMap: Record<string, string> = {}\n\n for (const property of entity.properties) {\n if (!property.info.name) {\n continue // Skip properties without a name\n }\n for (const semantic of property.semantics) {\n // We use the truncated `semantic.id` as the key, e.g. 'Password' so that it is possible to do something like:\n // `entity[semantics.Password] != null && !entity[semantics.Password].startsWith('$')`\n // Where the `semantics` object is the object returned by this function and\n // passed to the JEXL context.\n const semanticKey = semantic.id.replace('Semantic#', '')\n semanticFieldMap[semanticKey] = property.info.name\n }\n }\n return semanticFieldMap\n}\n\n/**\n * Evaluates a semantic condition against the execution context\n * In a real implementation, this would use JEXL to evaluate the expression\n *\n * @param condition The semantic condition to evaluate\n * @param context The execution context containing entity data, user info, etc.\n * @param semanticFieldMap A map of semantic field names to their actual field names.\n * Use the `buildSemanticFieldMap()` function to create this map.\n * @param jexl Optional JEXL instance to use for evaluation, defaults to a cached instance.\n * @returns A promise that resolves to true if the condition is met, false otherwise\n */\nexport function evaluateSemanticCondition(\n condition: SemanticCondition,\n context: SemanticExecutionContext,\n semanticFieldMap: Record<string, string>,\n jexl: Jexl = getJexlInstance()\n): Promise<boolean> {\n // Build the evaluation context\n const evalContext = {\n entity: context.entity,\n user: context.user,\n operation: context.operation,\n config: context.config,\n semantics: semanticFieldMap,\n }\n return jexl.eval(condition.expression, evalContext)\n}\n\n/**\n * Check if semantic should execute based on all its conditions\n *\n * @param semantic The semantic to check\n * @param context The execution context containing entity data, user info, etc.\n * @param semanticFieldMap A map of semantic field names to their actual field names.\n * Use the `buildSemanticFieldMap()` function to create this map.\n * @return A promise that resolves to true if all conditions are met, false otherwise\n */\nexport async function shouldSemanticExecute(\n semantic: AppliedDataSemantic,\n context: SemanticExecutionContext,\n semanticFieldMap: Record<string, string>\n): Promise<boolean> {\n const definition = DataSemantics[semantic.id]\n const conditions = definition?.runtime?.conditions\n\n if (!conditions || conditions.length === 0) {\n return true\n }\n\n const results = await Promise.all(\n conditions.map((condition) => evaluateSemanticCondition(condition, context, semanticFieldMap))\n )\n // All conditions must evaluate to true\n return results.every((result) => result === true)\n}\n\n/**\n * Helper to get the actual field name for a semantic type\n */\nexport function getFieldNameForSemantic(\n semanticType: SemanticType,\n semanticFieldMap: Record<string, string>\n): string | undefined {\n const semanticKey = semanticType.replace('Semantic#', '')\n return semanticFieldMap[semanticKey]\n}\n\n/**\n * Validates that all semantic references in conditions are available\n */\nexport function validateSemanticConditions(\n semantic: AppliedDataSemantic,\n semanticFieldMap: Record<string, string>\n): string[] {\n const definition = DataSemantics[semantic.id]\n const conditions = definition?.runtime?.conditions || []\n const errors: string[] = []\n\n conditions.forEach((condition, index) => {\n // Extract semantic references from the condition expression\n // Look for patterns like \"semantics.CreatedTimestamp\" or \"entity[semantics.Password]\"\n const semanticMatches = condition.expression.match(/semantics\\.(\\w+)/g)\n\n if (semanticMatches) {\n semanticMatches.forEach((match) => {\n const semanticKey = match.replace('semantics.', '')\n if (!semanticFieldMap[semanticKey]) {\n errors.push(\n `Condition ${index + 1} references semantic \"${semanticKey}\" but no field with that semantic was found`\n )\n }\n })\n }\n })\n\n return errors\n}\n"]}
|