@objectql/types 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +237 -30
- package/dist/field.d.ts +11 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/object.d.ts +11 -0
- package/dist/validation.d.ts +341 -0
- package/dist/validation.js +20 -0
- package/dist/validation.js.map +1 -0
- package/package.json +1 -1
- package/src/field.ts +14 -0
- package/src/index.ts +1 -0
- package/src/object.ts +11 -0
- package/src/validation.ts +390 -0
- package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,53 +1,260 @@
|
|
|
1
|
-
# @objectql/
|
|
1
|
+
# @objectql/types
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Type definitions for the ObjectQL system, including object schemas, field configurations, validation rules, queries, hooks, and actions.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- **
|
|
8
|
-
- **
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
11
|
-
- **
|
|
7
|
+
- **Object & Field Types**: Define data models with `ObjectConfig` and `FieldConfig`
|
|
8
|
+
- **Query Types**: Type-safe query definitions with `UnifiedQuery`
|
|
9
|
+
- **Validation Types**: Comprehensive validation rule types and interfaces
|
|
10
|
+
- **Hook & Action Types**: Event-driven logic type definitions
|
|
11
|
+
- **Driver Interface**: Abstraction layer for database drivers
|
|
12
|
+
- **AI Context**: Metadata for AI-friendly documentation
|
|
12
13
|
|
|
13
14
|
## Installation
|
|
14
15
|
|
|
15
16
|
```bash
|
|
16
|
-
npm install @objectql/
|
|
17
|
+
npm install @objectql/types
|
|
17
18
|
```
|
|
18
19
|
|
|
19
|
-
##
|
|
20
|
+
## Exported Types
|
|
21
|
+
|
|
22
|
+
### Core Types
|
|
23
|
+
- `ObjectConfig` - Object schema definition
|
|
24
|
+
- `FieldConfig` - Field configuration with validation
|
|
25
|
+
- `FieldType` - Supported field data types
|
|
26
|
+
- `ObjectDoc` - Base interface for all documents
|
|
27
|
+
|
|
28
|
+
### Validation Types
|
|
29
|
+
- `ValidationRule` - Base validation rule interface
|
|
30
|
+
- `AnyValidationRule` - Union of all validation rule types
|
|
31
|
+
- `ValidationContext` - Context provided to validators
|
|
32
|
+
- `ValidationResult` - Result of validation execution
|
|
33
|
+
- `ValidationRuleResult` - Result of a single rule
|
|
34
|
+
- `ValidationError` - Validation error class
|
|
35
|
+
- `ValidatorOptions` - Configuration for Validator class
|
|
36
|
+
|
|
37
|
+
**Validation Rule Types:**
|
|
38
|
+
- `CrossFieldValidationRule` - Compare fields with operators
|
|
39
|
+
- `StateMachineValidationRule` - Enforce state transitions
|
|
40
|
+
- `BusinessRuleValidationRule` - Complex business rules
|
|
41
|
+
- `UniquenessValidationRule` - Uniqueness constraints
|
|
42
|
+
- `DependencyValidationRule` - Related record validation
|
|
43
|
+
- `CustomValidationRule` - Custom validation functions
|
|
44
|
+
|
|
45
|
+
**Helper Types:**
|
|
46
|
+
- `ValidationRuleType` - Types of validation rules
|
|
47
|
+
- `ValidationSeverity` - Error severity levels (error, warning, info)
|
|
48
|
+
- `ValidationTrigger` - When to run validation (create, update, delete)
|
|
49
|
+
- `ValidationOperator` - Comparison operators (=, !=, >, >=, <, <=, in, contains, etc.)
|
|
50
|
+
- `ValidationCondition` - Condition structure for rules
|
|
51
|
+
- `ValidationAiContext` - AI-friendly metadata for rules
|
|
52
|
+
- `FieldValidation` - Field-level validation configuration
|
|
53
|
+
|
|
54
|
+
### Query Types
|
|
55
|
+
- `UnifiedQuery` - Generic query structure
|
|
56
|
+
- `QueryFilter` - Filter conditions
|
|
57
|
+
- `QuerySort` - Sort specifications
|
|
58
|
+
|
|
59
|
+
### Hook & Action Types
|
|
60
|
+
- `HookContext` - Context for hook execution
|
|
61
|
+
- `ActionContext` - Context for action execution
|
|
62
|
+
- `HookHandler` - Hook function signature
|
|
63
|
+
- `ActionHandler` - Action function signature
|
|
64
|
+
|
|
65
|
+
### Driver Types
|
|
66
|
+
- `Driver` - Database driver interface
|
|
67
|
+
- `DriverConfig` - Driver configuration
|
|
68
|
+
|
|
69
|
+
## Usage Examples
|
|
70
|
+
|
|
71
|
+
### Object Definition with Validation
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { ObjectConfig, FieldConfig } from '@objectql/types';
|
|
75
|
+
|
|
76
|
+
const projectObject: ObjectConfig = {
|
|
77
|
+
name: 'project',
|
|
78
|
+
label: 'Project',
|
|
79
|
+
fields: {
|
|
80
|
+
name: {
|
|
81
|
+
type: 'text',
|
|
82
|
+
label: 'Project Name',
|
|
83
|
+
required: true,
|
|
84
|
+
validation: {
|
|
85
|
+
min_length: 3,
|
|
86
|
+
max_length: 100,
|
|
87
|
+
pattern: '^[a-zA-Z0-9\\s]+$',
|
|
88
|
+
message: 'Name must be 3-100 alphanumeric characters'
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
email: {
|
|
92
|
+
type: 'email',
|
|
93
|
+
validation: {
|
|
94
|
+
format: 'email',
|
|
95
|
+
message: 'Please enter a valid email address'
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
status: {
|
|
99
|
+
type: 'select',
|
|
100
|
+
options: [
|
|
101
|
+
{ label: 'Planning', value: 'planning' },
|
|
102
|
+
{ label: 'Active', value: 'active' },
|
|
103
|
+
{ label: 'Completed', value: 'completed' }
|
|
104
|
+
],
|
|
105
|
+
ai_context: {
|
|
106
|
+
intent: 'Track project lifecycle',
|
|
107
|
+
rationale: 'Projects follow a controlled workflow'
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
validation: {
|
|
112
|
+
rules: [
|
|
113
|
+
{
|
|
114
|
+
name: 'valid_date_range',
|
|
115
|
+
type: 'cross_field',
|
|
116
|
+
rule: {
|
|
117
|
+
field: 'end_date',
|
|
118
|
+
operator: '>=',
|
|
119
|
+
compare_to: 'start_date'
|
|
120
|
+
},
|
|
121
|
+
message: 'End date must be on or after start date',
|
|
122
|
+
error_code: 'INVALID_DATE_RANGE'
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Validation Rule Definition
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import {
|
|
133
|
+
CrossFieldValidationRule,
|
|
134
|
+
StateMachineValidationRule,
|
|
135
|
+
ValidationContext
|
|
136
|
+
} from '@objectql/types';
|
|
137
|
+
|
|
138
|
+
// Cross-field validation
|
|
139
|
+
const dateRangeRule: CrossFieldValidationRule = {
|
|
140
|
+
name: 'valid_date_range',
|
|
141
|
+
type: 'cross_field',
|
|
142
|
+
rule: {
|
|
143
|
+
field: 'end_date',
|
|
144
|
+
operator: '>=',
|
|
145
|
+
compare_to: 'start_date'
|
|
146
|
+
},
|
|
147
|
+
message: 'End date must be on or after start date',
|
|
148
|
+
severity: 'error',
|
|
149
|
+
trigger: ['create', 'update']
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// State machine validation
|
|
153
|
+
const statusRule: StateMachineValidationRule = {
|
|
154
|
+
name: 'status_transition',
|
|
155
|
+
type: 'state_machine',
|
|
156
|
+
field: 'status',
|
|
157
|
+
transitions: {
|
|
158
|
+
planning: {
|
|
159
|
+
allowed_next: ['active', 'cancelled'],
|
|
160
|
+
ai_context: {
|
|
161
|
+
rationale: 'Can start work or cancel before beginning'
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
active: {
|
|
165
|
+
allowed_next: ['completed', 'cancelled']
|
|
166
|
+
},
|
|
167
|
+
completed: {
|
|
168
|
+
allowed_next: [],
|
|
169
|
+
is_terminal: true
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
message: 'Invalid status transition from {{old_status}} to {{new_status}}'
|
|
173
|
+
};
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Using Validation Context
|
|
20
177
|
|
|
21
178
|
```typescript
|
|
22
|
-
import {
|
|
23
|
-
import { MetadataRegistry } from '@objectql/metadata';
|
|
24
|
-
// Import a driver, e.g., @objectql/driver-knex
|
|
179
|
+
import { ValidationContext, ValidationTrigger } from '@objectql/types';
|
|
25
180
|
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
181
|
+
const context: ValidationContext = {
|
|
182
|
+
record: {
|
|
183
|
+
start_date: '2024-01-01',
|
|
184
|
+
end_date: '2024-12-31',
|
|
185
|
+
status: 'active'
|
|
186
|
+
},
|
|
187
|
+
previousRecord: {
|
|
188
|
+
status: 'planning'
|
|
189
|
+
},
|
|
190
|
+
operation: 'update',
|
|
191
|
+
changedFields: ['status'],
|
|
192
|
+
metadata: {
|
|
193
|
+
objectName: 'project',
|
|
194
|
+
ruleName: 'status_transition'
|
|
29
195
|
}
|
|
30
|
-
}
|
|
196
|
+
};
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Type Reference
|
|
200
|
+
|
|
201
|
+
### ValidationRule
|
|
31
202
|
|
|
32
|
-
|
|
203
|
+
Base interface for all validation rules:
|
|
33
204
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
205
|
+
```typescript
|
|
206
|
+
interface ValidationRule {
|
|
207
|
+
name: string;
|
|
208
|
+
type: ValidationRuleType;
|
|
209
|
+
message: string | Record<string, string>;
|
|
210
|
+
error_code?: string;
|
|
211
|
+
severity?: ValidationSeverity;
|
|
212
|
+
trigger?: ValidationTrigger[];
|
|
213
|
+
fields?: string[];
|
|
214
|
+
context?: string[];
|
|
215
|
+
skip_bulk?: boolean;
|
|
216
|
+
ai_context?: ValidationAiContext;
|
|
217
|
+
apply_when?: ValidationCondition;
|
|
218
|
+
async?: boolean;
|
|
219
|
+
timeout?: number;
|
|
220
|
+
}
|
|
39
221
|
```
|
|
40
222
|
|
|
41
|
-
|
|
223
|
+
### ValidationCondition
|
|
42
224
|
|
|
43
|
-
|
|
225
|
+
Condition structure for validation rules:
|
|
44
226
|
|
|
45
227
|
```typescript
|
|
46
|
-
|
|
47
|
-
|
|
228
|
+
interface ValidationCondition {
|
|
229
|
+
field?: string;
|
|
230
|
+
operator?: ValidationOperator;
|
|
231
|
+
value?: any;
|
|
232
|
+
compare_to?: string; // For cross-field comparison
|
|
233
|
+
expression?: string;
|
|
234
|
+
all_of?: ValidationCondition[];
|
|
235
|
+
any_of?: ValidationCondition[];
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### FieldValidation
|
|
240
|
+
|
|
241
|
+
Field-level validation configuration:
|
|
48
242
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
243
|
+
```typescript
|
|
244
|
+
interface FieldValidation {
|
|
245
|
+
format?: 'email' | 'url' | 'phone' | 'date' | 'datetime';
|
|
246
|
+
protocols?: string[];
|
|
247
|
+
min?: number;
|
|
248
|
+
max?: number;
|
|
249
|
+
min_length?: number;
|
|
250
|
+
max_length?: number;
|
|
251
|
+
pattern?: string;
|
|
252
|
+
message?: string;
|
|
253
|
+
}
|
|
53
254
|
```
|
|
255
|
+
|
|
256
|
+
## See Also
|
|
257
|
+
|
|
258
|
+
- [@objectql/core](../core) - Core engine with Validator class
|
|
259
|
+
- [Validation Specification](../../docs/spec/validation.md) - Complete validation metadata specification
|
|
260
|
+
|
package/dist/field.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FieldValidation, ValidationAiContext } from './validation';
|
|
1
2
|
/**
|
|
2
3
|
* Represents the supported field data types in the ObjectQL schema.
|
|
3
4
|
* These types determine how data is stored, validated, and rendered.
|
|
@@ -72,6 +73,16 @@ export interface FieldConfig {
|
|
|
72
73
|
max_length?: number;
|
|
73
74
|
/** Regular expression pattern for validation. */
|
|
74
75
|
regex?: string;
|
|
76
|
+
/**
|
|
77
|
+
* Field validation configuration.
|
|
78
|
+
* Defines validation rules applied at the field level.
|
|
79
|
+
*/
|
|
80
|
+
validation?: FieldValidation;
|
|
81
|
+
/**
|
|
82
|
+
* AI context for the field.
|
|
83
|
+
* Provides semantic information for AI tools.
|
|
84
|
+
*/
|
|
85
|
+
ai_context?: ValidationAiContext;
|
|
75
86
|
/** Dimension of the vector for 'vector' type fields. */
|
|
76
87
|
dimension?: number;
|
|
77
88
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -26,5 +26,6 @@ __exportStar(require("./app"), exports);
|
|
|
26
26
|
__exportStar(require("./plugin"), exports);
|
|
27
27
|
__exportStar(require("./config"), exports);
|
|
28
28
|
__exportStar(require("./context"), exports);
|
|
29
|
+
__exportStar(require("./validation"), exports);
|
|
29
30
|
__exportStar(require("./loader"), exports);
|
|
30
31
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,2CAAyB;AACzB,0CAAwB;AACxB,6CAA2B;AAC3B,yCAAuB;AACvB,2CAAyB;AACzB,+CAA6B;AAC7B,wCAAsB;AACtB,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,2CAAyB;AACzB,0CAAwB;AACxB,6CAA2B;AAC3B,yCAAuB;AACvB,2CAAyB;AACzB,+CAA6B;AAC7B,wCAAsB;AACtB,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;AAC1B,+CAA6B;AAG7B,2CAAyB"}
|
package/dist/object.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { FieldConfig } from './field';
|
|
2
2
|
import { ActionConfig } from './action';
|
|
3
|
+
import { AnyValidationRule } from './validation';
|
|
3
4
|
export interface IndexConfig {
|
|
4
5
|
/** List of fields involved in the index */
|
|
5
6
|
fields: string[];
|
|
@@ -31,6 +32,16 @@ export interface ObjectConfig {
|
|
|
31
32
|
/** AI capabilities configuration */
|
|
32
33
|
ai?: ObjectAiConfig;
|
|
33
34
|
actions?: Record<string, ActionConfig>;
|
|
35
|
+
/** Validation rules for this object */
|
|
36
|
+
validation?: {
|
|
37
|
+
/** Validation rules */
|
|
38
|
+
rules?: AnyValidationRule[];
|
|
39
|
+
/** AI context for validation strategy */
|
|
40
|
+
ai_context?: {
|
|
41
|
+
intent?: string;
|
|
42
|
+
validation_strategy?: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
34
45
|
}
|
|
35
46
|
/**
|
|
36
47
|
* Base interface for all ObjectQL documents.
|