import { NormalizedEntities, Entity } from '../normalization/normalizer'; /** * Relation definition for integrity checking */ export interface RelationDefinition { /** Source entity type */ from: string; /** Field containing the reference */ field: string; /** Target entity type */ to: string; /** Whether the relation is required */ required?: boolean; /** Whether it's an array relation */ isArray?: boolean; /** Cascade delete behavior */ onDelete?: 'cascade' | 'set-null' | 'restrict' | 'no-action'; } /** * Custom constraint definition */ export interface ConstraintDefinition { /** Constraint name */ name: string; /** Entity type to apply constraint to */ entity: string; /** Validation function */ validate: (entity: Entity, allEntities: NormalizedEntities) => boolean; /** Error message */ message: string | ((entity: Entity) => string); /** Severity level */ severity?: 'error' | 'warning' | 'info'; /** Repair function */ repair?: (entity: Entity, allEntities: NormalizedEntities) => Entity; } /** * Anomaly detection rule */ export interface AnomalyRule { /** Rule name */ name: string; /** Entity type to check */ entity?: string; /** Detection function */ detect: (entities: NormalizedEntities) => AnomalyResult[]; /** Severity level */ severity?: 'error' | 'warning' | 'info'; } /** * Anomaly detection result */ export interface AnomalyResult { /** Entity type */ entityType: string; /** Entity ID (if applicable) */ entityId?: string; /** Anomaly description */ description: string; /** Suggested fix */ suggestion?: string; /** Additional data */ data?: unknown; } /** * Integrity violation */ export interface IntegrityViolation { /** Violation type */ type: 'referential' | 'constraint' | 'anomaly' | 'orphan'; /** Severity */ severity: 'error' | 'warning' | 'info'; /** Entity type */ entityType: string; /** Entity ID */ entityId: string; /** Violation message */ message: string; /** Field involved (for referential) */ field?: string; /** Related entity info */ related?: { entityType: string; entityId: string; }; /** Repair suggestion */ repair?: { action: 'delete' | 'update' | 'create' | 'nullify'; data?: unknown; }; } /** * Integrity check report */ export interface IntegrityReport { /** Overall validity */ valid: boolean; /** Timestamp */ timestamp: number; /** Duration in milliseconds */ duration: number; /** Entity counts */ entityCounts: Record; /** Violations by type */ violations: IntegrityViolation[]; /** Summary statistics */ stats: { total: number; errors: number; warnings: number; info: number; }; } /** * Integrity checker configuration */ export interface IntegrityCheckerConfig { /** Entity types to check */ entities: string[]; /** Relation definitions */ relations?: RelationDefinition[]; /** Custom constraints */ constraints?: ConstraintDefinition[]; /** Anomaly detection rules */ anomalyRules?: AnomalyRule[]; /** ID field name */ idField?: string; /** Enable orphan detection */ detectOrphans?: boolean; /** Stop on first error */ failFast?: boolean; } /** * Repair options */ export interface RepairOptions { /** Only fix errors (not warnings) */ errorsOnly?: boolean; /** Dry run (return changes without applying) */ dryRun?: boolean; /** Custom repair handlers */ handlers?: Record NormalizedEntities>; } /** * Repair result */ export interface RepairResult { /** Repaired entities */ entities: NormalizedEntities; /** Applied repairs */ repairs: Array<{ violation: IntegrityViolation; action: string; success: boolean; }>; /** Remaining violations */ remaining: IntegrityViolation[]; } /** * Integrity checker interface */ export interface IntegrityChecker { /** Run integrity check */ check: (entities: NormalizedEntities) => IntegrityReport; /** Check specific entity */ checkEntity: (entityType: string, entityId: string, entities: NormalizedEntities) => IntegrityViolation[]; /** Repair violations */ repair: (entities: NormalizedEntities, report: IntegrityReport, options?: RepairOptions) => RepairResult; /** Add constraint */ addConstraint: (constraint: ConstraintDefinition) => void; /** Add relation */ addRelation: (relation: RelationDefinition) => void; /** Add anomaly rule */ addAnomalyRule: (rule: AnomalyRule) => void; /** Get configuration */ getConfig: () => IntegrityCheckerConfig; } /** * Create an integrity checker * * @param config - Checker configuration * @returns Integrity checker instance * * @example * ```typescript * const checker = createIntegrityChecker({ * entities: ['users', 'posts', 'comments'], * relations: [ * { from: 'posts', field: 'authorId', to: 'users', required: true }, * { from: 'comments', field: 'postId', to: 'posts', required: true }, * { from: 'comments', field: 'userId', to: 'users' }, * ], * constraints: [ * { * name: 'non-empty-title', * entity: 'posts', * validate: (post) => typeof post.title === 'string' && post.title.length > 0, * message: 'Post must have a non-empty title', * severity: 'error', * }, * ], * }); * * const report = checker.check(normalizedState); * console.log(report.violations); * ``` */ export declare function createIntegrityChecker(config: IntegrityCheckerConfig): IntegrityChecker; /** * Create duplicate detection rule */ export declare function createDuplicateDetectionRule(entityType: string, fields: string[]): AnomalyRule; /** * Create stale data detection rule */ export declare function createStaleDataRule(entityType: string, timestampField: string, maxAgeMs: number): AnomalyRule; /** * Create missing required fields rule */ export declare function createRequiredFieldsRule(entityType: string, requiredFields: string[]): AnomalyRule; /** * Create data consistency rule (cross-entity validation) */ export declare function createConsistencyRule(name: string, check: (entities: NormalizedEntities) => AnomalyResult[]): AnomalyRule; /** * Create unique field constraint */ export declare function createUniqueConstraint(entityType: string, field: string): ConstraintDefinition; /** * Create range constraint */ export declare function createRangeConstraint(entityType: string, field: string, min?: number, max?: number): ConstraintDefinition; /** * Create pattern constraint */ export declare function createPatternConstraint(entityType: string, field: string, pattern: RegExp, description: string): ConstraintDefinition; /** * Create enum constraint */ export declare function createEnumConstraint(entityType: string, field: string, allowedValues: readonly unknown[]): ConstraintDefinition;