@object-ui/core 0.3.1 → 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 (68) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/dist/actions/index.d.ts +1 -1
  3. package/dist/actions/index.js +1 -1
  4. package/dist/evaluator/ExpressionCache.d.ts +101 -0
  5. package/dist/evaluator/ExpressionCache.js +135 -0
  6. package/dist/evaluator/ExpressionEvaluator.d.ts +20 -2
  7. package/dist/evaluator/ExpressionEvaluator.js +34 -14
  8. package/dist/evaluator/index.d.ts +3 -2
  9. package/dist/evaluator/index.js +3 -2
  10. package/dist/index.d.ts +10 -7
  11. package/dist/index.js +9 -7
  12. package/dist/query/index.d.ts +6 -0
  13. package/dist/query/index.js +6 -0
  14. package/dist/query/query-ast.d.ts +32 -0
  15. package/dist/query/query-ast.js +268 -0
  16. package/dist/registry/PluginScopeImpl.d.ts +80 -0
  17. package/dist/registry/PluginScopeImpl.js +243 -0
  18. package/dist/registry/PluginSystem.d.ts +66 -0
  19. package/dist/registry/PluginSystem.js +142 -0
  20. package/dist/registry/Registry.d.ts +73 -4
  21. package/dist/registry/Registry.js +112 -7
  22. package/dist/validation/index.d.ts +9 -0
  23. package/dist/validation/index.js +9 -0
  24. package/dist/validation/validation-engine.d.ts +70 -0
  25. package/dist/validation/validation-engine.js +363 -0
  26. package/dist/validation/validators/index.d.ts +16 -0
  27. package/dist/validation/validators/index.js +16 -0
  28. package/dist/validation/validators/object-validation-engine.d.ts +118 -0
  29. package/dist/validation/validators/object-validation-engine.js +538 -0
  30. package/package.json +13 -5
  31. package/src/actions/index.ts +1 -1
  32. package/src/evaluator/ExpressionCache.ts +192 -0
  33. package/src/evaluator/ExpressionEvaluator.ts +33 -14
  34. package/src/evaluator/__tests__/ExpressionCache.test.ts +135 -0
  35. package/src/evaluator/index.ts +3 -2
  36. package/src/index.ts +10 -7
  37. package/src/query/__tests__/query-ast.test.ts +211 -0
  38. package/src/query/__tests__/window-functions.test.ts +275 -0
  39. package/src/query/index.ts +7 -0
  40. package/src/query/query-ast.ts +341 -0
  41. package/src/registry/PluginScopeImpl.ts +259 -0
  42. package/src/registry/PluginSystem.ts +161 -0
  43. package/src/registry/Registry.ts +125 -8
  44. package/src/registry/__tests__/PluginSystem.test.ts +226 -0
  45. package/src/registry/__tests__/Registry.test.ts +293 -0
  46. package/src/registry/__tests__/plugin-scope-integration.test.ts +283 -0
  47. package/src/validation/__tests__/object-validation-engine.test.ts +567 -0
  48. package/src/validation/__tests__/validation-engine.test.ts +102 -0
  49. package/src/validation/index.ts +10 -0
  50. package/src/validation/validation-engine.ts +461 -0
  51. package/src/validation/validators/index.ts +25 -0
  52. package/src/validation/validators/object-validation-engine.ts +722 -0
  53. package/tsconfig.tsbuildinfo +1 -1
  54. package/vitest.config.ts +2 -0
  55. package/src/adapters/index.d.ts +0 -8
  56. package/src/adapters/index.js +0 -10
  57. package/src/builder/schema-builder.d.ts +0 -294
  58. package/src/builder/schema-builder.js +0 -503
  59. package/src/index.d.ts +0 -13
  60. package/src/index.js +0 -16
  61. package/src/registry/Registry.d.ts +0 -56
  62. package/src/registry/Registry.js +0 -43
  63. package/src/types/index.d.ts +0 -19
  64. package/src/types/index.js +0 -8
  65. package/src/utils/filter-converter.d.ts +0 -57
  66. package/src/utils/filter-converter.js +0 -100
  67. package/src/validation/schema-validator.d.ts +0 -94
  68. package/src/validation/schema-validator.js +0 -278
@@ -0,0 +1,363 @@
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
+ * Validation Engine - Executes validation rules
10
+ */
11
+ export class ValidationEngine {
12
+ /**
13
+ * Validate a value against validation schema
14
+ */
15
+ async validate(value, schema, context) {
16
+ const errors = [];
17
+ const warnings = [];
18
+ for (const rule of schema.rules) {
19
+ const result = await this.validateRule(value, rule, context);
20
+ if (result) {
21
+ const error = {
22
+ field: schema.field || 'unknown',
23
+ message: result,
24
+ code: rule.type,
25
+ rule: rule.type,
26
+ severity: rule.severity || 'error',
27
+ };
28
+ if (rule.severity === 'warning' || rule.severity === 'info') {
29
+ warnings.push(error);
30
+ }
31
+ else {
32
+ errors.push(error);
33
+ }
34
+ }
35
+ }
36
+ return {
37
+ valid: errors.length === 0,
38
+ errors,
39
+ warnings,
40
+ };
41
+ }
42
+ /**
43
+ * Validate a single rule
44
+ */
45
+ async validateRule(value, rule, context) {
46
+ // Custom async validator
47
+ if (rule.async_validator) {
48
+ const result = await rule.async_validator(value, context);
49
+ if (result === false) {
50
+ return rule.message || 'Async validation failed';
51
+ }
52
+ if (typeof result === 'string') {
53
+ return result;
54
+ }
55
+ return null;
56
+ }
57
+ // Custom sync validator
58
+ if (rule.validator) {
59
+ const result = rule.validator(value, context);
60
+ if (result === false) {
61
+ return rule.message || 'Validation failed';
62
+ }
63
+ if (typeof result === 'string') {
64
+ return result;
65
+ }
66
+ return null;
67
+ }
68
+ // Built-in validators
69
+ return this.validateBuiltInRule(value, rule, context);
70
+ }
71
+ /**
72
+ * Validate built-in rules
73
+ */
74
+ async validateBuiltInRule(value, rule, context) {
75
+ const { type, params, message } = rule;
76
+ switch (type) {
77
+ case 'required':
78
+ if (value === null || value === undefined || value === '') {
79
+ return message || 'This field is required';
80
+ }
81
+ break;
82
+ case 'min_length':
83
+ if (typeof value === 'string' && value.length < params) {
84
+ return message || `Minimum length is ${params} characters`;
85
+ }
86
+ break;
87
+ case 'max_length':
88
+ if (typeof value === 'string' && value.length > params) {
89
+ return message || `Maximum length is ${params} characters`;
90
+ }
91
+ break;
92
+ case 'pattern':
93
+ if (typeof value === 'string') {
94
+ try {
95
+ const regex = typeof params === 'string' ? new RegExp(params) : params;
96
+ if (!regex.test(value)) {
97
+ return message || 'Invalid format';
98
+ }
99
+ }
100
+ catch (error) {
101
+ return message || 'Invalid pattern configuration';
102
+ }
103
+ }
104
+ break;
105
+ case 'email':
106
+ if (typeof value === 'string') {
107
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
108
+ if (!emailRegex.test(value)) {
109
+ return message || 'Invalid email address';
110
+ }
111
+ }
112
+ break;
113
+ case 'url':
114
+ if (typeof value === 'string') {
115
+ try {
116
+ new URL(value);
117
+ }
118
+ catch {
119
+ return message || 'Invalid URL';
120
+ }
121
+ }
122
+ break;
123
+ case 'phone':
124
+ if (typeof value === 'string') {
125
+ const phoneRegex = /^[\d\s\-+()]+$/;
126
+ if (!phoneRegex.test(value)) {
127
+ return message || 'Invalid phone number';
128
+ }
129
+ }
130
+ break;
131
+ case 'min':
132
+ if (typeof value === 'number' && value < params) {
133
+ return message || `Minimum value is ${params}`;
134
+ }
135
+ break;
136
+ case 'max':
137
+ if (typeof value === 'number' && value > params) {
138
+ return message || `Maximum value is ${params}`;
139
+ }
140
+ break;
141
+ case 'integer':
142
+ if (typeof value === 'number' && !Number.isInteger(value)) {
143
+ return message || 'Value must be an integer';
144
+ }
145
+ break;
146
+ case 'positive':
147
+ if (typeof value === 'number' && value <= 0) {
148
+ return message || 'Value must be positive';
149
+ }
150
+ break;
151
+ case 'negative':
152
+ if (typeof value === 'number' && value >= 0) {
153
+ return message || 'Value must be negative';
154
+ }
155
+ break;
156
+ case 'date_min':
157
+ if (value instanceof Date || typeof value === 'string') {
158
+ const date = value instanceof Date ? value : new Date(value);
159
+ const minDate = params instanceof Date ? params : new Date(params);
160
+ if (date < minDate) {
161
+ return message || `Date must be after ${minDate.toLocaleDateString()}`;
162
+ }
163
+ }
164
+ break;
165
+ case 'date_max':
166
+ if (value instanceof Date || typeof value === 'string') {
167
+ const date = value instanceof Date ? value : new Date(value);
168
+ const maxDate = params instanceof Date ? params : new Date(params);
169
+ if (date > maxDate) {
170
+ return message || `Date must be before ${maxDate.toLocaleDateString()}`;
171
+ }
172
+ }
173
+ break;
174
+ case 'date_future':
175
+ if (value instanceof Date || typeof value === 'string') {
176
+ const date = value instanceof Date ? value : new Date(value);
177
+ if (date <= new Date()) {
178
+ return message || 'Date must be in the future';
179
+ }
180
+ }
181
+ break;
182
+ case 'date_past':
183
+ if (value instanceof Date || typeof value === 'string') {
184
+ const date = value instanceof Date ? value : new Date(value);
185
+ if (date >= new Date()) {
186
+ return message || 'Date must be in the past';
187
+ }
188
+ }
189
+ break;
190
+ case 'min_items':
191
+ if (Array.isArray(value) && value.length < params) {
192
+ return message || `Minimum ${params} items required`;
193
+ }
194
+ break;
195
+ case 'max_items':
196
+ if (Array.isArray(value) && value.length > params) {
197
+ return message || `Maximum ${params} items allowed`;
198
+ }
199
+ break;
200
+ case 'unique_items':
201
+ if (Array.isArray(value)) {
202
+ const unique = new Set(value);
203
+ if (unique.size !== value.length) {
204
+ return message || 'All items must be unique';
205
+ }
206
+ }
207
+ break;
208
+ case 'field_match':
209
+ if (context?.values && params) {
210
+ const otherValue = context.values[params];
211
+ if (value !== otherValue) {
212
+ return message || `Value must match ${params}`;
213
+ }
214
+ }
215
+ break;
216
+ case 'field_compare':
217
+ if (context?.values && params) {
218
+ const { field: otherField, operator } = params;
219
+ const otherValue = context.values[otherField];
220
+ switch (operator) {
221
+ case '>':
222
+ if (value <= otherValue) {
223
+ return message || `Value must be greater than ${otherField}`;
224
+ }
225
+ break;
226
+ case '<':
227
+ if (value >= otherValue) {
228
+ return message || `Value must be less than ${otherField}`;
229
+ }
230
+ break;
231
+ case '>=':
232
+ if (value < otherValue) {
233
+ return message || `Value must be greater than or equal to ${otherField}`;
234
+ }
235
+ break;
236
+ case '<=':
237
+ if (value > otherValue) {
238
+ return message || `Value must be less than or equal to ${otherField}`;
239
+ }
240
+ break;
241
+ }
242
+ }
243
+ break;
244
+ case 'conditional':
245
+ if (context?.values && params) {
246
+ const { condition, rules } = params;
247
+ if (!Array.isArray(rules) || rules.length === 0) {
248
+ break;
249
+ }
250
+ const conditionMet = this.evaluateCondition(condition, context.values);
251
+ if (conditionMet) {
252
+ for (const conditionalRule of rules) {
253
+ const result = await this.validateRule(value, conditionalRule, context);
254
+ if (result) {
255
+ return result;
256
+ }
257
+ }
258
+ }
259
+ }
260
+ break;
261
+ default:
262
+ // Unhandled validation rule type
263
+ console.warn(`Unsupported validation rule type: ${type}`);
264
+ return null;
265
+ }
266
+ return null;
267
+ }
268
+ /**
269
+ * Evaluate a condition
270
+ * Note: Conditions must be declarative objects, not functions, for security.
271
+ */
272
+ evaluateCondition(condition, values) {
273
+ if (typeof condition === 'function') {
274
+ console.warn('Function-based conditions are deprecated and will be removed. Use declarative conditions instead.');
275
+ return false; // Security: reject function-based conditions
276
+ }
277
+ if (typeof condition === 'object' && condition.field) {
278
+ const fieldValue = values[condition.field];
279
+ const { operator = '=', value } = condition;
280
+ switch (operator) {
281
+ case '=':
282
+ return fieldValue === value;
283
+ case '!=':
284
+ return fieldValue !== value;
285
+ case '>':
286
+ return fieldValue > value;
287
+ case '<':
288
+ return fieldValue < value;
289
+ case '>=':
290
+ return fieldValue >= value;
291
+ case '<=':
292
+ return fieldValue <= value;
293
+ case 'in':
294
+ return Array.isArray(value) && value.includes(fieldValue);
295
+ default:
296
+ return false;
297
+ }
298
+ }
299
+ return false;
300
+ }
301
+ /**
302
+ * Validate multiple fields
303
+ */
304
+ async validateFields(values, schemas) {
305
+ const results = {};
306
+ const context = {
307
+ values,
308
+ };
309
+ for (const [field, schema] of Object.entries(schemas)) {
310
+ const value = values[field];
311
+ const schemaWithField = {
312
+ ...schema,
313
+ field: schema.field ?? field,
314
+ };
315
+ results[field] = await this.validate(value, schemaWithField, context);
316
+ }
317
+ return results;
318
+ }
319
+ /**
320
+ * Check if all fields are valid
321
+ */
322
+ isValid(results) {
323
+ return Object.values(results).every(result => result.valid);
324
+ }
325
+ /**
326
+ * Get all errors from validation results
327
+ */
328
+ getAllErrors(results) {
329
+ const errors = [];
330
+ for (const result of Object.values(results)) {
331
+ errors.push(...result.errors);
332
+ }
333
+ return errors;
334
+ }
335
+ /**
336
+ * Get all warnings from validation results
337
+ */
338
+ getAllWarnings(results) {
339
+ const warnings = [];
340
+ for (const result of Object.values(results)) {
341
+ if (result.warnings) {
342
+ warnings.push(...result.warnings);
343
+ }
344
+ }
345
+ return warnings;
346
+ }
347
+ }
348
+ /**
349
+ * Default validation engine instance
350
+ */
351
+ export const defaultValidationEngine = new ValidationEngine();
352
+ /**
353
+ * Convenience function to validate a value
354
+ */
355
+ export async function validate(value, schema, context) {
356
+ return defaultValidationEngine.validate(value, schema, context);
357
+ }
358
+ /**
359
+ * Convenience function to validate multiple fields
360
+ */
361
+ export async function validateFields(values, schemas) {
362
+ return defaultValidationEngine.validateFields(values, schemas);
363
+ }
@@ -0,0 +1,16 @@
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
+ * @object-ui/core - Validators
10
+ *
11
+ * ObjectStack Spec v0.7.1 compliant validators
12
+ *
13
+ * @module validators
14
+ * @packageDocumentation
15
+ */
16
+ export { ObjectValidationEngine, defaultObjectValidationEngine, validateRecord, type ObjectValidationContext, type ObjectValidationResult, type ValidationExpressionEvaluator, } from './object-validation-engine.js';
@@ -0,0 +1,16 @@
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
+ * @object-ui/core - Validators
10
+ *
11
+ * ObjectStack Spec v0.7.1 compliant validators
12
+ *
13
+ * @module validators
14
+ * @packageDocumentation
15
+ */
16
+ export { ObjectValidationEngine, defaultObjectValidationEngine, validateRecord, } from './object-validation-engine.js';
@@ -0,0 +1,118 @@
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
+ * @object-ui/core - Object-Level Validation Engine
10
+ *
11
+ * ObjectStack Spec v0.7.1 compliant validation engine for object-level validation rules.
12
+ * Supports all 9 validation types from the specification:
13
+ * - ScriptValidation
14
+ * - UniquenessValidation
15
+ * - StateMachineValidation
16
+ * - CrossFieldValidation
17
+ * - AsyncValidation
18
+ * - ConditionalValidation
19
+ * - FormatValidation
20
+ * - RangeValidation
21
+ *
22
+ * @module object-validation-engine
23
+ * @packageDocumentation
24
+ */
25
+ import type { ObjectValidationRule } from '@object-ui/types';
26
+ /**
27
+ * Validation context for object-level validations
28
+ */
29
+ export interface ObjectValidationContext {
30
+ /** Current record data */
31
+ record: Record<string, any>;
32
+ /** Previous record data (for updates) */
33
+ oldRecord?: Record<string, any>;
34
+ /** Current user */
35
+ user?: Record<string, any>;
36
+ /** Additional context data */
37
+ [key: string]: any;
38
+ }
39
+ /**
40
+ * Validation result
41
+ */
42
+ export interface ObjectValidationResult {
43
+ /** Whether validation passed */
44
+ valid: boolean;
45
+ /** Error message if validation failed */
46
+ message?: string;
47
+ /** Validation rule that failed */
48
+ rule?: string;
49
+ /** Severity */
50
+ severity?: 'error' | 'warning' | 'info';
51
+ }
52
+ /**
53
+ * Validation expression evaluator interface
54
+ */
55
+ export interface ValidationExpressionEvaluator {
56
+ evaluate(expression: string, context: Record<string, any>): any;
57
+ }
58
+ /**
59
+ * Object-Level Validation Engine
60
+ * Implements ObjectStack Spec v0.7.1 validation framework
61
+ */
62
+ export declare class ObjectValidationEngine {
63
+ private expressionEvaluator;
64
+ private uniquenessChecker?;
65
+ constructor(expressionEvaluator?: ValidationExpressionEvaluator, uniquenessChecker?: (fields: string[], values: Record<string, any>, scope?: string, context?: ObjectValidationContext) => Promise<boolean>);
66
+ /**
67
+ * Validate a record against a set of validation rules
68
+ */
69
+ validateRecord(rules: ObjectValidationRule[], context: ObjectValidationContext, event?: 'insert' | 'update' | 'delete'): Promise<ObjectValidationResult[]>;
70
+ /**
71
+ * Validate a single rule
72
+ */
73
+ private validateRule;
74
+ /**
75
+ * Validate script-based rule
76
+ */
77
+ private validateScript;
78
+ /**
79
+ * Validate uniqueness constraint
80
+ */
81
+ private validateUniqueness;
82
+ /**
83
+ * Validate state machine transitions
84
+ */
85
+ private validateStateMachine;
86
+ /**
87
+ * Validate cross-field constraints
88
+ */
89
+ private validateCrossField;
90
+ /**
91
+ * Validate async/remote validation
92
+ */
93
+ private validateAsync;
94
+ /**
95
+ * Validate conditional rules
96
+ */
97
+ private validateConditional;
98
+ /**
99
+ * Validate format/pattern
100
+ */
101
+ private validateFormat;
102
+ /**
103
+ * Validate range constraints
104
+ */
105
+ private validateRange;
106
+ /**
107
+ * Get predefined regex pattern
108
+ */
109
+ private getPredefinedPattern;
110
+ }
111
+ /**
112
+ * Default instance
113
+ */
114
+ export declare const defaultObjectValidationEngine: ObjectValidationEngine;
115
+ /**
116
+ * Convenience function to validate a record
117
+ */
118
+ export declare function validateRecord(rules: ObjectValidationRule[], context: ObjectValidationContext, event?: 'insert' | 'update' | 'delete'): Promise<ObjectValidationResult[]>;