ajt-validator
Version:
Validation library for JavaScript and TypeScript
97 lines (96 loc) • 3.17 kB
TypeScript
import { BaseValidator } from "../base";
import { ValidationResult } from "../../interfaces";
/**
* Standard gender options
*/
export declare enum GenderOption {
MALE = "male",
FEMALE = "female",
NON_BINARY = "non-binary",
OTHER = "other",
PREFER_NOT_TO_SAY = "prefer-not-to-say"
}
/**
* Options for configuring gender validation
*/
export interface GenderValidatorOptions {
/** List of allowed gender values */
allowedValues?: string[];
/** Whether to allow custom/self-described gender entries */
allowCustom?: boolean;
/** Maximum length for custom gender entries (if allowed) */
customMaxLength?: number;
/** Whether values should be normalized (e.g., lowercase, trimmed) */
normalize?: boolean;
/** Whether to use case-sensitive validation */
caseSensitive?: boolean;
/** Whether to allow abbreviated values (e.g., 'M', 'F') */
allowAbbreviations?: boolean;
/** Map of recognized abbreviations to full values */
abbreviationMap?: Record<string, string>;
}
/**
* Result of gender validation with additional metadata
*/
export interface GenderValidationResult extends ValidationResult<string> {
/** Whether the value matched a standard option */
isStandardOption?: boolean;
/** Normalized value (if normalization is enabled) */
normalizedValue?: string;
/** Mapped value from abbreviation (if applicable) */
expandedValue?: string;
}
/**
* Validator for gender fields
* Supports standard options, custom entries, and abbreviations
*/
export declare class GenderValidator extends BaseValidator<string, string> {
private options;
private standardValues;
private abbreviations;
private lowercaseAllowedValues;
/**
* Create a new gender validator
* @param options Configuration options
*/
constructor(options?: GenderValidatorOptions);
/**
* Validate a gender value
* @param value Gender string to validate
* @returns Validation result with additional metadata
*/
validate(value: any): GenderValidationResult;
/**
* Check if value exists in allowed values list
* @param value Value to check
* @param allowedValues List of allowed values
* @returns Whether the value is allowed
*/
private checkAllowedValue;
/**
* Check if a value is a standard option
* @param value Value to check (assumed to be already normalized)
* @returns Whether the value is a standard option
*/
private checkIsStandardOption;
/**
* Expand an abbreviation to its full form
* @param abbr Abbreviation to expand
* @returns Expanded value if found, or undefined
*/
private expandAbbreviation;
/**
* Get all standard gender options
* @returns List of standard gender values
*/
getStandardOptions(): string[];
/**
* Get all allowed abbreviations
* @returns Map of abbreviations to full values
*/
getAbbreviations(): Record<string, string>;
/**
* Create an error result for gender validation
*/
protected createError(code: string, message: string): GenderValidationResult;
}