/** * Authentication Validation Schemas * * Custom validation schemas for validating authentication form data. * Provides schema factories for sign-in, sign-up, and reset password forms, * as well as utility functions for email and password validation and * password strength calculation. * * @module auth/schemas */ import { StringValidator, SafeParseResult } from '../form/schema/custom-validation'; import { StandardSchemaV1 } from '../form/schema/standard-schema-v1'; import { PasswordRules, ResetPasswordData, SignInFormData, SignUpFormData } from './types'; /** * Localizable validation messages for authentication schemas. * * Pass this to schema factory functions to override the default English messages. * Obtain localized values from `AuthI18n` provider messages. */ export interface AuthValidationMessages { passwordMinLength?: (min: number) => string; passwordRequireUppercase?: string; passwordRequireLowercase?: string; passwordRequireNumber?: string; passwordRequireSpecialChar?: string; emailRequired?: string; invalidEmail?: string; passwordRequired?: string; nameRequired?: string; confirmPasswordRequired?: string; acceptTermsRequired?: string; passwordsDoNotMatch?: string; invalidPassword?: string; invalidEmailAddress?: string; } /** * Creates a password validation schema based on the provided rules. * * Builds a `StringValidator` that enforces minimum length, character class * requirements, and optional custom validation logic. * * @param rules - The password validation rules to enforce. Defaults to {@link defaultPasswordRules}. * @returns A `StringValidator` that validates passwords against the given rules. * * @example * ```ts * const schema = createPasswordSchema({ minLength: 10, requireNumbers: true }) * const result = schema.validate('hello123') * ``` */ export declare function createPasswordSchema(rules?: PasswordRules, messages?: AuthValidationMessages): StringValidator; /** * Creates an email validation schema with optional localized messages. * * @param messages - Optional localized validation messages * @returns A `StringValidator` that validates email addresses */ export declare function createEmailSchema(messages?: AuthValidationMessages): StringValidator; /** * Base email validation schema. * * Validates that the value is a non-empty string with a valid email format. */ export declare const emailSchema: StringValidator; /** * Creates a validation schema for the sign-in form. * * Validates email (required, valid format) and password fields. * When `passwordRules` is provided, the password field is validated * against those rules; otherwise, only a non-empty check is applied. * * @param passwordRules - Optional password validation rules. If omitted, only a "required" check is used. * @returns A `StandardSchemaV1` with a `safeParse` method. * * @example * ```ts * const schema = createSignInSchema({ minLength: 8, requireNumbers: true }) * const result = schema.safeParse({ email: 'user@test.com', password: 'pass1234' }) * ``` */ export declare function createSignInSchema(passwordRules?: PasswordRules, messages?: AuthValidationMessages): StandardSchemaV1 & { safeParse: (value: unknown) => SafeParseResult; }; /** * Creates a validation schema for the sign-up form. * * Validates name (optional), email, password, confirm password, and * terms acceptance fields. Password confirmation matching is enforced * when `showConfirmPassword` is enabled. * * @param passwordRules - Password validation rules. Defaults to {@link defaultPasswordRules}. * @param options - Optional flags controlling which fields are shown and required. * @param options.showNameField - Whether the name field is shown. @default true * @param options.showConfirmPassword - Whether the confirm password field is shown. @default true * @param options.showAcceptTermsAndConditions - Whether the terms checkbox is shown. @default true * @returns A `StandardSchemaV1` with a `safeParse` method. * * @example * ```ts * const schema = createSignUpSchema(undefined, { showNameField: false }) * const result = schema.safeParse({ * email: 'user@test.com', * password: 'Pass1234', * confirmPassword: 'Pass1234', * acceptTerms: true, * }) * ``` */ export declare function createSignUpSchema(passwordRules?: PasswordRules, options?: { showNameField?: boolean; showConfirmPassword?: boolean; showAcceptTermsAndConditions?: boolean; }, messages?: AuthValidationMessages): StandardSchemaV1 & { safeParse: (value: unknown) => SafeParseResult; }; /** * Validation schema for the reset password form. * * Validates that the email field is present and in a valid format. */ export declare const resetPasswordSchema: StandardSchemaV1 & { safeParse: (value: unknown) => SafeParseResult; }; /** * Pre-built sign-in schema using default password rules. * * Convenience constant equivalent to `createSignInSchema()`. */ export declare const defaultSignInSchema: StandardSchemaV1 & { safeParse: (value: unknown) => SafeParseResult; }; /** * Pre-built sign-up schema using default password rules. * * Convenience constant equivalent to `createSignUpSchema()`. */ export declare const defaultSignUpSchema: StandardSchemaV1 & { safeParse: (value: unknown) => SafeParseResult; }; /** * Alias for {@link ResetPasswordData}, used as the form data type for the reset password form. */ export type ResetPasswordFormData = ResetPasswordData; /** * Collection of schema factory functions for dynamic configuration. * * Provides convenient access to all auth schema creators. * * @example * ```ts * const signInSchema = authSchemas.signIn({ minLength: 12 }) * const resetSchema = authSchemas.resetPassword() * ``` */ export declare const authSchemas: { signIn: typeof createSignInSchema; signUp: typeof createSignUpSchema; resetPassword: () => StandardSchemaV1 & { safeParse: (value: unknown) => SafeParseResult; }; }; /** * Validates an email address against the email schema. * * @param email - The email address to validate. * @returns An error message string if invalid, or `null` if valid. * * @example * ```ts * validateEmail('user@test.com') // null * validateEmail('invalid') // 'Please enter a valid email address' * ``` */ export declare function validateEmail(email: string, messages?: AuthValidationMessages): string | null; /** * Validates a password against the provided rules. * * @param password - The password to validate. * @param rules - The password validation rules to check against. Defaults to {@link defaultPasswordRules}. * @returns An error message string if invalid, or `null` if valid. * * @example * ```ts * validatePassword('weak', { minLength: 8 }) // 'Password must be at least 8 characters' * validatePassword('StrongPass1', { minLength: 8, requireUppercase: true, requireNumbers: true }) // null * ``` */ export declare function validatePassword(password: string, rules?: PasswordRules, messages?: AuthValidationMessages): string | null; /** * Calculates the strength of a password based on the provided rules. * * Evaluates each enabled rule and produces a strength level, a numerical * score (0-100), and a detailed breakdown of which checks passed. * * @param password - The password to evaluate. * @param rules - The password rules to evaluate against. Defaults to {@link defaultPasswordRules}. * @returns An object containing: * - `strength` - The qualitative strength level: `'weak'`, `'fair'`, `'good'`, or `'strong'`. * - `score` - A numerical score from 0 to 100. * - `checks` - An object indicating which individual checks passed. * * @example * ```ts * const result = calculatePasswordStrength('Hello123', { * minLength: 8, * requireUppercase: true, * requireNumbers: true, * }) * // result.strength === 'good' * // result.score === 67 * ``` */ export declare function calculatePasswordStrength(password: string, rules?: PasswordRules): { strength: 'weak' | 'fair' | 'good' | 'strong'; score: number; checks: { length: boolean; uppercase: boolean; lowercase: boolean; numbers: boolean; symbols: boolean; custom: boolean; }; };