@object-ui/core 17.5.0 → 17.7.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/CHANGELOG.md +7404 -0
- package/README.md +127 -26
- package/dist/actions/ActionEngine.d.ts +5 -3
- package/dist/actions/ActionEngine.js +14 -10
- package/dist/actions/ActionRunner.d.ts +363 -30
- package/dist/actions/ActionRunner.js +544 -58
- package/dist/actions/TransactionManager.d.ts +7 -2
- package/dist/actions/actionKeys.d.ts +75 -13
- package/dist/actions/actionKeys.js +131 -16
- package/dist/actions/actionResponse.js +3 -3
- package/dist/actions/index.d.ts +2 -0
- package/dist/actions/index.js +2 -0
- package/dist/actions/recordDelete.d.ts +118 -0
- package/dist/actions/recordDelete.js +112 -0
- package/dist/actions/recordIdParam.d.ts +77 -0
- package/dist/actions/recordIdParam.js +37 -0
- package/dist/actions/serverActionHandler.d.ts +1 -1
- package/dist/actions/serverActionHandler.js +1 -1
- package/dist/adapters/ValueDataSource.d.ts +6 -1
- package/dist/adapters/ValueDataSource.js +1090 -98
- package/dist/adapters/batchTransaction.d.ts +1 -1
- package/dist/adapters/batchTransaction.js +1 -1
- package/dist/builder/schema-builder.d.ts +38 -81
- package/dist/builder/schema-builder.js +33 -152
- package/dist/data-scope/ViewDataProvider.d.ts +1 -1
- package/dist/data-scope/ViewDataProvider.js +21 -1
- package/dist/data-scope/element-data-source.d.ts +261 -7
- package/dist/data-scope/element-data-source.js +342 -52
- package/dist/data-scope/index.d.ts +9 -4
- package/dist/data-scope/index.js +9 -4
- package/dist/evaluator/ExpressionEvaluator.d.ts +128 -1
- package/dist/evaluator/ExpressionEvaluator.js +226 -13
- package/dist/evaluator/FormulaFunctions.d.ts +0 -10
- package/dist/evaluator/FormulaFunctions.js +166 -27
- package/dist/evaluator/SafeExpressionParser.js +0 -4
- package/dist/evaluator/declaredPredicate.d.ts +93 -9
- package/dist/evaluator/declaredPredicate.js +118 -17
- package/dist/evaluator/fieldRules.d.ts +107 -4
- package/dist/evaluator/fieldRules.js +231 -35
- package/dist/evaluator/index.d.ts +2 -0
- package/dist/evaluator/index.js +2 -0
- package/dist/evaluator/listConditional.d.ts +125 -9
- package/dist/evaluator/listConditional.js +167 -26
- package/dist/evaluator/optionRules.d.ts +126 -0
- package/dist/evaluator/optionRules.js +141 -4
- package/dist/evaluator/predicateInput.d.ts +15 -4
- package/dist/evaluator/predicateInput.js +35 -2
- package/dist/evaluator/rowPredicateCanon.d.ts +56 -0
- package/dist/evaluator/rowPredicateCanon.js +158 -0
- package/dist/evaluator/subjectPermissions.d.ts +76 -0
- package/dist/evaluator/subjectPermissions.js +44 -0
- package/dist/evaluator/unevaluablePredicate.d.ts +57 -0
- package/dist/evaluator/unevaluablePredicate.js +70 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +97 -2
- package/dist/protocols/index.d.ts +0 -1
- package/dist/protocols/index.js +9 -1
- package/dist/registry/Registry.d.ts +397 -48
- package/dist/registry/Registry.js +263 -11
- package/dist/registry/WidgetRegistry.js +7 -3
- package/dist/registry/html-tier-intrinsics.d.ts +90 -0
- package/dist/registry/html-tier-intrinsics.js +110 -0
- package/dist/registry/public-blocks.d.ts +8 -0
- package/dist/registry/public-blocks.js +57 -0
- package/dist/theme/ThemeEngine.js +1 -1
- package/dist/theme/index.d.ts +1 -1
- package/dist/theme/index.js +1 -1
- package/dist/types/index.d.ts +92 -13
- package/dist/utils/chart-category-key.d.ts +82 -0
- package/dist/utils/chart-category-key.js +84 -0
- package/dist/utils/chart-measure-key.d.ts +64 -0
- package/dist/utils/chart-measure-key.js +65 -0
- package/dist/utils/chart-presentation.d.ts +188 -0
- package/dist/utils/chart-presentation.js +345 -0
- package/dist/utils/chart-series.d.ts +271 -31
- package/dist/utils/chart-series.js +478 -55
- package/dist/utils/column-identity.d.ts +43 -0
- package/dist/utils/column-identity.js +47 -1
- package/dist/utils/column-sortability.d.ts +157 -0
- package/dist/utils/column-sortability.js +217 -0
- package/dist/utils/dashboard-filters.d.ts +38 -6
- package/dist/utils/dashboard-filters.js +107 -61
- package/dist/utils/dataset-format.d.ts +211 -30
- package/dist/utils/dataset-format.js +407 -52
- package/dist/utils/date-display.d.ts +354 -0
- package/dist/utils/date-display.js +463 -0
- package/dist/utils/date-macros.js +7 -0
- package/dist/utils/debug.js +7 -0
- package/dist/utils/drill-down.d.ts +78 -0
- package/dist/utils/drill-down.js +93 -0
- package/dist/utils/emptiness.d.ts +96 -0
- package/dist/utils/emptiness.js +101 -0
- package/dist/utils/expand-fields.d.ts +171 -11
- package/dist/utils/expand-fields.js +171 -11
- package/dist/utils/extract-records.d.ts +65 -5
- package/dist/utils/extract-records.js +65 -8
- package/dist/utils/filter-converter.d.ts +152 -4
- package/dist/utils/filter-converter.js +1092 -22
- package/dist/utils/filter-tokens.d.ts +49 -5
- package/dist/utils/filter-tokens.js +105 -34
- package/dist/utils/freeze-schema.d.ts +38 -1
- package/dist/utils/freeze-schema.js +10 -0
- package/dist/utils/grouping-fields.d.ts +61 -0
- package/dist/utils/grouping-fields.js +84 -0
- package/dist/utils/humanize-label.d.ts +60 -0
- package/dist/utils/humanize-label.js +62 -0
- package/dist/utils/managedBy.d.ts +30 -5
- package/dist/utils/managedBy.js +5 -5
- package/dist/utils/native-date-value.d.ts +48 -0
- package/dist/utils/native-date-value.js +167 -0
- package/dist/utils/non-grid-row-ceiling.d.ts +143 -0
- package/dist/utils/non-grid-row-ceiling.js +141 -0
- package/dist/utils/normalize-list-view.d.ts +131 -1
- package/dist/utils/normalize-list-view.js +248 -6
- package/dist/utils/number-display.d.ts +70 -17
- package/dist/utils/number-display.js +34 -12
- package/dist/utils/object-aggregate-query.d.ts +113 -0
- package/dist/utils/object-aggregate-query.js +50 -0
- package/dist/utils/parent-scope.d.ts +123 -0
- package/dist/utils/parent-scope.js +140 -0
- package/dist/utils/predicate-fields.d.ts +62 -4
- package/dist/utils/predicate-fields.js +169 -4
- package/dist/utils/record-color.d.ts +75 -0
- package/dist/utils/record-color.js +65 -0
- package/dist/utils/record-source.d.ts +298 -0
- package/dist/utils/record-source.js +331 -0
- package/dist/utils/record-title.d.ts +148 -8
- package/dist/utils/record-title.js +220 -26
- package/dist/utils/reference-keys.d.ts +21 -22
- package/dist/utils/reference-keys.js +532 -29
- package/dist/utils/retired-field-types.d.ts +90 -0
- package/dist/utils/retired-field-types.js +124 -0
- package/dist/utils/sort-query.d.ts +128 -26
- package/dist/utils/sort-query.js +240 -13
- package/dist/utils/text-comparand.d.ts +92 -0
- package/dist/utils/text-comparand.js +99 -0
- package/dist/utils/unmaterialized-fields.d.ts +52 -0
- package/dist/utils/unmaterialized-fields.js +57 -0
- package/dist/utils/without-denied-fields.d.ts +49 -0
- package/dist/utils/without-denied-fields.js +52 -0
- package/dist/validation/index.d.ts +9 -4
- package/dist/validation/index.js +9 -4
- package/dist/validation/schema-validator.d.ts +1 -1
- package/dist/validation/schema-validator.js +305 -82
- package/dist/validation/server-owned-value.d.ts +62 -0
- package/dist/validation/server-owned-value.js +99 -0
- package/dist/validation/validators/object-validation-engine.d.ts +1 -1
- package/dist/validation/validators/object-validation-engine.js +14 -5
- package/package.json +8 -9
- package/dist/__benchmarks__/core.bench.d.ts +0 -8
- package/dist/__benchmarks__/core.bench.js +0 -53
- package/dist/data-scope/DataScopeManager.d.ts +0 -127
- package/dist/data-scope/DataScopeManager.js +0 -229
- package/dist/protocols/ResponsiveProtocol.d.ts +0 -73
- package/dist/protocols/ResponsiveProtocol.js +0 -158
- package/dist/validation/validation-engine.d.ts +0 -88
- package/dist/validation/validation-engine.js +0 -434
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ObjectUI
|
|
3
|
-
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
/** Breakpoint minimum pixel widths aligned with Tailwind CSS defaults. */
|
|
9
|
-
export const BREAKPOINT_VALUES = {
|
|
10
|
-
xs: 0,
|
|
11
|
-
sm: 640,
|
|
12
|
-
md: 768,
|
|
13
|
-
lg: 1024,
|
|
14
|
-
xl: 1280,
|
|
15
|
-
'2xl': 1536,
|
|
16
|
-
};
|
|
17
|
-
/** Ordered breakpoint keys from smallest to largest. */
|
|
18
|
-
const BREAKPOINT_ORDER = ['xs', 'sm', 'md', 'lg', 'xl', '2xl'];
|
|
19
|
-
// ============================================================================
|
|
20
|
-
// Config Resolution
|
|
21
|
-
// ============================================================================
|
|
22
|
-
/**
|
|
23
|
-
* Resolve a responsive configuration by applying defaults.
|
|
24
|
-
*
|
|
25
|
-
* @param config - SpecResponsiveConfig from the spec
|
|
26
|
-
* @returns Fully resolved responsive configuration
|
|
27
|
-
*/
|
|
28
|
-
export function resolveResponsiveConfig(config) {
|
|
29
|
-
return {
|
|
30
|
-
breakpoint: config.breakpoint,
|
|
31
|
-
hiddenOn: (config.hiddenOn ?? []),
|
|
32
|
-
columns: (config.columns ?? {}),
|
|
33
|
-
order: (config.order ?? {}),
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
// ============================================================================
|
|
37
|
-
// Visibility Classes
|
|
38
|
-
// ============================================================================
|
|
39
|
-
/**
|
|
40
|
-
* Generate Tailwind CSS classes for responsive visibility.
|
|
41
|
-
*
|
|
42
|
-
* If `breakpoint` is set, the element is hidden below that breakpoint
|
|
43
|
-
* (e.g. breakpoint "md" → `['hidden', 'md:block']`).
|
|
44
|
-
*
|
|
45
|
-
* If `hiddenOn` contains breakpoints, the element is hidden at those
|
|
46
|
-
* specific sizes (e.g. hiddenOn: ["sm", "lg"] → `['sm:hidden', 'md:block', 'lg:hidden', 'xl:block']`).
|
|
47
|
-
*
|
|
48
|
-
* @param config - SpecResponsiveConfig from the spec
|
|
49
|
-
* @returns Array of Tailwind CSS class strings
|
|
50
|
-
*/
|
|
51
|
-
export function getVisibilityClasses(config) {
|
|
52
|
-
const classes = [];
|
|
53
|
-
// Minimum breakpoint visibility
|
|
54
|
-
if (config.breakpoint) {
|
|
55
|
-
const bp = config.breakpoint;
|
|
56
|
-
if (bp !== 'xs') {
|
|
57
|
-
classes.push('hidden');
|
|
58
|
-
classes.push(`${bp}:block`);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
// Per-breakpoint hidden overrides
|
|
62
|
-
const hiddenOn = (config.hiddenOn ?? []);
|
|
63
|
-
if (hiddenOn.length > 0) {
|
|
64
|
-
for (let i = 0; i < BREAKPOINT_ORDER.length; i++) {
|
|
65
|
-
const bp = BREAKPOINT_ORDER[i];
|
|
66
|
-
const isHidden = hiddenOn.includes(bp);
|
|
67
|
-
const prevHidden = i > 0 ? hiddenOn.includes(BREAKPOINT_ORDER[i - 1]) : false;
|
|
68
|
-
if (isHidden && !prevHidden) {
|
|
69
|
-
classes.push(bp === 'xs' ? 'hidden' : `${bp}:hidden`);
|
|
70
|
-
}
|
|
71
|
-
else if (!isHidden && prevHidden) {
|
|
72
|
-
classes.push(bp === 'xs' ? 'block' : `${bp}:block`);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return classes;
|
|
77
|
-
}
|
|
78
|
-
// ============================================================================
|
|
79
|
-
// Column Classes
|
|
80
|
-
// ============================================================================
|
|
81
|
-
/**
|
|
82
|
-
* Generate Tailwind grid-cols classes for responsive column layouts.
|
|
83
|
-
*
|
|
84
|
-
* @param config - SpecResponsiveConfig from the spec
|
|
85
|
-
* @returns Array of Tailwind CSS grid column class strings
|
|
86
|
-
*/
|
|
87
|
-
export function getColumnClasses(config) {
|
|
88
|
-
const classes = [];
|
|
89
|
-
const columns = (config.columns ?? {});
|
|
90
|
-
for (const bp of BREAKPOINT_ORDER) {
|
|
91
|
-
const cols = columns[bp];
|
|
92
|
-
if (cols == null)
|
|
93
|
-
continue;
|
|
94
|
-
const prefix = bp === 'xs' ? '' : `${bp}:`;
|
|
95
|
-
classes.push(`${prefix}grid-cols-${cols}`);
|
|
96
|
-
}
|
|
97
|
-
return classes;
|
|
98
|
-
}
|
|
99
|
-
// ============================================================================
|
|
100
|
-
// Order Classes
|
|
101
|
-
// ============================================================================
|
|
102
|
-
/**
|
|
103
|
-
* Generate Tailwind order utility classes for responsive ordering.
|
|
104
|
-
*
|
|
105
|
-
* @param config - SpecResponsiveConfig from the spec
|
|
106
|
-
* @returns Array of Tailwind CSS order class strings
|
|
107
|
-
*/
|
|
108
|
-
export function getOrderClasses(config) {
|
|
109
|
-
const classes = [];
|
|
110
|
-
const order = (config.order ?? {});
|
|
111
|
-
for (const bp of BREAKPOINT_ORDER) {
|
|
112
|
-
const ord = order[bp];
|
|
113
|
-
if (ord == null)
|
|
114
|
-
continue;
|
|
115
|
-
const prefix = bp === 'xs' ? '' : `${bp}:`;
|
|
116
|
-
classes.push(`${prefix}order-${ord}`);
|
|
117
|
-
}
|
|
118
|
-
return classes;
|
|
119
|
-
}
|
|
120
|
-
// ============================================================================
|
|
121
|
-
// Runtime Width Check
|
|
122
|
-
// ============================================================================
|
|
123
|
-
/**
|
|
124
|
-
* Determine whether a component should be hidden at a given viewport width.
|
|
125
|
-
*
|
|
126
|
-
* Checks both the minimum `breakpoint` threshold and the `hiddenOn` list.
|
|
127
|
-
*
|
|
128
|
-
* @param config - SpecResponsiveConfig from the spec
|
|
129
|
-
* @param width - Current viewport width in pixels
|
|
130
|
-
* @returns `true` if the component should be hidden at the given width
|
|
131
|
-
*/
|
|
132
|
-
export function shouldHideAtBreakpoint(config, width) {
|
|
133
|
-
// Check minimum breakpoint
|
|
134
|
-
if (config.breakpoint) {
|
|
135
|
-
const minWidth = BREAKPOINT_VALUES[config.breakpoint];
|
|
136
|
-
if (minWidth !== undefined && width < minWidth) {
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
// Check hiddenOn list
|
|
141
|
-
const hiddenOn = (config.hiddenOn ?? []);
|
|
142
|
-
if (hiddenOn.length > 0) {
|
|
143
|
-
const currentBp = getCurrentBreakpoint(width);
|
|
144
|
-
return hiddenOn.includes(currentBp);
|
|
145
|
-
}
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Determine the current breakpoint name for a given width.
|
|
150
|
-
*/
|
|
151
|
-
function getCurrentBreakpoint(width) {
|
|
152
|
-
for (let i = BREAKPOINT_ORDER.length - 1; i >= 0; i--) {
|
|
153
|
-
if (width >= BREAKPOINT_VALUES[BREAKPOINT_ORDER[i]]) {
|
|
154
|
-
return BREAKPOINT_ORDER[i];
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return 'xs';
|
|
158
|
-
}
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ObjectUI
|
|
3
|
-
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* @object-ui/core - Validation Engine
|
|
10
|
-
*
|
|
11
|
-
* Phase 3.5: Complete validation engine implementation with support for:
|
|
12
|
-
* - Sync and async validation
|
|
13
|
-
* - Cross-field validation
|
|
14
|
-
* - Custom validation functions
|
|
15
|
-
* - Improved error messages
|
|
16
|
-
*
|
|
17
|
-
* @module validation-engine
|
|
18
|
-
* @packageDocumentation
|
|
19
|
-
*/
|
|
20
|
-
import type { AdvancedValidationSchema, ValidationFunction, AsyncValidationFunction, ValidationContext, AdvancedValidationResult, AdvancedValidationError } from '@object-ui/types';
|
|
21
|
-
/**
|
|
22
|
-
* Validation Engine - Executes validation rules
|
|
23
|
-
*/
|
|
24
|
-
export declare class ValidationEngine {
|
|
25
|
-
private customValidators;
|
|
26
|
-
private customAsyncValidators;
|
|
27
|
-
/**
|
|
28
|
-
* Register a custom synchronous validator by name
|
|
29
|
-
*/
|
|
30
|
-
registerValidator(name: string, fn: ValidationFunction): void;
|
|
31
|
-
/**
|
|
32
|
-
* Register a custom asynchronous validator by name
|
|
33
|
-
*/
|
|
34
|
-
registerAsyncValidator(name: string, fn: AsyncValidationFunction): void;
|
|
35
|
-
/**
|
|
36
|
-
* Check if a custom validator is registered
|
|
37
|
-
*/
|
|
38
|
-
hasValidator(name: string): boolean;
|
|
39
|
-
/**
|
|
40
|
-
* Get all registered custom validator names
|
|
41
|
-
*/
|
|
42
|
-
getValidatorNames(): string[];
|
|
43
|
-
/**
|
|
44
|
-
* Validate a value against validation schema
|
|
45
|
-
*/
|
|
46
|
-
validate(value: any, schema: AdvancedValidationSchema, context?: ValidationContext): Promise<AdvancedValidationResult>;
|
|
47
|
-
/**
|
|
48
|
-
* Validate a single rule
|
|
49
|
-
*/
|
|
50
|
-
private validateRule;
|
|
51
|
-
/**
|
|
52
|
-
* Validate built-in rules
|
|
53
|
-
*/
|
|
54
|
-
private validateBuiltInRule;
|
|
55
|
-
/**
|
|
56
|
-
* Evaluate a condition
|
|
57
|
-
* Note: Conditions must be declarative objects, not functions, for security.
|
|
58
|
-
*/
|
|
59
|
-
private evaluateCondition;
|
|
60
|
-
/**
|
|
61
|
-
* Validate multiple fields
|
|
62
|
-
*/
|
|
63
|
-
validateFields(values: Record<string, any>, schemas: Record<string, AdvancedValidationSchema>): Promise<Record<string, AdvancedValidationResult>>;
|
|
64
|
-
/**
|
|
65
|
-
* Check if all fields are valid
|
|
66
|
-
*/
|
|
67
|
-
isValid(results: Record<string, AdvancedValidationResult>): boolean;
|
|
68
|
-
/**
|
|
69
|
-
* Get all errors from validation results
|
|
70
|
-
*/
|
|
71
|
-
getAllErrors(results: Record<string, AdvancedValidationResult>): AdvancedValidationError[];
|
|
72
|
-
/**
|
|
73
|
-
* Get all warnings from validation results
|
|
74
|
-
*/
|
|
75
|
-
getAllWarnings(results: Record<string, AdvancedValidationResult>): AdvancedValidationError[];
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Default validation engine instance
|
|
79
|
-
*/
|
|
80
|
-
export declare const defaultValidationEngine: ValidationEngine;
|
|
81
|
-
/**
|
|
82
|
-
* Convenience function to validate a value
|
|
83
|
-
*/
|
|
84
|
-
export declare function validate(value: any, schema: AdvancedValidationSchema, context?: ValidationContext): Promise<AdvancedValidationResult>;
|
|
85
|
-
/**
|
|
86
|
-
* Convenience function to validate multiple fields
|
|
87
|
-
*/
|
|
88
|
-
export declare function validateFields(values: Record<string, any>, schemas: Record<string, AdvancedValidationSchema>): Promise<Record<string, AdvancedValidationResult>>;
|
|
@@ -1,434 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ObjectUI
|
|
3
|
-
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Validation Engine - Executes validation rules
|
|
10
|
-
*/
|
|
11
|
-
export class ValidationEngine {
|
|
12
|
-
constructor() {
|
|
13
|
-
Object.defineProperty(this, "customValidators", {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
configurable: true,
|
|
16
|
-
writable: true,
|
|
17
|
-
value: new Map()
|
|
18
|
-
});
|
|
19
|
-
Object.defineProperty(this, "customAsyncValidators", {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
configurable: true,
|
|
22
|
-
writable: true,
|
|
23
|
-
value: new Map()
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Register a custom synchronous validator by name
|
|
28
|
-
*/
|
|
29
|
-
registerValidator(name, fn) {
|
|
30
|
-
this.customValidators.set(name, fn);
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Register a custom asynchronous validator by name
|
|
34
|
-
*/
|
|
35
|
-
registerAsyncValidator(name, fn) {
|
|
36
|
-
this.customAsyncValidators.set(name, fn);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Check if a custom validator is registered
|
|
40
|
-
*/
|
|
41
|
-
hasValidator(name) {
|
|
42
|
-
return this.customValidators.has(name) || this.customAsyncValidators.has(name);
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Get all registered custom validator names
|
|
46
|
-
*/
|
|
47
|
-
getValidatorNames() {
|
|
48
|
-
return [
|
|
49
|
-
...Array.from(this.customValidators.keys()),
|
|
50
|
-
...Array.from(this.customAsyncValidators.keys()),
|
|
51
|
-
];
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Validate a value against validation schema
|
|
55
|
-
*/
|
|
56
|
-
async validate(value, schema, context) {
|
|
57
|
-
const errors = [];
|
|
58
|
-
const warnings = [];
|
|
59
|
-
for (const rule of schema.rules) {
|
|
60
|
-
const result = await this.validateRule(value, rule, context);
|
|
61
|
-
if (result) {
|
|
62
|
-
const error = {
|
|
63
|
-
field: schema.field || 'unknown',
|
|
64
|
-
message: result,
|
|
65
|
-
code: rule.type,
|
|
66
|
-
rule: rule.type,
|
|
67
|
-
severity: rule.severity || 'error',
|
|
68
|
-
};
|
|
69
|
-
if (rule.severity === 'warning' || rule.severity === 'info') {
|
|
70
|
-
warnings.push(error);
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
errors.push(error);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
valid: errors.length === 0,
|
|
79
|
-
errors,
|
|
80
|
-
warnings,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Validate a single rule
|
|
85
|
-
*/
|
|
86
|
-
async validateRule(value, rule, context) {
|
|
87
|
-
// Custom async validator (inline)
|
|
88
|
-
if (rule.async_validator) {
|
|
89
|
-
const result = await rule.async_validator(value, context);
|
|
90
|
-
if (result === false) {
|
|
91
|
-
return rule.message || 'Async validation failed';
|
|
92
|
-
}
|
|
93
|
-
if (typeof result === 'string') {
|
|
94
|
-
return result;
|
|
95
|
-
}
|
|
96
|
-
return null;
|
|
97
|
-
}
|
|
98
|
-
// Custom sync validator (inline)
|
|
99
|
-
if (rule.validator) {
|
|
100
|
-
const result = rule.validator(value, context);
|
|
101
|
-
if (result === false) {
|
|
102
|
-
return rule.message || 'Validation failed';
|
|
103
|
-
}
|
|
104
|
-
if (typeof result === 'string') {
|
|
105
|
-
return result;
|
|
106
|
-
}
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
// Registered custom async validator (by name)
|
|
110
|
-
const registeredAsync = this.customAsyncValidators.get(rule.type);
|
|
111
|
-
if (registeredAsync) {
|
|
112
|
-
const result = await registeredAsync(value, context);
|
|
113
|
-
if (result === false) {
|
|
114
|
-
return rule.message || 'Async validation failed';
|
|
115
|
-
}
|
|
116
|
-
if (typeof result === 'string') {
|
|
117
|
-
return result;
|
|
118
|
-
}
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
// Registered custom sync validator (by name)
|
|
122
|
-
const registeredSync = this.customValidators.get(rule.type);
|
|
123
|
-
if (registeredSync) {
|
|
124
|
-
const result = registeredSync(value, context);
|
|
125
|
-
if (result === false) {
|
|
126
|
-
return rule.message || 'Validation failed';
|
|
127
|
-
}
|
|
128
|
-
if (typeof result === 'string') {
|
|
129
|
-
return result;
|
|
130
|
-
}
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
// Built-in validators
|
|
134
|
-
return this.validateBuiltInRule(value, rule, context);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Validate built-in rules
|
|
138
|
-
*/
|
|
139
|
-
async validateBuiltInRule(value, rule, context) {
|
|
140
|
-
const { type, params, message } = rule;
|
|
141
|
-
switch (type) {
|
|
142
|
-
case 'required':
|
|
143
|
-
if (value === null || value === undefined || value === '') {
|
|
144
|
-
return message || 'This field is required';
|
|
145
|
-
}
|
|
146
|
-
break;
|
|
147
|
-
case 'min_length':
|
|
148
|
-
if (typeof value === 'string' && value.length < params) {
|
|
149
|
-
return message || `Minimum length is ${params} characters`;
|
|
150
|
-
}
|
|
151
|
-
break;
|
|
152
|
-
case 'max_length':
|
|
153
|
-
if (typeof value === 'string' && value.length > params) {
|
|
154
|
-
return message || `Maximum length is ${params} characters`;
|
|
155
|
-
}
|
|
156
|
-
break;
|
|
157
|
-
case 'pattern':
|
|
158
|
-
if (typeof value === 'string') {
|
|
159
|
-
try {
|
|
160
|
-
const regex = typeof params === 'string' ? new RegExp(params) : params;
|
|
161
|
-
if (!regex.test(value)) {
|
|
162
|
-
return message || 'Invalid format';
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
catch (error) {
|
|
166
|
-
return message || 'Invalid pattern configuration';
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
break;
|
|
170
|
-
case 'email':
|
|
171
|
-
if (typeof value === 'string') {
|
|
172
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
173
|
-
if (!emailRegex.test(value)) {
|
|
174
|
-
return message || 'Invalid email address';
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
break;
|
|
178
|
-
case 'url':
|
|
179
|
-
if (typeof value === 'string') {
|
|
180
|
-
try {
|
|
181
|
-
new URL(value);
|
|
182
|
-
}
|
|
183
|
-
catch {
|
|
184
|
-
return message || 'Invalid URL';
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
break;
|
|
188
|
-
case 'phone':
|
|
189
|
-
if (typeof value === 'string') {
|
|
190
|
-
const phoneRegex = /^[\d\s\-+()]+$/;
|
|
191
|
-
if (!phoneRegex.test(value)) {
|
|
192
|
-
return message || 'Invalid phone number';
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
break;
|
|
196
|
-
case 'min':
|
|
197
|
-
if (typeof value === 'number' && value < params) {
|
|
198
|
-
return message || `Minimum value is ${params}`;
|
|
199
|
-
}
|
|
200
|
-
break;
|
|
201
|
-
case 'max':
|
|
202
|
-
if (typeof value === 'number' && value > params) {
|
|
203
|
-
return message || `Maximum value is ${params}`;
|
|
204
|
-
}
|
|
205
|
-
break;
|
|
206
|
-
case 'integer':
|
|
207
|
-
if (typeof value === 'number' && !Number.isInteger(value)) {
|
|
208
|
-
return message || 'Value must be an integer';
|
|
209
|
-
}
|
|
210
|
-
break;
|
|
211
|
-
case 'positive':
|
|
212
|
-
if (typeof value === 'number' && value <= 0) {
|
|
213
|
-
return message || 'Value must be positive';
|
|
214
|
-
}
|
|
215
|
-
break;
|
|
216
|
-
case 'negative':
|
|
217
|
-
if (typeof value === 'number' && value >= 0) {
|
|
218
|
-
return message || 'Value must be negative';
|
|
219
|
-
}
|
|
220
|
-
break;
|
|
221
|
-
case 'date_min':
|
|
222
|
-
if (value instanceof Date || typeof value === 'string') {
|
|
223
|
-
const date = value instanceof Date ? value : new Date(value);
|
|
224
|
-
const minDate = params instanceof Date ? params : new Date(params);
|
|
225
|
-
if (date < minDate) {
|
|
226
|
-
return message || `Date must be after ${minDate.toLocaleDateString()}`;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
break;
|
|
230
|
-
case 'date_max':
|
|
231
|
-
if (value instanceof Date || typeof value === 'string') {
|
|
232
|
-
const date = value instanceof Date ? value : new Date(value);
|
|
233
|
-
const maxDate = params instanceof Date ? params : new Date(params);
|
|
234
|
-
if (date > maxDate) {
|
|
235
|
-
return message || `Date must be before ${maxDate.toLocaleDateString()}`;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
break;
|
|
239
|
-
case 'date_future':
|
|
240
|
-
if (value instanceof Date || typeof value === 'string') {
|
|
241
|
-
const date = value instanceof Date ? value : new Date(value);
|
|
242
|
-
if (date <= new Date()) {
|
|
243
|
-
return message || 'Date must be in the future';
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
break;
|
|
247
|
-
case 'date_past':
|
|
248
|
-
if (value instanceof Date || typeof value === 'string') {
|
|
249
|
-
const date = value instanceof Date ? value : new Date(value);
|
|
250
|
-
if (date >= new Date()) {
|
|
251
|
-
return message || 'Date must be in the past';
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
break;
|
|
255
|
-
case 'min_items':
|
|
256
|
-
if (Array.isArray(value) && value.length < params) {
|
|
257
|
-
return message || `Minimum ${params} items required`;
|
|
258
|
-
}
|
|
259
|
-
break;
|
|
260
|
-
case 'max_items':
|
|
261
|
-
if (Array.isArray(value) && value.length > params) {
|
|
262
|
-
return message || `Maximum ${params} items allowed`;
|
|
263
|
-
}
|
|
264
|
-
break;
|
|
265
|
-
case 'unique_items':
|
|
266
|
-
if (Array.isArray(value)) {
|
|
267
|
-
const unique = new Set(value);
|
|
268
|
-
if (unique.size !== value.length) {
|
|
269
|
-
return message || 'All items must be unique';
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
break;
|
|
273
|
-
case 'field_match':
|
|
274
|
-
if (context?.values && params) {
|
|
275
|
-
const otherValue = context.values[params];
|
|
276
|
-
if (value !== otherValue) {
|
|
277
|
-
return message || `Value must match ${params}`;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
break;
|
|
281
|
-
case 'field_compare':
|
|
282
|
-
if (context?.values && params) {
|
|
283
|
-
const { field: otherField, operator } = params;
|
|
284
|
-
const otherValue = context.values[otherField];
|
|
285
|
-
switch (operator) {
|
|
286
|
-
case '>':
|
|
287
|
-
if (value <= otherValue) {
|
|
288
|
-
return message || `Value must be greater than ${otherField}`;
|
|
289
|
-
}
|
|
290
|
-
break;
|
|
291
|
-
case '<':
|
|
292
|
-
if (value >= otherValue) {
|
|
293
|
-
return message || `Value must be less than ${otherField}`;
|
|
294
|
-
}
|
|
295
|
-
break;
|
|
296
|
-
case '>=':
|
|
297
|
-
if (value < otherValue) {
|
|
298
|
-
return message || `Value must be greater than or equal to ${otherField}`;
|
|
299
|
-
}
|
|
300
|
-
break;
|
|
301
|
-
case '<=':
|
|
302
|
-
if (value > otherValue) {
|
|
303
|
-
return message || `Value must be less than or equal to ${otherField}`;
|
|
304
|
-
}
|
|
305
|
-
break;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
break;
|
|
309
|
-
case 'conditional':
|
|
310
|
-
if (context?.values && params) {
|
|
311
|
-
const { condition, rules } = params;
|
|
312
|
-
if (!Array.isArray(rules) || rules.length === 0) {
|
|
313
|
-
break;
|
|
314
|
-
}
|
|
315
|
-
const conditionMet = this.evaluateCondition(condition, context.values);
|
|
316
|
-
if (conditionMet) {
|
|
317
|
-
for (const conditionalRule of rules) {
|
|
318
|
-
const result = await this.validateRule(value, conditionalRule, context);
|
|
319
|
-
if (result) {
|
|
320
|
-
return result;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
break;
|
|
326
|
-
default:
|
|
327
|
-
// Unhandled validation rule type
|
|
328
|
-
console.warn(`Unsupported validation rule type: ${type}`);
|
|
329
|
-
return null;
|
|
330
|
-
}
|
|
331
|
-
return null;
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* Evaluate a condition
|
|
335
|
-
* Note: Conditions must be declarative objects, not functions, for security.
|
|
336
|
-
*/
|
|
337
|
-
evaluateCondition(condition, values) {
|
|
338
|
-
if (typeof condition === 'function') {
|
|
339
|
-
console.warn('Function-based conditions are deprecated and will be removed. Use declarative conditions instead.\n\n' +
|
|
340
|
-
' Migration:\n' +
|
|
341
|
-
' // Before (deprecated):\n' +
|
|
342
|
-
' { condition: (values) => values.age > 18 }\n\n' +
|
|
343
|
-
' // After:\n' +
|
|
344
|
-
' { condition: { field: "age", operator: ">", value: 18 } }\n\n' +
|
|
345
|
-
' See: https://github.com/objectstack-ai/objectui/blob/main/MIGRATION_GUIDE.md');
|
|
346
|
-
return false; // Security: reject function-based conditions
|
|
347
|
-
}
|
|
348
|
-
if (typeof condition === 'object' && condition.field) {
|
|
349
|
-
const fieldValue = values[condition.field];
|
|
350
|
-
const { operator = '=', value } = condition;
|
|
351
|
-
switch (operator) {
|
|
352
|
-
case '=':
|
|
353
|
-
return fieldValue === value;
|
|
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 'in':
|
|
365
|
-
return Array.isArray(value) && value.includes(fieldValue);
|
|
366
|
-
default:
|
|
367
|
-
return false;
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
return false;
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Validate multiple fields
|
|
374
|
-
*/
|
|
375
|
-
async validateFields(values, schemas) {
|
|
376
|
-
const results = {};
|
|
377
|
-
const context = {
|
|
378
|
-
values,
|
|
379
|
-
};
|
|
380
|
-
for (const [field, schema] of Object.entries(schemas)) {
|
|
381
|
-
const value = values[field];
|
|
382
|
-
const schemaWithField = {
|
|
383
|
-
...schema,
|
|
384
|
-
field: schema.field ?? field,
|
|
385
|
-
};
|
|
386
|
-
results[field] = await this.validate(value, schemaWithField, context);
|
|
387
|
-
}
|
|
388
|
-
return results;
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Check if all fields are valid
|
|
392
|
-
*/
|
|
393
|
-
isValid(results) {
|
|
394
|
-
return Object.values(results).every(result => result.valid);
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* Get all errors from validation results
|
|
398
|
-
*/
|
|
399
|
-
getAllErrors(results) {
|
|
400
|
-
const errors = [];
|
|
401
|
-
for (const result of Object.values(results)) {
|
|
402
|
-
errors.push(...result.errors);
|
|
403
|
-
}
|
|
404
|
-
return errors;
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Get all warnings from validation results
|
|
408
|
-
*/
|
|
409
|
-
getAllWarnings(results) {
|
|
410
|
-
const warnings = [];
|
|
411
|
-
for (const result of Object.values(results)) {
|
|
412
|
-
if (result.warnings) {
|
|
413
|
-
warnings.push(...result.warnings);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
return warnings;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
/**
|
|
420
|
-
* Default validation engine instance
|
|
421
|
-
*/
|
|
422
|
-
export const defaultValidationEngine = new ValidationEngine();
|
|
423
|
-
/**
|
|
424
|
-
* Convenience function to validate a value
|
|
425
|
-
*/
|
|
426
|
-
export async function validate(value, schema, context) {
|
|
427
|
-
return defaultValidationEngine.validate(value, schema, context);
|
|
428
|
-
}
|
|
429
|
-
/**
|
|
430
|
-
* Convenience function to validate multiple fields
|
|
431
|
-
*/
|
|
432
|
-
export async function validateFields(values, schemas) {
|
|
433
|
-
return defaultValidationEngine.validateFields(values, schemas);
|
|
434
|
-
}
|