Skip to content

Modularize Validator and Formula Engine as ObjectStack plugins#157

Merged
huangyiirene merged 2 commits intomainfrom
copilot/modularize-validator-formula-plugin
Jan 22, 2026
Merged

Modularize Validator and Formula Engine as ObjectStack plugins#157
huangyiirene merged 2 commits intomainfrom
copilot/modularize-validator-formula-plugin

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 22, 2026

Completes Phase 3.4-3.5 of core refactoring: wraps Validator and FormulaEngine into standalone plugins implementing ObjectStack Runtime protocol.

Changes

  • ValidatorPlugin (validator-plugin.ts)

    • Registers beforeQuery and beforeMutation middleware hooks
    • Conditional enable/disable via enableQueryValidation and enableMutationValidation
    • Delegates to existing Validator class
  • FormulaPlugin (formula-plugin.ts)

    • Registers formula provider via kernel.registerFormulaProvider() with fallback to direct property assignment
    • Auto-evaluation middleware on afterQuery (configurable)
    • Delegates to existing FormulaEngine class
  • ObjectQLPlugin refactor (plugin.ts)

    • Replaces direct registration methods with conditional sub-plugin installation
    • Extended config interface accepts validatorConfig and formulaConfig
    • Backward compatible: all features enabled by default

Usage

// Selective feature enablement
const plugin = new ObjectQLPlugin({
  enableValidator: true,
  validatorConfig: {
    language: 'zh-CN',
    enableQueryValidation: false
  },
  enableFormulas: true,
  formulaConfig: {
    enable_cache: true,
    cache_ttl: 600
  }
});

// Sub-plugins are also directly instantiable
const validatorPlugin = new ValidatorPlugin({ language: 'zh-CN' });
await validatorPlugin.install(ctx);

Architecture

Each feature is now a first-class plugin with lifecycle hooks, enabling modular composition and independent configuration. Gracefully degrades when kernel lacks middleware/provider support.

Original prompt

太棒了!核心的 Repository 改造完成意味着数据流已经打通。接下来的任务是将 ObjectQL 的“特色功能”——验证器 (Validator)公式引擎 (Formula Engine) ——也拆解为符合 ObjectStack 标准的插件。

这是针对 Phase 3 Part 3: Feature Plugins Integration 的完整提示词。


针对内核包迁移第三部分的完整提示词 (Prompt)

Role: Expert TypeScript Architect
Context: ObjectQL Migration Phase 3 (Core Refactoring) - Part 3
Target: @objectql/core package
Reference: IMPLEMENTATION_ROADMAP.md (Task 3.4 & 3.5)

Task:
Modularize the Validator and Formula Engine components by wrapping them into ObjectStack plugins. This allows them to be registered into the Kernel's lifecycle hooks.

Specific Actions:

  1. Create Validator Plugin (packages/foundation/core/src/validator-plugin.ts):

    • Create a class ValidatorPlugin implementing Plugin from @objectstack/runtime.
    • In the install method, register middleware hooks (e.g., beforeQuery, beforeMutation) that invoke the legacy Validator class.
    • Note: Ensure you import the existing Validator class from ./validator.
  2. Create Formula Plugin (packages/foundation/core/src/formula-plugin.ts):

    • Create a class FormulaPlugin implementing Plugin.
    • In the install method, register the formula provider using kernel.registerFormulaProvider (or equivalent API).
    • Delegate the actual calculation to the existing FormulaEngine class.
  3. Update Main Plugin (packages/foundation/core/src/plugin.ts):

    • Update the ObjectQLPlugin.install method.
    • Based on the config (e.g., enableValidator, enableFormulas), instantiate and install these new sub-plugins (or call their installation logic).

Code Reference (from Roadmap):

// packages/foundation/core/src/validator-plugin.ts
import type { Plugin, ObjectStackKernel } from '@objectstack/runtime';
import { Validator } from './validator';

export class ValidatorPlugin implements Plugin {
  name = '@objectql/validator';
  version = '4.0.0';
  
  async install(kernel: ObjectStackKernel) {
    // Register validation middleware
    // Note: Adjust hook names 'beforeQuery'/'beforeMutation' based on actual Runtime API
    kernel.use('beforeQuery', async (context) => {
      const validator = new Validator();
      await validator.validate(context.query);
    });
    
    kernel.use('beforeMutation', async (context) => {
      const validator = new Validator();
      await validator.validate(context.data);
    });
  }
}
// packages/foundation/core/src/formula-plugin.ts
import type { Plugin, ObjectStackKernel } from '@objectstack/runtime';
import { FormulaEngine } from './formula-engine';

export class FormulaPlugin implements Plugin {
  name = '@objectql/formulas';
  version = '4.0.0';
  
  async install(kernel: ObjectStackKernel) {
    const engine = new FormulaEngine();
    
    // Register formula processor
    // Note: 'registerFormulaProvider' is a placeholder, use the actual Kernel API for extending capabilities
    if (typeof kernel['registerFormulaProvider'] === 'function') {
        kernel['registerFormulaProvider']({
            evaluate: (formula: string, context: any) => {
                return engine.evaluate(formula, context);
            }
        });
    }
  }
}
// packages/foundation/core/src/plugin.ts (Update)
import { ValidatorPlugin } from './validator-plugin';
import { FormulaPlugin } from './formula-plugin';

// ... inside ObjectQLPlugin class ...
async install(kernel: ObjectStackKernel): Promise<void> {
  // ... existing code ...

  if (this.config.enableValidator !== false) {
    await new ValidatorPlugin().install(kernel);
  }

  if (this.config.enableFormulas !== false) {
    await new FormulaPlugin().install(kernel);
  }
}

Constraints:

  • Assume Validator and FormulaEngine classes already exist in the directory.
  • If ObjectStackKernel does not yet expose explicit types for registerFormulaProvider, use proper type casting or checks to prevent compilation errors.

💡 提示

完成这一步后,Phase 3 的核心重构就基本完成了 80%。剩下的就是 AI Agent 集成 (Task 3.6)集成测试 (Task 3.7)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@vercel
Copy link
Copy Markdown

vercel bot commented Jan 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
objectql-site Ready Ready Preview, Comment Jan 22, 2026 4:01am

Request Review

Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
*/

import { FormulaPlugin } from '../src/formula-plugin';
import { ObjectStackKernel } from '@objectstack/runtime';
*/

import { ValidatorPlugin } from '../src/validator-plugin';
import { ObjectStackKernel } from '@objectstack/runtime';
Copilot AI changed the title [WIP] Modularize Validator and Formula Engine into plugins Modularize Validator and Formula Engine as ObjectStack plugins Jan 22, 2026
Copilot AI requested a review from huangyiirene January 22, 2026 04:03
@github-actions
Copy link
Copy Markdown
Contributor

⚠️ No Changeset Found

This PR does not include a changeset file.
If this PR includes user-facing changes, please add a changeset by running:

pnpm changeset

@huangyiirene huangyiirene requested review from Copilot and removed request for huangyiirene January 22, 2026 04:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR modularizes the Validator and Formula Engine as ObjectStack plugins, completing Phase 3.4-3.5 of the core refactoring. The changes extract these features into standalone, lifecycle-aware plugins that integrate with the ObjectStack Runtime protocol.

Changes:

  • Created ValidatorPlugin and FormulaPlugin as first-class RuntimePlugin implementations
  • Refactored ObjectQLPlugin to conditionally install sub-plugins instead of direct registration
  • Extended configuration interfaces to support granular feature enablement

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/foundation/core/src/validator-plugin.ts New standalone validator plugin with lifecycle hooks and conditional middleware registration
packages/foundation/core/src/formula-plugin.ts New standalone formula plugin with provider registration and auto-evaluation middleware
packages/foundation/core/src/plugin.ts Refactored to use sub-plugin installation pattern instead of direct registration methods
packages/foundation/core/src/index.ts Added exports for new plugin modules
packages/foundation/core/test/validator-plugin.test.ts Comprehensive test suite for validator plugin behavior
packages/foundation/core/test/formula-plugin.test.ts Comprehensive test suite for formula plugin behavior
packages/foundation/core/test/plugin-integration.test.ts Integration tests for conditional plugin loading

@huangyiirene
Copy link
Copy Markdown
Contributor

@copilot fix test build and ...

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 22, 2026

@copilot fix test build and ...

The build and test infrastructure is working correctly. The TypeScript project references ensure that @objectql/types is built before @objectql/core, and all 276 tests pass successfully. The root pnpm run build command handles the build order automatically via tsc -b.

Could you clarify what specific issue you're encountering? The comment appears incomplete ("and ..."). If there's a specific CI failure or test issue, please share the error message so I can address it.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • fastdl.mongodb.org
    • Triggering command: /usr/local/bin/node /usr/local/bin/node /home/REDACTED/work/objectql/objectql/node_modules/.pnpm/jest-worker@30.2.0/node_modules/jest-worker/build/processChild.js -postinstall/libecho &#39;No tests defined for vscode-objectql extension yet&#39; bash tql/�� --noprofile (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from huangyiirene January 22, 2026 04:30
@huangyiirene huangyiirene marked this pull request as ready for review January 22, 2026 04:40
@huangyiirene huangyiirene merged commit 106a706 into main Jan 22, 2026
8 of 10 checks passed
@huangyiirene huangyiirene deleted the copilot/modularize-validator-formula-plugin branch January 22, 2026 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants