Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 12, 2026

Two core validation methods in validator.ts were empty stubs that always returned true. This PR implements their actual logic.

Changes

validateUniqueness

  • Queries database via context.api.count() to detect duplicate values
  • Supports single field (field) and composite (fields) uniqueness constraints
  • Excludes current record on updates using { _id: { $ne: previousRecord._id } }
  • Returns early on null values (nothing to validate)
  • Applies scope conditions when specified

validateBusinessRule

  • Evaluates all_of (AND), any_of (OR), and then_require constraint logic
  • Delegates to existing evaluateCondition method for field comparisons
  • Expression constraints remain stubbed (requires safe evaluator)

Helper

  • extractFieldsFromCondition: recursively extracts field names from nested conditions for scope filtering

Example Usage

const uniqueRule: UniquenessValidationRule = {
  name: 'unique_email',
  type: 'unique',
  field: 'email',
  message: 'Email already exists',
};

const context: ValidationContext = {
  record: { email: 'test@example.com' },
  operation: 'create',
  api: mockApi,
  metadata: { objectName: 'user' },
};

const result = await validator.validate([uniqueRule], context);
// Queries: count('user', { email: 'test@example.com' })
// Returns: { valid: false } if count > 0

Notes

  • Case-insensitive string comparison requires driver-specific implementation (documented in code)
  • Gracefully degrades when context.api is unavailable (returns valid: true)
Original prompt

This section details on the original issue you should resolve

<issue_title>Feature: Implement missing Validator logic (Uniqueness & Business Rules)</issue_title>
<issue_description>## Problem
In packages/foundation/core/src/validator.ts, key validation methods are currently empty stubs:

  • validateUniqueness: Returns true without checking the database.
  • validateBusinessRule: Returns true without evaluating expressions.

Goal

  • Implement validateUniqueness by checking the database count for the field value.
  • Implement basic validateBusinessRule logic.</issue_description>

Comments on the Issue (you are @copilot in this section)


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

Copilot AI and others added 2 commits January 12, 2026 21:33
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
…mments

Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement missing Validator logic for uniqueness and business rules Implement validateUniqueness and validateBusinessRule methods Jan 12, 2026
Copilot AI requested a review from hotlong January 12, 2026 21:39
@hotlong hotlong requested review from Copilot and removed request for hotlong January 12, 2026 21:43
@hotlong hotlong marked this pull request as ready for review January 12, 2026 22:54
@hotlong hotlong merged commit 0eda259 into main Jan 12, 2026
3 of 4 checks passed
@hotlong hotlong deleted the copilot/implement-validator-logic branch January 12, 2026 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Implement missing Validator logic (Uniqueness & Business Rules)

2 participants