ajt-validator
Version:
Validation library for JavaScript and TypeScript
75 lines (74 loc) • 2.53 kB
TypeScript
import { BaseValidator } from '../base';
import { ValidationResult } from '../../interfaces';
/**
* Options for configuring the age validation
*/
export interface AgeValidatorOptions {
/** Minimum age allowed (inclusive) */
minAge?: number;
/** Maximum age allowed (inclusive) */
maxAge?: number;
/** Whether to allow decimal ages (e.g., 18.5 years) */
allowDecimals?: boolean;
/** Additional validation for specific ranges */
ageRanges?: {
/** Name of the range category (e.g., "child", "adult", "senior") */
name: string;
/** Minimum age for this range (inclusive) */
min: number;
/** Maximum age for this range (inclusive) */
max: number;
}[];
/** Whether to validate age based on date of birth and current date */
validateFromDate?: boolean;
}
/**
* Result of age validation with additional metadata
*/
export interface AgeValidationResult extends ValidationResult<number> {
/** Range category the age falls into, if defined in options */
ageCategory?: string;
/** Age calculated from date of birth, if provided */
calculatedAge?: number;
/** Years, months, days representation (for detailed age) */
detailed?: {
years: number;
months: number;
days: number;
};
}
/**
* Validator for age fields
* Can validate ages as numbers or calculate from dates
*/
export declare class AgeValidator extends BaseValidator<number | Date, number> {
private options;
/**
* Create a new age validator
* @param options Configuration options for age validation
*/
constructor(options?: AgeValidatorOptions);
/**
* Validate an age value or calculate and validate from birth date
* @param value Age as number or birth date
* @returns Validation result with additional age metadata
*/
validate(value: number | Date): AgeValidationResult;
/**
* Validate age range category
* @param age Age to validate
* @param categoryName Name of the category to check
* @returns Whether the age falls into the specified category
*/
isInAgeRange(age: number, categoryName: string): boolean;
/**
* Calculate age from birth date
* @param birthDate Date of birth
* @returns Calculated age in years and detailed breakdown
*/
private calculateAgeFromDate;
/**
* Create an error result for age validation
*/
protected createError(code: string, message: string): AgeValidationResult;
}