UNPKG

ajt-validator

Version:

Validation library for JavaScript and TypeScript

103 lines (102 loc) 3.41 kB
import { BaseValidator } from "../base"; import { ValidationResult } from "../../interfaces"; /** * Options for configuring the Date of Birth validation */ export interface DOBValidatorOptions { /** Minimum age allowed (in years) */ minAge?: number; /** Maximum age allowed (in years) */ maxAge?: number; /** Whether to allow future dates */ allowFutureDates?: boolean; /** Reference date to use instead of current date for age calculations */ referenceDate?: Date; /** Specific valid date ranges */ validRanges?: { /** Name of the range */ name: string; /** Start date of valid range (inclusive) */ startDate: Date; /** End date of valid range (inclusive) */ endDate: Date; }[]; /** Format validation options */ format?: { /** Whether to strictly validate the format */ strict?: boolean; /** Allowed formats (YYYY-MM-DD, MM/DD/YYYY, etc.) */ allowedFormats?: string[]; }; } /** * Result of DOB validation with additional metadata */ export interface DOBValidationResult extends ValidationResult<Date> { /** Age calculated from the DOB */ age: { /** Age in years */ years: number; /** Age in months */ months: number; /** Age in days */ days: number; /** Total age in decimal years */ decimalYears: number; } | undefined; /** Category the DOB falls into, if defined in ranges */ category?: string; /** Whether the person is of legal age (based on minAge) */ isLegalAge?: boolean; } /** * Validator for Date of Birth fields * Validates dates and provides age calculations */ export declare class DOBValidator extends BaseValidator<string | Date, Date> { private options; private readonly DEFAULT_MIN_AGE; private readonly DEFAULT_MAX_AGE; /** * Create a new Date of Birth validator * @param options Configuration options for DOB validation */ constructor(options?: DOBValidatorOptions); /** * Validate a date of birth value * @param value DOB as string or Date object * @returns Validation result with additional DOB and age metadata */ validate(value: string | Date): DOBValidationResult; /** * Check if a person is of legal age based on their DOB * @param dob Date of birth * @param minAge Minimum age to be considered legal (defaults to options.minAge) * @returns Whether the person is of legal age */ isLegalAge(dob: Date, minAge?: number): boolean; /** * Get age at a specific reference date * @param dob Date of birth * @param referenceDate Date to calculate age at (defaults to current date) * @returns Age details */ getAgeAt(dob: Date, referenceDate?: Date): NonNullable<DOBValidationResult['age']>; /** * Calculate exact age from DOB * @param dob Date of birth * @param referenceDate Reference date (defaults to current date) * @returns Detailed age calculation */ private calculateAge; /** * Parse string date into Date object with format detection * @param dateStr Date string * @returns Parsed Date object */ private parseDate; /** * Create an error result for DOB validation */ protected createError(code: string, message: string): DOBValidationResult; }