@object-ui/core 0.3.0 → 0.5.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.
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +8 -0
- package/dist/actions/ActionRunner.d.ts +40 -0
- package/dist/actions/ActionRunner.js +160 -0
- package/dist/actions/index.d.ts +8 -0
- package/dist/actions/index.js +8 -0
- package/dist/adapters/index.d.ts +7 -0
- package/dist/adapters/index.js +10 -0
- package/dist/builder/schema-builder.d.ts +7 -0
- package/dist/builder/schema-builder.js +4 -6
- package/dist/evaluator/ExpressionCache.d.ts +101 -0
- package/dist/evaluator/ExpressionCache.js +135 -0
- package/dist/evaluator/ExpressionContext.d.ts +51 -0
- package/dist/evaluator/ExpressionContext.js +110 -0
- package/dist/evaluator/ExpressionEvaluator.d.ts +117 -0
- package/dist/evaluator/ExpressionEvaluator.js +220 -0
- package/dist/evaluator/index.d.ts +10 -0
- package/dist/evaluator/index.js +10 -0
- package/dist/index.d.ts +17 -4
- package/dist/index.js +16 -5
- package/dist/query/index.d.ts +6 -0
- package/dist/query/index.js +6 -0
- package/dist/query/query-ast.d.ts +32 -0
- package/dist/query/query-ast.js +268 -0
- package/dist/registry/PluginScopeImpl.d.ts +80 -0
- package/dist/registry/PluginScopeImpl.js +243 -0
- package/dist/registry/PluginSystem.d.ts +66 -0
- package/dist/registry/PluginSystem.js +142 -0
- package/dist/registry/Registry.d.ts +80 -4
- package/dist/registry/Registry.js +119 -7
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.js +7 -0
- package/dist/utils/filter-converter.d.ts +57 -0
- package/dist/utils/filter-converter.js +100 -0
- package/dist/validation/index.d.ts +9 -0
- package/dist/validation/index.js +9 -0
- package/dist/validation/schema-validator.d.ts +7 -0
- package/dist/validation/schema-validator.js +4 -6
- package/dist/validation/validation-engine.d.ts +70 -0
- package/dist/validation/validation-engine.js +363 -0
- package/dist/validation/validators/index.d.ts +16 -0
- package/dist/validation/validators/index.js +16 -0
- package/dist/validation/validators/object-validation-engine.d.ts +118 -0
- package/dist/validation/validators/object-validation-engine.js +538 -0
- package/package.json +26 -7
- package/src/actions/ActionRunner.ts +195 -0
- package/src/actions/index.ts +9 -0
- package/src/adapters/README.md +180 -0
- package/src/adapters/index.ts +10 -0
- package/src/builder/schema-builder.ts +8 -0
- package/src/evaluator/ExpressionCache.ts +192 -0
- package/src/evaluator/ExpressionContext.ts +118 -0
- package/src/evaluator/ExpressionEvaluator.ts +267 -0
- package/src/evaluator/__tests__/ExpressionCache.test.ts +135 -0
- package/src/evaluator/__tests__/ExpressionEvaluator.test.ts +101 -0
- package/src/evaluator/index.ts +11 -0
- package/src/index.test.ts +8 -0
- package/src/index.ts +18 -5
- package/src/query/__tests__/query-ast.test.ts +211 -0
- package/src/query/__tests__/window-functions.test.ts +275 -0
- package/src/query/index.ts +7 -0
- package/src/query/query-ast.ts +341 -0
- package/src/registry/PluginScopeImpl.ts +259 -0
- package/src/registry/PluginSystem.ts +161 -0
- package/src/registry/Registry.ts +133 -8
- package/src/registry/__tests__/PluginSystem.test.ts +226 -0
- package/src/registry/__tests__/Registry.test.ts +293 -0
- package/src/registry/__tests__/plugin-scope-integration.test.ts +283 -0
- package/src/types/index.ts +8 -0
- package/src/utils/__tests__/filter-converter.test.ts +118 -0
- package/src/utils/filter-converter.ts +133 -0
- package/src/validation/__tests__/object-validation-engine.test.ts +567 -0
- package/src/validation/__tests__/validation-engine.test.ts +102 -0
- package/src/validation/index.ts +10 -0
- package/src/validation/schema-validator.ts +8 -0
- package/src/validation/validation-engine.ts +461 -0
- package/src/validation/validators/index.ts +25 -0
- package/src/validation/validators/object-validation-engine.ts +722 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +2 -0
- package/src/builder/schema-builder.d.ts +0 -287
- package/src/builder/schema-builder.js +0 -505
- package/src/index.d.ts +0 -4
- package/src/index.js +0 -7
- package/src/registry/Registry.d.ts +0 -49
- package/src/registry/Registry.js +0 -36
- package/src/types/index.d.ts +0 -12
- package/src/types/index.js +0 -1
- package/src/validation/schema-validator.d.ts +0 -87
- package/src/validation/schema-validator.js +0 -280
|
@@ -0,0 +1,567 @@
|
|
|
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 v0.7.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
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @object-ui/core - Validation Engine Tests
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect } from 'vitest';
|
|
6
|
+
import { ValidationEngine } from '../validation-engine';
|
|
7
|
+
import type { AdvancedValidationSchema } from '@object-ui/types';
|
|
8
|
+
|
|
9
|
+
describe('ValidationEngine', () => {
|
|
10
|
+
const engine = new ValidationEngine();
|
|
11
|
+
|
|
12
|
+
describe('Basic Validation', () => {
|
|
13
|
+
it('should validate required field', async () => {
|
|
14
|
+
const schema: AdvancedValidationSchema = {
|
|
15
|
+
field: 'email',
|
|
16
|
+
rules: [
|
|
17
|
+
{
|
|
18
|
+
type: 'required',
|
|
19
|
+
message: 'Email is required',
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const result1 = await engine.validate('', schema);
|
|
25
|
+
expect(result1.valid).toBe(false);
|
|
26
|
+
expect(result1.errors).toHaveLength(1);
|
|
27
|
+
expect(result1.errors[0].message).toBe('Email is required');
|
|
28
|
+
|
|
29
|
+
const result2 = await engine.validate('test@example.com', schema);
|
|
30
|
+
expect(result2.valid).toBe(true);
|
|
31
|
+
expect(result2.errors).toHaveLength(0);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should validate email format', async () => {
|
|
35
|
+
const schema: AdvancedValidationSchema = {
|
|
36
|
+
field: 'email',
|
|
37
|
+
rules: [
|
|
38
|
+
{
|
|
39
|
+
type: 'email',
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const result1 = await engine.validate('invalid-email', schema);
|
|
45
|
+
expect(result1.valid).toBe(false);
|
|
46
|
+
|
|
47
|
+
const result2 = await engine.validate('valid@example.com', schema);
|
|
48
|
+
expect(result2.valid).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should validate phone format', async () => {
|
|
52
|
+
const schema: AdvancedValidationSchema = {
|
|
53
|
+
field: 'phone',
|
|
54
|
+
rules: [
|
|
55
|
+
{
|
|
56
|
+
type: 'phone',
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Valid phone numbers with various formats
|
|
62
|
+
const validResult1 = await engine.validate('123-456-7890', schema);
|
|
63
|
+
expect(validResult1.valid).toBe(true);
|
|
64
|
+
|
|
65
|
+
const validResult2 = await engine.validate('+1 (234) 567-8900', schema);
|
|
66
|
+
expect(validResult2.valid).toBe(true);
|
|
67
|
+
|
|
68
|
+
const validResult3 = await engine.validate('1234567890', schema);
|
|
69
|
+
expect(validResult3.valid).toBe(true);
|
|
70
|
+
|
|
71
|
+
// Invalid phone number with letters
|
|
72
|
+
const invalidResult = await engine.validate('123-abc-7890', schema);
|
|
73
|
+
expect(invalidResult.valid).toBe(false);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('Custom Validation', () => {
|
|
78
|
+
it('should validate with custom sync function', async () => {
|
|
79
|
+
const schema: AdvancedValidationSchema = {
|
|
80
|
+
field: 'custom',
|
|
81
|
+
rules: [
|
|
82
|
+
{
|
|
83
|
+
type: 'custom',
|
|
84
|
+
validator: (value) => {
|
|
85
|
+
if (value === 'forbidden') {
|
|
86
|
+
return 'This value is forbidden';
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const result1 = await engine.validate('forbidden', schema);
|
|
95
|
+
expect(result1.valid).toBe(false);
|
|
96
|
+
expect(result1.errors[0].message).toBe('This value is forbidden');
|
|
97
|
+
|
|
98
|
+
const result2 = await engine.validate('allowed', schema);
|
|
99
|
+
expect(result2.valid).toBe(true);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @object-ui/core - Validation Module
|
|
3
|
+
*
|
|
4
|
+
* Phase 3.5: Validation engine
|
|
5
|
+
* ObjectStack Spec v0.7.1: Object-level validation
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from './validation-engine.js';
|
|
9
|
+
export * from './schema-validator.js';
|
|
10
|
+
export * from './validators/index.js';
|