@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.
Files changed (90) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +8 -0
  3. package/dist/actions/ActionRunner.d.ts +40 -0
  4. package/dist/actions/ActionRunner.js +160 -0
  5. package/dist/actions/index.d.ts +8 -0
  6. package/dist/actions/index.js +8 -0
  7. package/dist/adapters/index.d.ts +7 -0
  8. package/dist/adapters/index.js +10 -0
  9. package/dist/builder/schema-builder.d.ts +7 -0
  10. package/dist/builder/schema-builder.js +4 -6
  11. package/dist/evaluator/ExpressionCache.d.ts +101 -0
  12. package/dist/evaluator/ExpressionCache.js +135 -0
  13. package/dist/evaluator/ExpressionContext.d.ts +51 -0
  14. package/dist/evaluator/ExpressionContext.js +110 -0
  15. package/dist/evaluator/ExpressionEvaluator.d.ts +117 -0
  16. package/dist/evaluator/ExpressionEvaluator.js +220 -0
  17. package/dist/evaluator/index.d.ts +10 -0
  18. package/dist/evaluator/index.js +10 -0
  19. package/dist/index.d.ts +17 -4
  20. package/dist/index.js +16 -5
  21. package/dist/query/index.d.ts +6 -0
  22. package/dist/query/index.js +6 -0
  23. package/dist/query/query-ast.d.ts +32 -0
  24. package/dist/query/query-ast.js +268 -0
  25. package/dist/registry/PluginScopeImpl.d.ts +80 -0
  26. package/dist/registry/PluginScopeImpl.js +243 -0
  27. package/dist/registry/PluginSystem.d.ts +66 -0
  28. package/dist/registry/PluginSystem.js +142 -0
  29. package/dist/registry/Registry.d.ts +80 -4
  30. package/dist/registry/Registry.js +119 -7
  31. package/dist/types/index.d.ts +7 -0
  32. package/dist/types/index.js +7 -0
  33. package/dist/utils/filter-converter.d.ts +57 -0
  34. package/dist/utils/filter-converter.js +100 -0
  35. package/dist/validation/index.d.ts +9 -0
  36. package/dist/validation/index.js +9 -0
  37. package/dist/validation/schema-validator.d.ts +7 -0
  38. package/dist/validation/schema-validator.js +4 -6
  39. package/dist/validation/validation-engine.d.ts +70 -0
  40. package/dist/validation/validation-engine.js +363 -0
  41. package/dist/validation/validators/index.d.ts +16 -0
  42. package/dist/validation/validators/index.js +16 -0
  43. package/dist/validation/validators/object-validation-engine.d.ts +118 -0
  44. package/dist/validation/validators/object-validation-engine.js +538 -0
  45. package/package.json +26 -7
  46. package/src/actions/ActionRunner.ts +195 -0
  47. package/src/actions/index.ts +9 -0
  48. package/src/adapters/README.md +180 -0
  49. package/src/adapters/index.ts +10 -0
  50. package/src/builder/schema-builder.ts +8 -0
  51. package/src/evaluator/ExpressionCache.ts +192 -0
  52. package/src/evaluator/ExpressionContext.ts +118 -0
  53. package/src/evaluator/ExpressionEvaluator.ts +267 -0
  54. package/src/evaluator/__tests__/ExpressionCache.test.ts +135 -0
  55. package/src/evaluator/__tests__/ExpressionEvaluator.test.ts +101 -0
  56. package/src/evaluator/index.ts +11 -0
  57. package/src/index.test.ts +8 -0
  58. package/src/index.ts +18 -5
  59. package/src/query/__tests__/query-ast.test.ts +211 -0
  60. package/src/query/__tests__/window-functions.test.ts +275 -0
  61. package/src/query/index.ts +7 -0
  62. package/src/query/query-ast.ts +341 -0
  63. package/src/registry/PluginScopeImpl.ts +259 -0
  64. package/src/registry/PluginSystem.ts +161 -0
  65. package/src/registry/Registry.ts +133 -8
  66. package/src/registry/__tests__/PluginSystem.test.ts +226 -0
  67. package/src/registry/__tests__/Registry.test.ts +293 -0
  68. package/src/registry/__tests__/plugin-scope-integration.test.ts +283 -0
  69. package/src/types/index.ts +8 -0
  70. package/src/utils/__tests__/filter-converter.test.ts +118 -0
  71. package/src/utils/filter-converter.ts +133 -0
  72. package/src/validation/__tests__/object-validation-engine.test.ts +567 -0
  73. package/src/validation/__tests__/validation-engine.test.ts +102 -0
  74. package/src/validation/index.ts +10 -0
  75. package/src/validation/schema-validator.ts +8 -0
  76. package/src/validation/validation-engine.ts +461 -0
  77. package/src/validation/validators/index.ts +25 -0
  78. package/src/validation/validators/object-validation-engine.ts +722 -0
  79. package/tsconfig.tsbuildinfo +1 -1
  80. package/vitest.config.ts +2 -0
  81. package/src/builder/schema-builder.d.ts +0 -287
  82. package/src/builder/schema-builder.js +0 -505
  83. package/src/index.d.ts +0 -4
  84. package/src/index.js +0 -7
  85. package/src/registry/Registry.d.ts +0 -49
  86. package/src/registry/Registry.js +0 -36
  87. package/src/types/index.d.ts +0 -12
  88. package/src/types/index.js +0 -1
  89. package/src/validation/schema-validator.d.ts +0 -87
  90. package/src/validation/schema-validator.js +0 -280
@@ -0,0 +1,461 @@
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 - Validation Engine
11
+ *
12
+ * Phase 3.5: Complete validation engine implementation with support for:
13
+ * - Sync and async validation
14
+ * - Cross-field validation
15
+ * - Custom validation functions
16
+ * - Improved error messages
17
+ *
18
+ * @module validation-engine
19
+ * @packageDocumentation
20
+ */
21
+
22
+ import type {
23
+ AdvancedValidationSchema,
24
+ AdvancedValidationRule,
25
+ ValidationRuleType,
26
+ ValidationFunction,
27
+ AsyncValidationFunction,
28
+ ValidationContext,
29
+ AdvancedValidationResult,
30
+ AdvancedValidationError,
31
+ } from '@object-ui/types';
32
+
33
+ /**
34
+ * Validation Engine - Executes validation rules
35
+ */
36
+ export class ValidationEngine {
37
+ /**
38
+ * Validate a value against validation schema
39
+ */
40
+ async validate(
41
+ value: any,
42
+ schema: AdvancedValidationSchema,
43
+ context?: ValidationContext
44
+ ): Promise<AdvancedValidationResult> {
45
+ const errors: AdvancedValidationError[] = [];
46
+ const warnings: AdvancedValidationError[] = [];
47
+
48
+ for (const rule of schema.rules) {
49
+ const result = await this.validateRule(value, rule, context);
50
+
51
+ if (result) {
52
+ const error: AdvancedValidationError = {
53
+ field: schema.field || 'unknown',
54
+ message: result,
55
+ code: rule.type,
56
+ rule: rule.type,
57
+ severity: rule.severity || 'error',
58
+ };
59
+
60
+ if (rule.severity === 'warning' || rule.severity === 'info') {
61
+ warnings.push(error);
62
+ } else {
63
+ errors.push(error);
64
+ }
65
+ }
66
+ }
67
+
68
+ return {
69
+ valid: errors.length === 0,
70
+ errors,
71
+ warnings,
72
+ };
73
+ }
74
+
75
+ /**
76
+ * Validate a single rule
77
+ */
78
+ private async validateRule(
79
+ value: any,
80
+ rule: AdvancedValidationRule,
81
+ context?: ValidationContext
82
+ ): Promise<string | null> {
83
+ // Custom async validator
84
+ if (rule.async_validator) {
85
+ const result = await rule.async_validator(value, context);
86
+ if (result === false) {
87
+ return rule.message || 'Async validation failed';
88
+ }
89
+ if (typeof result === 'string') {
90
+ return result;
91
+ }
92
+ return null;
93
+ }
94
+
95
+ // Custom sync validator
96
+ if (rule.validator) {
97
+ const result = rule.validator(value, context);
98
+ if (result === false) {
99
+ return rule.message || 'Validation failed';
100
+ }
101
+ if (typeof result === 'string') {
102
+ return result;
103
+ }
104
+ return null;
105
+ }
106
+
107
+ // Built-in validators
108
+ return this.validateBuiltInRule(value, rule, context);
109
+ }
110
+
111
+ /**
112
+ * Validate built-in rules
113
+ */
114
+ private async validateBuiltInRule(
115
+ value: any,
116
+ rule: AdvancedValidationRule,
117
+ context?: ValidationContext
118
+ ): Promise<string | null> {
119
+ const { type, params, message } = rule;
120
+
121
+ switch (type) {
122
+ case 'required':
123
+ if (value === null || value === undefined || value === '') {
124
+ return message || 'This field is required';
125
+ }
126
+ break;
127
+
128
+ case 'min_length':
129
+ if (typeof value === 'string' && value.length < params) {
130
+ return message || `Minimum length is ${params} characters`;
131
+ }
132
+ break;
133
+
134
+ case 'max_length':
135
+ if (typeof value === 'string' && value.length > params) {
136
+ return message || `Maximum length is ${params} characters`;
137
+ }
138
+ break;
139
+
140
+ case 'pattern':
141
+ if (typeof value === 'string') {
142
+ try {
143
+ const regex = typeof params === 'string' ? new RegExp(params) : params;
144
+ if (!regex.test(value)) {
145
+ return message || 'Invalid format';
146
+ }
147
+ } catch (error) {
148
+ return message || 'Invalid pattern configuration';
149
+ }
150
+ }
151
+ break;
152
+
153
+ case 'email':
154
+ if (typeof value === 'string') {
155
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
156
+ if (!emailRegex.test(value)) {
157
+ return message || 'Invalid email address';
158
+ }
159
+ }
160
+ break;
161
+
162
+ case 'url':
163
+ if (typeof value === 'string') {
164
+ try {
165
+ new URL(value);
166
+ } catch {
167
+ return message || 'Invalid URL';
168
+ }
169
+ }
170
+ break;
171
+
172
+ case 'phone':
173
+ if (typeof value === 'string') {
174
+ const phoneRegex = /^[\d\s\-+()]+$/;
175
+ if (!phoneRegex.test(value)) {
176
+ return message || 'Invalid phone number';
177
+ }
178
+ }
179
+ break;
180
+
181
+ case 'min':
182
+ if (typeof value === 'number' && value < params) {
183
+ return message || `Minimum value is ${params}`;
184
+ }
185
+ break;
186
+
187
+ case 'max':
188
+ if (typeof value === 'number' && value > params) {
189
+ return message || `Maximum value is ${params}`;
190
+ }
191
+ break;
192
+
193
+ case 'integer':
194
+ if (typeof value === 'number' && !Number.isInteger(value)) {
195
+ return message || 'Value must be an integer';
196
+ }
197
+ break;
198
+
199
+ case 'positive':
200
+ if (typeof value === 'number' && value <= 0) {
201
+ return message || 'Value must be positive';
202
+ }
203
+ break;
204
+
205
+ case 'negative':
206
+ if (typeof value === 'number' && value >= 0) {
207
+ return message || 'Value must be negative';
208
+ }
209
+ break;
210
+
211
+ case 'date_min':
212
+ if (value instanceof Date || typeof value === 'string') {
213
+ const date = value instanceof Date ? value : new Date(value);
214
+ const minDate = params instanceof Date ? params : new Date(params);
215
+ if (date < minDate) {
216
+ return message || `Date must be after ${minDate.toLocaleDateString()}`;
217
+ }
218
+ }
219
+ break;
220
+
221
+ case 'date_max':
222
+ if (value instanceof Date || typeof value === 'string') {
223
+ const date = value instanceof Date ? value : new Date(value);
224
+ const maxDate = params instanceof Date ? params : new Date(params);
225
+ if (date > maxDate) {
226
+ return message || `Date must be before ${maxDate.toLocaleDateString()}`;
227
+ }
228
+ }
229
+ break;
230
+
231
+ case 'date_future':
232
+ if (value instanceof Date || typeof value === 'string') {
233
+ const date = value instanceof Date ? value : new Date(value);
234
+ if (date <= new Date()) {
235
+ return message || 'Date must be in the future';
236
+ }
237
+ }
238
+ break;
239
+
240
+ case 'date_past':
241
+ if (value instanceof Date || typeof value === 'string') {
242
+ const date = value instanceof Date ? value : new Date(value);
243
+ if (date >= new Date()) {
244
+ return message || 'Date must be in the past';
245
+ }
246
+ }
247
+ break;
248
+
249
+ case 'min_items':
250
+ if (Array.isArray(value) && value.length < params) {
251
+ return message || `Minimum ${params} items required`;
252
+ }
253
+ break;
254
+
255
+ case 'max_items':
256
+ if (Array.isArray(value) && value.length > params) {
257
+ return message || `Maximum ${params} items allowed`;
258
+ }
259
+ break;
260
+
261
+ case 'unique_items':
262
+ if (Array.isArray(value)) {
263
+ const unique = new Set(value);
264
+ if (unique.size !== value.length) {
265
+ return message || 'All items must be unique';
266
+ }
267
+ }
268
+ break;
269
+
270
+ case 'field_match':
271
+ if (context?.values && params) {
272
+ const otherValue = context.values[params];
273
+ if (value !== otherValue) {
274
+ return message || `Value must match ${params}`;
275
+ }
276
+ }
277
+ break;
278
+
279
+ case 'field_compare':
280
+ if (context?.values && params) {
281
+ const { field: otherField, operator } = params;
282
+ const otherValue = context.values[otherField];
283
+
284
+ switch (operator) {
285
+ case '>':
286
+ if (value <= otherValue) {
287
+ return message || `Value must be greater than ${otherField}`;
288
+ }
289
+ break;
290
+ case '<':
291
+ if (value >= otherValue) {
292
+ return message || `Value must be less than ${otherField}`;
293
+ }
294
+ break;
295
+ case '>=':
296
+ if (value < otherValue) {
297
+ return message || `Value must be greater than or equal to ${otherField}`;
298
+ }
299
+ break;
300
+ case '<=':
301
+ if (value > otherValue) {
302
+ return message || `Value must be less than or equal to ${otherField}`;
303
+ }
304
+ break;
305
+ }
306
+ }
307
+ break;
308
+
309
+ case 'conditional':
310
+ if (context?.values && params) {
311
+ const { condition, rules } = params;
312
+
313
+ if (!Array.isArray(rules) || rules.length === 0) {
314
+ break;
315
+ }
316
+
317
+ const conditionMet = this.evaluateCondition(condition, context.values);
318
+
319
+ if (conditionMet) {
320
+ for (const conditionalRule of rules) {
321
+ const result = await this.validateRule(value, conditionalRule, context);
322
+ if (result) {
323
+ return result;
324
+ }
325
+ }
326
+ }
327
+ }
328
+ break;
329
+
330
+ default:
331
+ // Unhandled validation rule type
332
+ console.warn(`Unsupported validation rule type: ${type}`);
333
+ return null;
334
+ }
335
+
336
+ return null;
337
+ }
338
+
339
+ /**
340
+ * Evaluate a condition
341
+ * Note: Conditions must be declarative objects, not functions, for security.
342
+ */
343
+ private evaluateCondition(condition: any, values: Record<string, any>): boolean {
344
+ if (typeof condition === 'function') {
345
+ console.warn('Function-based conditions are deprecated and will be removed. Use declarative conditions instead.');
346
+ return false; // Security: reject function-based conditions
347
+ }
348
+
349
+ if (typeof condition === 'object' && condition.field) {
350
+ const fieldValue = values[condition.field];
351
+ const { operator = '=', value } = condition;
352
+
353
+ switch (operator) {
354
+ case '=':
355
+ return fieldValue === value;
356
+ case '!=':
357
+ return fieldValue !== value;
358
+ case '>':
359
+ return fieldValue > value;
360
+ case '<':
361
+ return fieldValue < value;
362
+ case '>=':
363
+ return fieldValue >= value;
364
+ case '<=':
365
+ return fieldValue <= value;
366
+ case 'in':
367
+ return Array.isArray(value) && value.includes(fieldValue);
368
+ default:
369
+ return false;
370
+ }
371
+ }
372
+
373
+ return false;
374
+ }
375
+
376
+ /**
377
+ * Validate multiple fields
378
+ */
379
+ async validateFields(
380
+ values: Record<string, any>,
381
+ schemas: Record<string, AdvancedValidationSchema>
382
+ ): Promise<Record<string, AdvancedValidationResult>> {
383
+ const results: Record<string, AdvancedValidationResult> = {};
384
+
385
+ const context: ValidationContext = {
386
+ values,
387
+ };
388
+
389
+ for (const [field, schema] of Object.entries(schemas)) {
390
+ const value = values[field];
391
+ const schemaWithField: AdvancedValidationSchema = {
392
+ ...schema,
393
+ field: schema.field ?? field,
394
+ };
395
+ results[field] = await this.validate(value, schemaWithField, context);
396
+ }
397
+
398
+ return results;
399
+ }
400
+
401
+ /**
402
+ * Check if all fields are valid
403
+ */
404
+ isValid(results: Record<string, AdvancedValidationResult>): boolean {
405
+ return Object.values(results).every(result => result.valid);
406
+ }
407
+
408
+ /**
409
+ * Get all errors from validation results
410
+ */
411
+ getAllErrors(results: Record<string, AdvancedValidationResult>): AdvancedValidationError[] {
412
+ const errors: AdvancedValidationError[] = [];
413
+
414
+ for (const result of Object.values(results)) {
415
+ errors.push(...result.errors);
416
+ }
417
+
418
+ return errors;
419
+ }
420
+
421
+ /**
422
+ * Get all warnings from validation results
423
+ */
424
+ getAllWarnings(results: Record<string, AdvancedValidationResult>): AdvancedValidationError[] {
425
+ const warnings: AdvancedValidationError[] = [];
426
+
427
+ for (const result of Object.values(results)) {
428
+ if (result.warnings) {
429
+ warnings.push(...result.warnings);
430
+ }
431
+ }
432
+
433
+ return warnings;
434
+ }
435
+ }
436
+
437
+ /**
438
+ * Default validation engine instance
439
+ */
440
+ export const defaultValidationEngine = new ValidationEngine();
441
+
442
+ /**
443
+ * Convenience function to validate a value
444
+ */
445
+ export async function validate(
446
+ value: any,
447
+ schema: AdvancedValidationSchema,
448
+ context?: ValidationContext
449
+ ): Promise<AdvancedValidationResult> {
450
+ return defaultValidationEngine.validate(value, schema, context);
451
+ }
452
+
453
+ /**
454
+ * Convenience function to validate multiple fields
455
+ */
456
+ export async function validateFields(
457
+ values: Record<string, any>,
458
+ schemas: Record<string, AdvancedValidationSchema>
459
+ ): Promise<Record<string, AdvancedValidationResult>> {
460
+ return defaultValidationEngine.validateFields(values, schemas);
461
+ }
@@ -0,0 +1,25 @@
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 - Validators
11
+ *
12
+ * ObjectStack Spec v0.7.1 compliant validators
13
+ *
14
+ * @module validators
15
+ * @packageDocumentation
16
+ */
17
+
18
+ export {
19
+ ObjectValidationEngine,
20
+ defaultObjectValidationEngine,
21
+ validateRecord,
22
+ type ObjectValidationContext,
23
+ type ObjectValidationResult,
24
+ type ValidationExpressionEvaluator,
25
+ } from './object-validation-engine.js';