import { Result } from '../../../utils/src/index.ts'; /** * Business rule validator function type */ export type BusinessRuleValidator = (value: T) => boolean | Promise; /** * Value object generator strategy */ export type ValueObjectStrategy = 'random' | 'sequential' | 'template' | 'custom' | ((index?: number) => T | Promise); /** * Constraint specification for value object generation */ export interface ValueObjectConstraints { /** Minimum value (for numeric types) */ min?: number; /** Maximum value (for numeric types) */ max?: number; /** Minimum length (for string types) */ minLength?: number; /** Maximum length (for string types) */ maxLength?: number; /** Regular expression pattern (for string types) */ pattern?: RegExp; /** Allowed values (enum-like constraints) */ allowedValues?: unknown[]; /** Forbidden values */ forbiddenValues?: unknown[]; /** Custom constraint validator */ customValidator?: (value: unknown) => boolean; /** Geographic constraints */ geographic?: { country?: string; region?: string; city?: string; coordinates?: { lat: number; lng: number; radius?: number; }; }; /** Temporal constraints */ temporal?: { after?: Date; before?: Date; timezone?: string; businessHours?: boolean; }; /** Additional arbitrary constraints */ [key: string]: unknown; } /** * Configuration for value object generation */ export interface ValueObjectBuilderConfig { /** Generation strategy to use */ strategy?: ValueObjectStrategy; /** Enable business rule validation */ enableValidation?: boolean; /** Maximum retry attempts for constraint violations */ maxRetryAttempts?: number; /** Custom data generator function */ customGenerator?: (constraints?: ValueObjectConstraints) => T | Promise; /** Locale for localized data generation */ locale?: string; /** Seed for reproducible random generation */ seed?: number; } /** * Template for creating predefined value object patterns */ export interface ValueObjectTemplate { name: string; description?: string; generator: (index?: number) => T | Promise; constraints?: ValueObjectConstraints; businessRules?: string[]; } /** * Result of value object validation */ export interface ValidationResult { isValid: boolean; violations: string[]; warnings: string[]; } /** * Builder class for creating DDD value objects with business rule compliance. * * This builder provides intelligent value object generation that: * - Respects business rules and domain constraints * - Generates realistic data appropriate for the value object type * - Handles constraint violations through retry mechanisms * - Supports custom validation and business rule checking * - Integrates with geographic and temporal constraint systems */ export declare class ValueObjectBuilder { private readonly ValueObjectClass; private readonly config; private constraints; private businessRules; private templates; private customValidators; /** * Creates a new ValueObjectBuilder for the specified value object type. * * @param ValueObjectClass Constructor for the value object * @param config Optional configuration for generation behavior */ constructor(ValueObjectClass: new (...args: any[]) => T, config?: ValueObjectBuilderConfig); /** * Sets constraints for value object generation. * * @param constraints Constraint specification * @returns Builder instance for method chaining * * @example * ```typescript * const ageBuilder = new ValueObjectBuilder(AgeVO) * .withConstraints({ * min: 18, * max: 120, * forbiddenValues: [0, -1] * }); * ``` */ withConstraints(constraints: ValueObjectConstraints): this; /** * Enables specific business rules for validation. * * @param rules Array of business rule names or custom validators * @returns Builder instance for method chaining * * @example * ```typescript * const emailBuilder = new ValueObjectBuilder(EmailVO) * .withBusinessRules([ * 'valid-format', * 'no-disposable-emails', * 'domain-whitelist' * ]); * ``` */ withBusinessRules(rules: Array>): this; /** * Adds a custom validation function. * * @param validator Function that validates value objects * @returns Builder instance for method chaining * * @example * ```typescript * const priceBuilder = new ValueObjectBuilder(PriceVO) * .withCustomValidator(async (price) => ({ * isValid: price.amount > 0 && price.currency !== 'INVALID', * violations: price.amount <= 0 ? ['Amount must be positive'] : [], * warnings: [] * })); * ``` */ withCustomValidator(validator: (value: T) => ValidationResult | Promise): this; /** * Registers a template for reusable value object patterns. * * @param template Template configuration * @returns Builder instance for method chaining * * @example * ```typescript * const polishAddressTemplate: ValueObjectTemplate = { * name: 'polish-urban-address', * description: 'Realistic Polish urban address', * generator: () => AddressVO.create({ * street: faker.location.streetAddress(), * city: faker.helpers.arrayElement(['Warsaw', 'Krakow', 'Gdansk']), * postalCode: faker.location.zipCode('##-###'), * country: 'Poland' * }), * constraints: { geographic: { country: 'Poland' } } * }; * * const addressBuilder = new ValueObjectBuilder(AddressVO) * .withTemplate(polishAddressTemplate); * ``` */ withTemplate(template: ValueObjectTemplate): this; /** * Sets the generation strategy. * * @param strategy Strategy to use for value object generation * @returns Builder instance for method chaining */ withStrategy(strategy: ValueObjectStrategy): this; /** * Builds a single value object instance. * * @param templateName Optional template to use for generation * @param overrides Optional property overrides * @returns Promise resolving to created value object or error * * @example * ```typescript * // Build with random generation * const email = await emailBuilder.build(); * * // Build with template * const polishAddress = await addressBuilder.build('polish-urban-address'); * * // Build with overrides * const customEmail = await emailBuilder.build(undefined, { domain: 'company.com' }); * ``` */ build(templateName?: string, overrides?: Partial): Promise>; /** * Builds multiple value object instances. * * @param count Number of value objects to create * @param templateName Optional template to use for all instances * @param overrideGenerator Optional function to generate per-instance overrides * @returns Promise resolving to array of value objects or error * * @example * ```typescript * // Create 100 random emails * const emails = await emailBuilder.buildMany(100); * * // Create varied emails with different domains * const corporateEmails = await emailBuilder.buildMany(50, undefined, (index) => ({ * domain: index < 25 ? 'company.com' : 'partner.com' * })); * ``` */ buildMany(count: number, templateName?: string, overrideGenerator?: (index: number) => Partial): Promise>; /** * Gets available templates. * * @returns Array of template names and descriptions */ getTemplates(): Array<{ name: string; description?: string; }>; /** * Gets current constraints. * * @returns Current constraint specification */ getConstraints(): Readonly; /** * Gets registered business rules. * * @returns Array of business rule names */ getBusinessRules(): string[]; /** * Generates value object data based on strategy and constraints. */ private generateValueObjectData; /** * Generates data based on the configured strategy. */ private generateByStrategy; /** * Generates random data appropriate for the value object type. */ private generateRandomData; /** * Generates sequential data for predictable patterns. */ private generateSequentialData; /** * Applies constraints to generated data. */ private applyConstraints; /** * Generates a value that complies with the given regex pattern. */ private generatePatternCompliantValue; /** * Validates a value object against business rules and constraints. */ private validateValueObject; /** * Initializes built-in business rule validators. */ private initializeBuiltInValidators; }