import type { Constructor } from '../../types.js'; import type { BaseComponent } from '../../base_component.js'; import type { InferInput, Infer, ConstructableSchema } from '@vinejs/vine/types'; /** * Error bag for storing validation errors * Key: field name, Value: array of error messages */ export type ErrorBag = Record; /** * Trait/mixin for handling validation errors on components * * Provides methods to manage validation errors (error bag) on components. * Uses the component's data store to persist error state. * * @example * ```typescript * class FormComponent extends Component { * async submit() { * try { * await validator.validate(...) * } catch (error) { * if (error instanceof ValidationException) { * this.setErrorBag(error.messages) * } * } * } * } * ``` */ export declare function HandlesValidation>(Base: T): { new (...args: any[]): { /** * Extract component data for validation * Similar to Livewire.generateComponentData but as instance method */ "__#174@#getComponentData": () => Record; /** * Extract validation errors from Vine.js error object * Follows the same pattern as Form.validate() in adonis project */ "__#174@#extractVineErrors": (error: any) => ErrorBag; /** * Get the error bag for this component * Returns an empty error bag if none exists * * @returns Error bag object with field names as keys and error messages as arrays * * @example * ```typescript * const errors = component.getErrorBag() * if (errors.email) { * console.log('Email errors:', errors.email) * } * ``` */ getErrorBag(): ErrorBag; /** * Set the error bag for this component * Accepts either an ErrorBag object or a plain object * * @param bag - Error bag object or plain object with error messages * * @example * ```typescript * component.setErrorBag({ * email: ['Email is required'], * password: ['Password must be at least 8 characters'] * }) * * component.setErrorBag({ email: 'Invalid email' }) * ``` */ setErrorBag(bag: ErrorBag | Record): void; /** * Add an error to a specific field * * @param field - Field name * @param message - Error message * * @example * ```typescript * component.addError('email', 'Email is required') * component.addError('email', 'Email must be valid') * ``` */ addError(field: string, message: string): void; /** * Reset error bag, optionally for specific fields only * * @param fields - Optional field names to reset. If not provided, resets all errors * * @example * ```typescript * component.resetErrorBag() * * component.resetErrorBag(['email', 'password']) * * component.resetErrorBag('email') * ``` */ resetErrorBag(fields?: string | string[]): void; /** * Clear validation errors for specific fields or all fields * Alias for resetErrorBag * * @param fields - Optional field names to clear. If not provided, clears all errors */ clearValidation(fields?: string | string[]): void; /** * Reset validation errors for specific fields or all fields * Alias for resetErrorBag * * @param fields - Optional field names to reset. If not provided, resets all errors */ resetValidation(fields?: string | string[]): void; /** * Check if a specific field has errors * * @param field - Field name to check * @returns True if the field has errors, false otherwise * * @example * ```typescript * if (component.hasError('email')) { * console.log('Email field has errors') * } * ``` */ hasError(field: string): boolean; /** * Get error messages for a specific field * * @param field - Field name * @returns Array of error messages for the field, or empty array if no errors * * @example * ```typescript * const emailErrors = component.getError('email') * ``` */ getError(field: string): string[]; /** * Get the first error message for a specific field * * @param field - Field name * @returns First error message, or undefined if no errors * * @example * ```typescript * const firstError = component.getFirstError('email') * ``` */ getFirstError(field: string): string | undefined; /** * Optional method to define validation rules as a Vine.js schema * * If defined, this method should return a ConstructableSchema that will be used * for validation when validate() is called. * * @returns Vine.js schema object or undefined * * @example * ```typescript * rules?() { * return vine.object({ * name: vine.string().minLength(3), * email: vine.string().email() * }) * } * ``` */ rules?(): ConstructableSchema | undefined; /** * Validate component data using schema from rules() method or @validate decorators * * Priority: * 1. rules() method on component * 2. @validate decorators on properties (builds schema from HasValidate properties) * * Returns: * - ReturnType of rules() method if defined * - ValidatedProperties if using @validate decorators * * @param data - Optional data object to validate. If omitted, uses component state. * @returns Promise resolving to validated data typed according to rules() return type or @validate properties * @throws Error if no schema found (no rules() method and no @validate decorators) * * @example * ```typescript * // Using @validate decorators (typed automatically) * class MyComponent extends Component { * @validate(() => vine.string().minLength(3)) * declare name: HasValidate * * @validate(() => vine.string().email()) * declare email: HasValidate * * async save() { * const data = await this.validate() * // data is typed as { name: string; email: string } * } * } * ``` * * @example * ```typescript * class MyComponent extends Component { * rules() { * return vine.object({ * name: vine.string().minLength(3), * email: vine.string().email() * }) * } * * async submit() { * // validated is typed as { name: string, email: string } from rules() * const validated = await this.validate() * } * } * ``` * class MyComponent extends Component { * @validate(() => vine.string().minLength(3)) * declare name: HasValidate * * @validate(() => vine.string().email()) * declare email: HasValidate * * async submit() { * // validated is typed as { name: string, email: string } from ValidatedProperties * const validated = await this.validate() * } * } * ``` */ validate(data?: Record): Promise>; /** * Build Vine.js schema from @validate decorators on properties */ "__#174@#buildSchemaFromValidators": () => Promise | undefined>; /** * Validate component data using a Vine.js schema * * @param schema - Vine.js validation schema (required) * @param data - Optional data to validate. If not provided, uses component properties * @returns Promise resolving to validated data with proper type inference * @throws Validation error if validation fails (errors are automatically set in error bag) * * @example * ```typescript * import vine from '@vinejs/vine' * * const schema = vine.object({ * name: vine.string().minLength(3), * email: vine.string().email() * }) * * const validated = await this.validateUsing(schema) * // validated is typed as { name: string, email: string } * ``` */ validateUsing>(schema: TSchema, data?: InferInput): Promise>; "__#170@#id": string; "__#170@#name": string; "__#170@#viewPath": string; "__#170@#view": ReturnType; "__#170@#viewData": Record; "__#170@#router": import("@adonisjs/core/types").HttpRouterService; bindings: any; app: import("@adonisjs/core/types").ApplicationService; ctx: import("@adonisjs/http-server").HttpContext; __setRouter(router: import("@adonisjs/core/types").HttpRouterService): void; __getRouter(): import("@adonisjs/core/types").HttpRouterService; __id: string; __name: string; viewPath: string; view: ReturnType; viewData: Record; setId(id: string): void; getId(): string; setName(name: string): void; setViewPath(view: string): void; getName(): string; render(): Promise; skipRender(html?: string): void; transition(type?: string): void; skipTransition(): void; shouldSkipRender(): boolean; skipMount(): void; skipHydrate(): void; }; } & T;