@object-ui/core 3.1.5 → 3.3.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.
Files changed (110) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +20 -1
  3. package/dist/actions/ActionRunner.d.ts +9 -0
  4. package/dist/actions/ActionRunner.js +41 -4
  5. package/dist/adapters/ValueDataSource.d.ts +5 -1
  6. package/dist/adapters/ValueDataSource.js +30 -1
  7. package/dist/errors/index.js +2 -3
  8. package/dist/evaluator/ExpressionCache.d.ts +9 -10
  9. package/dist/evaluator/ExpressionCache.js +29 -8
  10. package/dist/evaluator/SafeExpressionParser.d.ts +131 -0
  11. package/dist/evaluator/SafeExpressionParser.js +851 -0
  12. package/dist/evaluator/index.d.ts +1 -0
  13. package/dist/evaluator/index.js +1 -0
  14. package/dist/protocols/DndProtocol.js +2 -14
  15. package/dist/protocols/KeyboardProtocol.js +1 -4
  16. package/dist/protocols/NotificationProtocol.js +3 -13
  17. package/dist/utils/debug.js +2 -1
  18. package/dist/utils/filter-converter.js +25 -5
  19. package/package.json +33 -9
  20. package/.turbo/turbo-build.log +0 -4
  21. package/src/__benchmarks__/core.bench.ts +0 -64
  22. package/src/__tests__/protocols/DndProtocol.test.ts +0 -186
  23. package/src/__tests__/protocols/KeyboardProtocol.test.ts +0 -177
  24. package/src/__tests__/protocols/NotificationProtocol.test.ts +0 -142
  25. package/src/__tests__/protocols/ResponsiveProtocol.test.ts +0 -176
  26. package/src/__tests__/protocols/SharingProtocol.test.ts +0 -188
  27. package/src/actions/ActionEngine.ts +0 -268
  28. package/src/actions/ActionRunner.ts +0 -717
  29. package/src/actions/TransactionManager.ts +0 -521
  30. package/src/actions/UndoManager.ts +0 -215
  31. package/src/actions/__tests__/ActionEngine.test.ts +0 -206
  32. package/src/actions/__tests__/ActionRunner.params.test.ts +0 -134
  33. package/src/actions/__tests__/ActionRunner.test.ts +0 -711
  34. package/src/actions/__tests__/TransactionManager.test.ts +0 -447
  35. package/src/actions/__tests__/UndoManager.test.ts +0 -320
  36. package/src/actions/index.ts +0 -12
  37. package/src/adapters/ApiDataSource.ts +0 -376
  38. package/src/adapters/README.md +0 -180
  39. package/src/adapters/ValueDataSource.ts +0 -438
  40. package/src/adapters/__tests__/ApiDataSource.test.ts +0 -418
  41. package/src/adapters/__tests__/ValueDataSource.test.ts +0 -472
  42. package/src/adapters/__tests__/resolveDataSource.test.ts +0 -144
  43. package/src/adapters/index.ts +0 -15
  44. package/src/adapters/resolveDataSource.ts +0 -79
  45. package/src/builder/__tests__/schema-builder.test.ts +0 -235
  46. package/src/builder/schema-builder.ts +0 -584
  47. package/src/data-scope/DataScopeManager.ts +0 -269
  48. package/src/data-scope/ViewDataProvider.ts +0 -282
  49. package/src/data-scope/__tests__/DataScopeManager.test.ts +0 -211
  50. package/src/data-scope/__tests__/ViewDataProvider.test.ts +0 -270
  51. package/src/data-scope/index.ts +0 -24
  52. package/src/errors/__tests__/errors.test.ts +0 -292
  53. package/src/errors/index.ts +0 -270
  54. package/src/evaluator/ExpressionCache.ts +0 -192
  55. package/src/evaluator/ExpressionContext.ts +0 -118
  56. package/src/evaluator/ExpressionEvaluator.ts +0 -315
  57. package/src/evaluator/FormulaFunctions.ts +0 -398
  58. package/src/evaluator/__tests__/ExpressionCache.test.ts +0 -135
  59. package/src/evaluator/__tests__/ExpressionContext.test.ts +0 -110
  60. package/src/evaluator/__tests__/ExpressionEvaluator.test.ts +0 -131
  61. package/src/evaluator/__tests__/FormulaFunctions.test.ts +0 -447
  62. package/src/evaluator/index.ts +0 -12
  63. package/src/index.ts +0 -38
  64. package/src/protocols/DndProtocol.ts +0 -184
  65. package/src/protocols/KeyboardProtocol.ts +0 -185
  66. package/src/protocols/NotificationProtocol.ts +0 -159
  67. package/src/protocols/ResponsiveProtocol.ts +0 -210
  68. package/src/protocols/SharingProtocol.ts +0 -185
  69. package/src/protocols/index.ts +0 -13
  70. package/src/query/__tests__/query-ast.test.ts +0 -211
  71. package/src/query/__tests__/window-functions.test.ts +0 -275
  72. package/src/query/index.ts +0 -7
  73. package/src/query/query-ast.ts +0 -341
  74. package/src/registry/PluginScopeImpl.ts +0 -259
  75. package/src/registry/PluginSystem.ts +0 -206
  76. package/src/registry/Registry.ts +0 -219
  77. package/src/registry/WidgetRegistry.ts +0 -316
  78. package/src/registry/__tests__/PluginSystem.test.ts +0 -309
  79. package/src/registry/__tests__/Registry.test.ts +0 -293
  80. package/src/registry/__tests__/WidgetRegistry.test.ts +0 -321
  81. package/src/registry/__tests__/plugin-scope-integration.test.ts +0 -283
  82. package/src/theme/ThemeEngine.ts +0 -530
  83. package/src/theme/__tests__/ThemeEngine.test.ts +0 -668
  84. package/src/theme/index.ts +0 -24
  85. package/src/types/index.ts +0 -21
  86. package/src/utils/__tests__/debug-collector.test.ts +0 -102
  87. package/src/utils/__tests__/debug.test.ts +0 -134
  88. package/src/utils/__tests__/expand-fields.test.ts +0 -120
  89. package/src/utils/__tests__/extract-records.test.ts +0 -50
  90. package/src/utils/__tests__/filter-converter.test.ts +0 -118
  91. package/src/utils/__tests__/merge-views-into-objects.test.ts +0 -110
  92. package/src/utils/__tests__/normalize-quick-filter.test.ts +0 -123
  93. package/src/utils/debug-collector.ts +0 -100
  94. package/src/utils/debug.ts +0 -147
  95. package/src/utils/expand-fields.ts +0 -76
  96. package/src/utils/extract-records.ts +0 -33
  97. package/src/utils/filter-converter.ts +0 -133
  98. package/src/utils/merge-views-into-objects.ts +0 -36
  99. package/src/utils/normalize-quick-filter.ts +0 -78
  100. package/src/validation/__tests__/object-validation-engine.test.ts +0 -567
  101. package/src/validation/__tests__/schema-validator.test.ts +0 -118
  102. package/src/validation/__tests__/validation-engine.test.ts +0 -102
  103. package/src/validation/index.ts +0 -10
  104. package/src/validation/schema-validator.ts +0 -344
  105. package/src/validation/validation-engine.ts +0 -528
  106. package/src/validation/validators/index.ts +0 -25
  107. package/src/validation/validators/object-validation-engine.ts +0 -722
  108. package/tsconfig.json +0 -15
  109. package/tsconfig.tsbuildinfo +0 -1
  110. package/vitest.config.ts +0 -2
@@ -1,78 +0,0 @@
1
- /**
2
- * ObjectUI
3
- * Copyright (c) 2024-present ObjectStack Inc.
4
- *
5
- * This source code is licensed under the MIT license found in the
6
- * LICENSE file in the root directory of this source tree.
7
- */
8
-
9
- /**
10
- * QuickFilter Normalization Utility
11
- *
12
- * Adapter layer that converts between two QuickFilter formats:
13
- * - Spec format: { field, operator, value }
14
- * - ObjectUI format: { id, label, filters[], icon?, defaultActive? }
15
- *
16
- * Both formats are accepted; spec-format items are auto-converted to ObjectUI format.
17
- */
18
-
19
- import type { QuickFilterItem, ObjectUIQuickFilterItem } from '@object-ui/types';
20
-
21
- /** Normalized ObjectUI QuickFilter shape (output of normalizeQuickFilter) */
22
- export type NormalizedQuickFilter = ObjectUIQuickFilterItem;
23
-
24
- /**
25
- * Map a human-readable / spec operator to the ObjectStack AST operator.
26
- */
27
- function mapSpecOperator(op: string): string {
28
- switch (op) {
29
- case 'equals': case 'eq': return '=';
30
- case 'notEquals': case 'ne': case 'neq': return '!=';
31
- case 'contains': return 'contains';
32
- case 'notContains': return 'notcontains';
33
- case 'greaterThan': case 'gt': return '>';
34
- case 'greaterOrEqual': case 'gte': return '>=';
35
- case 'lessThan': case 'lt': return '<';
36
- case 'lessOrEqual': case 'lte': return '<=';
37
- case 'in': return 'in';
38
- case 'notIn': return 'not in';
39
- case 'before': return '<';
40
- case 'after': return '>';
41
- default: return op;
42
- }
43
- }
44
-
45
- /**
46
- * Normalize a single QuickFilter item.
47
- * - If it's already in ObjectUI format (has id + label + filters), return as-is.
48
- * - If it's in Spec format (has field + operator), convert to ObjectUI format.
49
- */
50
- export function normalizeQuickFilter(item: QuickFilterItem): NormalizedQuickFilter {
51
- // Already in ObjectUI format
52
- if ('id' in item && 'filters' in item && item.label && Array.isArray(item.filters)) {
53
- return item as NormalizedQuickFilter;
54
- }
55
- // Spec format: { field, operator, value }
56
- if ('field' in item && 'operator' in item) {
57
- const op = mapSpecOperator(item.operator);
58
- return {
59
- id: `${item.field}-${item.operator}-${String(item.value ?? '')}`,
60
- label: item.label || `${item.field} ${item.operator} ${String(item.value ?? '')}`,
61
- filters: [[item.field, op, item.value]],
62
- icon: item.icon,
63
- defaultActive: item.defaultActive,
64
- };
65
- }
66
- // Unknown format — return as-is
67
- return item as NormalizedQuickFilter;
68
- }
69
-
70
- /**
71
- * Normalize an array of QuickFilter items (mixed formats accepted).
72
- */
73
- export function normalizeQuickFilters(
74
- items: QuickFilterItem[] | undefined,
75
- ): NormalizedQuickFilter[] | undefined {
76
- if (!items || items.length === 0) return undefined;
77
- return items.map(normalizeQuickFilter);
78
- }
@@ -1,567 +0,0 @@
1
- /**
2
- * ObjectUI
3
- * Copyright (c) 2024-present ObjectStack Inc.
4
- *
5
- * This source code is licensed under the MIT license found in the
6
- * LICENSE file in the root directory of this source tree.
7
- */
8
-
9
- /**
10
- * @object-ui/core - Object Validation Engine Tests
11
- *
12
- * Tests for ObjectStack Spec v2.0.1 object-level validation
13
- */
14
-
15
- import { describe, it, expect, vi } from 'vitest';
16
- import { ObjectValidationEngine, type ObjectValidationContext } from '../validators/object-validation-engine';
17
- import type {
18
- ScriptValidation,
19
- UniquenessValidation,
20
- StateMachineValidation,
21
- CrossFieldValidation,
22
- AsyncValidation,
23
- ConditionalValidation,
24
- FormatValidation,
25
- RangeValidation,
26
- } from '@object-ui/types';
27
-
28
- describe('ObjectValidationEngine', () => {
29
- describe('ScriptValidation', () => {
30
- it('should validate when script condition is true', async () => {
31
- const engine = new ObjectValidationEngine();
32
- const rule: ScriptValidation = {
33
- type: 'script',
34
- name: 'age_check',
35
- label: 'Age Check',
36
- active: true,
37
- events: ['insert', 'update'],
38
- severity: 'error',
39
- message: 'Must be 18 or older',
40
- condition: 'age >= 18',
41
- };
42
-
43
- const context: ObjectValidationContext = {
44
- record: { age: 25 },
45
- };
46
-
47
- const results = await engine.validateRecord([rule], context, 'insert');
48
- expect(results).toHaveLength(0); // No errors
49
- });
50
-
51
- it('should fail validation when script condition is false', async () => {
52
- const engine = new ObjectValidationEngine();
53
- const rule: ScriptValidation = {
54
- type: 'script',
55
- name: 'age_check',
56
- active: true,
57
- events: ['insert', 'update'],
58
- severity: 'error',
59
- message: 'Must be 18 or older',
60
- condition: 'age >= 18',
61
- };
62
-
63
- const context: ObjectValidationContext = {
64
- record: { age: 16 },
65
- };
66
-
67
- const results = await engine.validateRecord([rule], context, 'insert');
68
- expect(results).toHaveLength(1);
69
- expect(results[0]).toMatchObject({
70
- valid: false,
71
- message: 'Must be 18 or older',
72
- rule: 'age_check',
73
- severity: 'error',
74
- });
75
- });
76
-
77
- it('should support complex script conditions', async () => {
78
- const engine = new ObjectValidationEngine();
79
- const rule: ScriptValidation = {
80
- type: 'script',
81
- name: 'discount_check',
82
- active: true,
83
- events: ['insert', 'update'],
84
- severity: 'error',
85
- message: 'Discount cannot exceed 50% for non-premium customers',
86
- condition: 'discount <= 50 || is_premium === true',
87
- };
88
-
89
- const context1: ObjectValidationContext = {
90
- record: { discount: 30, is_premium: false },
91
- };
92
- const results1 = await engine.validateRecord([rule], context1, 'insert');
93
- expect(results1).toHaveLength(0);
94
-
95
- const context2: ObjectValidationContext = {
96
- record: { discount: 75, is_premium: true },
97
- };
98
- const results2 = await engine.validateRecord([rule], context2, 'insert');
99
- expect(results2).toHaveLength(0);
100
-
101
- const context3: ObjectValidationContext = {
102
- record: { discount: 75, is_premium: false },
103
- };
104
- const results3 = await engine.validateRecord([rule], context3, 'insert');
105
- expect(results3).toHaveLength(1);
106
- });
107
- });
108
-
109
- describe('UniquenessValidation', () => {
110
- it('should validate uniqueness using custom checker', async () => {
111
- const uniquenessChecker = vi.fn().mockResolvedValue(true);
112
- const engine = new ObjectValidationEngine(undefined, uniquenessChecker);
113
-
114
- const rule: UniquenessValidation = {
115
- type: 'unique',
116
- name: 'unique_email',
117
- active: true,
118
- events: ['insert', 'update'],
119
- severity: 'error',
120
- message: 'Email must be unique',
121
- fields: ['email'],
122
- };
123
-
124
- const context: ObjectValidationContext = {
125
- record: { email: 'user@example.com' },
126
- };
127
-
128
- const results = await engine.validateRecord([rule], context, 'insert');
129
- expect(results).toHaveLength(0);
130
- expect(uniquenessChecker).toHaveBeenCalledWith(
131
- ['email'],
132
- { email: 'user@example.com' },
133
- undefined,
134
- context
135
- );
136
- });
137
-
138
- it('should fail when uniqueness check fails', async () => {
139
- const uniquenessChecker = vi.fn().mockResolvedValue(false);
140
- const engine = new ObjectValidationEngine(undefined, uniquenessChecker);
141
-
142
- const rule: UniquenessValidation = {
143
- type: 'unique',
144
- name: 'unique_email',
145
- active: true,
146
- events: ['insert', 'update'],
147
- severity: 'error',
148
- message: 'Email must be unique',
149
- fields: ['email'],
150
- };
151
-
152
- const context: ObjectValidationContext = {
153
- record: { email: 'duplicate@example.com' },
154
- };
155
-
156
- const results = await engine.validateRecord([rule], context, 'insert');
157
- expect(results).toHaveLength(1);
158
- expect(results[0]).toMatchObject({
159
- valid: false,
160
- message: 'Email must be unique',
161
- });
162
- });
163
-
164
- it('should support multi-field uniqueness', async () => {
165
- const uniquenessChecker = vi.fn().mockResolvedValue(true);
166
- const engine = new ObjectValidationEngine(undefined, uniquenessChecker);
167
-
168
- const rule: UniquenessValidation = {
169
- type: 'unique',
170
- name: 'unique_email_tenant',
171
- active: true,
172
- events: ['insert', 'update'],
173
- severity: 'error',
174
- message: 'Email must be unique within tenant',
175
- fields: ['email', 'tenant_id'],
176
- };
177
-
178
- const context: ObjectValidationContext = {
179
- record: { email: 'user@example.com', tenant_id: 'tenant-123' },
180
- };
181
-
182
- await engine.validateRecord([rule], context, 'insert');
183
- expect(uniquenessChecker).toHaveBeenCalledWith(
184
- ['email', 'tenant_id'],
185
- { email: 'user@example.com', tenant_id: 'tenant-123' },
186
- undefined,
187
- context
188
- );
189
- });
190
- });
191
-
192
- describe('StateMachineValidation', () => {
193
- it('should allow valid state transition', async () => {
194
- const engine = new ObjectValidationEngine();
195
- const rule: StateMachineValidation = {
196
- type: 'state_machine',
197
- name: 'order_status_flow',
198
- active: true,
199
- events: ['update'],
200
- severity: 'error',
201
- message: 'Invalid status transition',
202
- stateField: 'status',
203
- transitions: [
204
- { from: 'draft', to: 'submitted' },
205
- { from: 'submitted', to: 'approved' },
206
- { from: 'submitted', to: 'rejected' },
207
- { from: 'approved', to: 'completed' },
208
- ],
209
- };
210
-
211
- const context: ObjectValidationContext = {
212
- record: { status: 'submitted' },
213
- oldRecord: { status: 'draft' },
214
- };
215
-
216
- const results = await engine.validateRecord([rule], context, 'update');
217
- expect(results).toHaveLength(0);
218
- });
219
-
220
- it('should prevent invalid state transition', async () => {
221
- const engine = new ObjectValidationEngine();
222
- const rule: StateMachineValidation = {
223
- type: 'state_machine',
224
- name: 'order_status_flow',
225
- active: true,
226
- events: ['update'],
227
- severity: 'error',
228
- message: 'Invalid status transition',
229
- stateField: 'status',
230
- transitions: [
231
- { from: 'draft', to: 'submitted' },
232
- { from: 'submitted', to: 'approved' },
233
- ],
234
- };
235
-
236
- const context: ObjectValidationContext = {
237
- record: { status: 'approved' },
238
- oldRecord: { status: 'draft' },
239
- };
240
-
241
- const results = await engine.validateRecord([rule], context, 'update');
242
- expect(results).toHaveLength(1);
243
- expect(results[0]).toMatchObject({
244
- valid: false,
245
- });
246
- expect(results[0].message).toContain('transition');
247
- });
248
-
249
- it('should support conditional state transitions', async () => {
250
- const engine = new ObjectValidationEngine();
251
- const rule: StateMachineValidation = {
252
- type: 'state_machine',
253
- name: 'conditional_transition',
254
- active: true,
255
- events: ['update'],
256
- severity: 'error',
257
- message: 'Invalid transition',
258
- stateField: 'status',
259
- transitions: [
260
- {
261
- from: 'pending',
262
- to: 'approved',
263
- condition: 'amount < 1000',
264
- },
265
- ],
266
- };
267
-
268
- const context1: ObjectValidationContext = {
269
- record: { status: 'approved', amount: 500 },
270
- oldRecord: { status: 'pending', amount: 500 },
271
- };
272
- const results1 = await engine.validateRecord([rule], context1, 'update');
273
- expect(results1).toHaveLength(0);
274
-
275
- const context2: ObjectValidationContext = {
276
- record: { status: 'approved', amount: 2000 },
277
- oldRecord: { status: 'pending', amount: 2000 },
278
- };
279
- const results2 = await engine.validateRecord([rule], context2, 'update');
280
- expect(results2).toHaveLength(1);
281
- });
282
- });
283
-
284
- describe('CrossFieldValidation', () => {
285
- it('should validate cross-field constraints', async () => {
286
- const engine = new ObjectValidationEngine();
287
- const rule: CrossFieldValidation = {
288
- type: 'cross_field',
289
- name: 'date_range',
290
- active: true,
291
- events: ['insert', 'update'],
292
- severity: 'error',
293
- message: 'End date must be after start date',
294
- fields: ['start_date', 'end_date'],
295
- condition: 'end_date > start_date',
296
- };
297
-
298
- const context1: ObjectValidationContext = {
299
- record: { start_date: new Date('2024-01-01'), end_date: new Date('2024-12-31') },
300
- };
301
- const results1 = await engine.validateRecord([rule], context1, 'insert');
302
- expect(results1).toHaveLength(0);
303
-
304
- const context2: ObjectValidationContext = {
305
- record: { start_date: new Date('2024-12-31'), end_date: new Date('2024-01-01') },
306
- };
307
- const results2 = await engine.validateRecord([rule], context2, 'insert');
308
- expect(results2).toHaveLength(1);
309
- });
310
- });
311
-
312
- describe('FormatValidation', () => {
313
- it('should validate email format', async () => {
314
- const engine = new ObjectValidationEngine();
315
- const rule: FormatValidation = {
316
- type: 'format',
317
- name: 'email_format',
318
- active: true,
319
- events: ['insert', 'update'],
320
- severity: 'error',
321
- message: 'Invalid email format',
322
- field: 'email',
323
- format: 'email',
324
- };
325
-
326
- const context1: ObjectValidationContext = {
327
- record: { email: 'user@example.com' },
328
- };
329
- const results1 = await engine.validateRecord([rule], context1, 'insert');
330
- expect(results1).toHaveLength(0);
331
-
332
- const context2: ObjectValidationContext = {
333
- record: { email: 'invalid-email' },
334
- };
335
- const results2 = await engine.validateRecord([rule], context2, 'insert');
336
- expect(results2).toHaveLength(1);
337
- });
338
-
339
- it('should validate URL format', async () => {
340
- const engine = new ObjectValidationEngine();
341
- const rule: FormatValidation = {
342
- type: 'format',
343
- name: 'url_format',
344
- active: true,
345
- events: ['insert', 'update'],
346
- severity: 'error',
347
- message: 'Invalid URL format',
348
- field: 'website',
349
- format: 'url',
350
- };
351
-
352
- const context1: ObjectValidationContext = {
353
- record: { website: 'https://example.com' },
354
- };
355
- const results1 = await engine.validateRecord([rule], context1, 'insert');
356
- expect(results1).toHaveLength(0);
357
-
358
- const context2: ObjectValidationContext = {
359
- record: { website: 'not-a-url' },
360
- };
361
- const results2 = await engine.validateRecord([rule], context2, 'insert');
362
- expect(results2).toHaveLength(1);
363
- });
364
-
365
- it('should validate custom regex pattern', async () => {
366
- const engine = new ObjectValidationEngine();
367
- const rule: FormatValidation = {
368
- type: 'format',
369
- name: 'custom_pattern',
370
- active: true,
371
- events: ['insert', 'update'],
372
- severity: 'error',
373
- message: 'Must be 3 uppercase letters',
374
- field: 'code',
375
- pattern: '^[A-Z]{3}$',
376
- };
377
-
378
- const context1: ObjectValidationContext = {
379
- record: { code: 'ABC' },
380
- };
381
- const results1 = await engine.validateRecord([rule], context1, 'insert');
382
- expect(results1).toHaveLength(0);
383
-
384
- const context2: ObjectValidationContext = {
385
- record: { code: 'ab' },
386
- };
387
- const results2 = await engine.validateRecord([rule], context2, 'insert');
388
- expect(results2).toHaveLength(1);
389
- });
390
- });
391
-
392
- describe('RangeValidation', () => {
393
- it('should validate numeric ranges', async () => {
394
- const engine = new ObjectValidationEngine();
395
- const rule: RangeValidation = {
396
- type: 'range',
397
- name: 'age_range',
398
- active: true,
399
- events: ['insert', 'update'],
400
- severity: 'error',
401
- message: 'Age must be between 18 and 65',
402
- field: 'age',
403
- min: 18,
404
- max: 65,
405
- };
406
-
407
- const context1: ObjectValidationContext = {
408
- record: { age: 30 },
409
- };
410
- const results1 = await engine.validateRecord([rule], context1, 'insert');
411
- expect(results1).toHaveLength(0);
412
-
413
- const context2: ObjectValidationContext = {
414
- record: { age: 16 },
415
- };
416
- const results2 = await engine.validateRecord([rule], context2, 'insert');
417
- expect(results2).toHaveLength(1);
418
-
419
- const context3: ObjectValidationContext = {
420
- record: { age: 70 },
421
- };
422
- const results3 = await engine.validateRecord([rule], context3, 'insert');
423
- expect(results3).toHaveLength(1);
424
- });
425
-
426
- it('should validate date ranges', async () => {
427
- const engine = new ObjectValidationEngine();
428
- const rule: RangeValidation = {
429
- type: 'range',
430
- name: 'date_range',
431
- active: true,
432
- events: ['insert', 'update'],
433
- severity: 'error',
434
- message: 'Date must be in 2024',
435
- field: 'event_date',
436
- min: new Date('2024-01-01'),
437
- max: new Date('2024-12-31'),
438
- };
439
-
440
- const context1: ObjectValidationContext = {
441
- record: { event_date: new Date('2024-06-15') },
442
- };
443
- const results1 = await engine.validateRecord([rule], context1, 'insert');
444
- expect(results1).toHaveLength(0);
445
-
446
- const context2: ObjectValidationContext = {
447
- record: { event_date: new Date('2025-01-01') },
448
- };
449
- const results2 = await engine.validateRecord([rule], context2, 'insert');
450
- expect(results2).toHaveLength(1);
451
- });
452
- });
453
-
454
- describe('ConditionalValidation', () => {
455
- it('should apply nested rules when condition is met', async () => {
456
- const engine = new ObjectValidationEngine();
457
- const rule: ConditionalValidation = {
458
- type: 'conditional',
459
- name: 'conditional_validation',
460
- active: true,
461
- events: ['insert', 'update'],
462
- severity: 'error',
463
- message: 'Conditional validation',
464
- condition: 'is_company === true',
465
- rules: [
466
- {
467
- type: 'script',
468
- name: 'company_name_required',
469
- active: true,
470
- events: ['insert', 'update'],
471
- severity: 'error',
472
- message: 'Company name is required',
473
- condition: 'company_name !== null && company_name !== ""',
474
- },
475
- ],
476
- };
477
-
478
- const context1: ObjectValidationContext = {
479
- record: { is_company: true, company_name: 'Acme Corp' },
480
- };
481
- const results1 = await engine.validateRecord([rule], context1, 'insert');
482
- expect(results1).toHaveLength(0);
483
-
484
- const context2: ObjectValidationContext = {
485
- record: { is_company: true, company_name: '' },
486
- };
487
- const results2 = await engine.validateRecord([rule], context2, 'insert');
488
- expect(results2).toHaveLength(1);
489
-
490
- const context3: ObjectValidationContext = {
491
- record: { is_company: false, company_name: '' },
492
- };
493
- const results3 = await engine.validateRecord([rule], context3, 'insert');
494
- expect(results3).toHaveLength(0); // Condition not met, rules not applied
495
- });
496
- });
497
-
498
- describe('Event Filtering', () => {
499
- it('should only run rules for matching events', async () => {
500
- const engine = new ObjectValidationEngine();
501
- const rule: ScriptValidation = {
502
- type: 'script',
503
- name: 'insert_only',
504
- active: true,
505
- events: ['insert'],
506
- severity: 'error',
507
- message: 'Validation message',
508
- condition: 'value > 0',
509
- };
510
-
511
- const context: ObjectValidationContext = {
512
- record: { value: -1 },
513
- };
514
-
515
- const insertResults = await engine.validateRecord([rule], context, 'insert');
516
- expect(insertResults).toHaveLength(1);
517
-
518
- const updateResults = await engine.validateRecord([rule], context, 'update');
519
- expect(updateResults).toHaveLength(0); // Rule not applied for update
520
- });
521
- });
522
-
523
- describe('Active Flag', () => {
524
- it('should skip inactive rules', async () => {
525
- const engine = new ObjectValidationEngine();
526
- const rule: ScriptValidation = {
527
- type: 'script',
528
- name: 'inactive_rule',
529
- active: false,
530
- events: ['insert', 'update'],
531
- severity: 'error',
532
- message: 'Validation message',
533
- condition: 'false',
534
- };
535
-
536
- const context: ObjectValidationContext = {
537
- record: { value: 1 },
538
- };
539
-
540
- const results = await engine.validateRecord([rule], context, 'insert');
541
- expect(results).toHaveLength(0); // Rule skipped because inactive
542
- });
543
- });
544
-
545
- describe('Severity Levels', () => {
546
- it('should return validation results with correct severity', async () => {
547
- const engine = new ObjectValidationEngine();
548
- const warningRule: ScriptValidation = {
549
- type: 'script',
550
- name: 'warning_check',
551
- active: true,
552
- events: ['insert', 'update'],
553
- severity: 'warning',
554
- message: 'This is a warning',
555
- condition: 'false',
556
- };
557
-
558
- const context: ObjectValidationContext = {
559
- record: {},
560
- };
561
-
562
- const results = await engine.validateRecord([warningRule], context, 'insert');
563
- expect(results).toHaveLength(1);
564
- expect(results[0].severity).toBe('warning');
565
- });
566
- });
567
- });