@objectql/types 1.3.1 → 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 CHANGED
@@ -1,5 +1,24 @@
1
1
  # @objectql/core
2
2
 
3
+ ## 1.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Minor version release - 1.5.0
8
+
9
+ ## 1.4.0
10
+
11
+ ### Minor Changes
12
+
13
+ - Release version 1.4.0 with new features and enhancements:
14
+ - Added complete REST API implementation with CRUD operations
15
+ - Enhanced error handling with standardized error codes and HTTP status mapping
16
+ - Added AI context support for tracking intent and use cases
17
+ - Enhanced metadata API with detailed field information and action listing
18
+ - Improved JSON-RPC API with better error categorization
19
+ - Added hooks and actions validation and implementation
20
+ - Updated documentation and examples
21
+
3
22
  ## 1.3.1
4
23
 
5
24
  ## 1.3.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ObjectQL Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,53 +1,260 @@
1
- # @objectql/core
1
+ # @objectql/types
2
2
 
3
- The core ORM and runtime engine for ObjectQL. This package handles object querying, CRUD operations, database driver coordination, and transaction management.
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
- - **Unified Query Language**: A generic way to query data across different databases (SQL, Mongo, etc.).
8
- - **Repository Pattern**: `ObjectRepository` for managing object records.
9
- - **Driver Agnostic**: Abstraction layer for database drivers.
10
- - **Dynamic Schema**: Loads object definitions from `@objectql/metadata`.
11
- - **Hooks & Actions**: Runtime logic injection.
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/core
17
+ npm install @objectql/types
17
18
  ```
18
19
 
19
- ## Usage
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 { ObjectQL } from '@objectql/core';
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 objectql = new ObjectQL({
27
- datasources: {
28
- default: new MyDriver({ ... })
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
- await objectql.init();
203
+ Base interface for all validation rules:
33
204
 
34
- // Use context for operations
35
- const ctx = objectql.createContext({ userId: 'u-1' });
36
- const projects = await ctx.object('project').find({
37
- filters: [['status', '=', 'active']]
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
- ## Shared Metadata
223
+ ### ValidationCondition
42
224
 
43
- You can pass an existing `MetadataRegistry` (e.g., from a low-code platform loader) to ObjectQL:
225
+ Condition structure for validation rules:
44
226
 
45
227
  ```typescript
46
- const registry = new MetadataRegistry();
47
- // ... pre-load metadata ...
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
- const objectql = new ObjectQL({
50
- registry: registry,
51
- datasources: { ... }
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/app.d.ts CHANGED
@@ -15,6 +15,11 @@ export interface IObjectQL {
15
15
  registerObject(object: ObjectConfig): void;
16
16
  loadFromDirectory(dir: string): void;
17
17
  addLoader(plugin: LoaderPlugin): void;
18
+ /**
19
+ * Updates and persists metadata content.
20
+ * Only works if the metadata was loaded from a writable file source (e.g. local disk).
21
+ */
22
+ updateMetadata(type: string, id: string, content: any): Promise<void>;
18
23
  on(event: HookName, objectName: string, handler: HookHandler): void;
19
24
  triggerHook(event: HookName, objectName: string, ctx: HookContext): Promise<void>;
20
25
  registerAction(objectName: string, actionName: string, handler: ActionHandler): void;
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/hook.d.ts CHANGED
@@ -67,18 +67,17 @@ export interface MutationHookContext<T = any> extends BaseHookContext<T> {
67
67
  * Only available in 'after' hooks.
68
68
  */
69
69
  result?: T;
70
+ /**
71
+ * The existing record fetched from DB before operation.
72
+ * Available in 'update' and 'delete' hooks.
73
+ */
74
+ previousData?: T;
70
75
  }
71
76
  /**
72
77
  * Specialized context for Updates, including change tracking.
73
78
  */
74
79
  export interface UpdateHookContext<T = any> extends MutationHookContext<T> {
75
80
  operation: 'update';
76
- /**
77
- * The record state BEFORE the update.
78
- * Useful for comparison logic (e.g. status changed from A to B).
79
- * Note: This may require a pre-fetch lookup depending on engine configuration.
80
- */
81
- previousData?: T;
82
81
  /**
83
82
  * Helper to check if a specific field is being modified.
84
83
  * Checks if the field exists in 'data' AND is different from 'previousData'.
package/dist/index.d.ts CHANGED
@@ -10,4 +10,5 @@ export * from './app';
10
10
  export * from './plugin';
11
11
  export * from './config';
12
12
  export * from './context';
13
+ export * from './validation';
13
14
  export * from './loader';
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;AAG1B,2CAAyB"}
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.