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