/** * CmpStr Options Validator * src/utils/OptionsValidator.ts * * This module provides the OptionsValidator class, which contains static methods for validating * the options passed to the CmpStr function. It checks for correct types, allowed values, and * the existence of specified metrics and phonetic algorithms in their respective registries. * * If any validation fails, a CmpStrValidationError is thrown with a descriptive message and * relevant details about the invalid option. * * @module Utils * @name OptionsValidator * @author Paul Köhler (komed3) * @license MIT */ import type { CmpStrOptions, CmpStrProcessors, MetricOptions, PhoneticOptions, StructuredDataOptions } from './Types'; /** * Utility for validating CmpStr options. * * This class provides static methods to validate various aspects of the * options object passed to CmpStr. */ export declare class OptionsValidator { /** Allowed normalization flags */ private static readonly ALLOWED_FLAGS; /** Allowed output modes */ private static readonly ALLOWED_OUTPUT; /** Allowed comparison modes */ private static readonly ALLOWED_MODES; /** Allowed sort modes */ private static readonly ALLOWED_SORT; /** Processor dispatch table */ private static readonly PROCESSORS; /** Metric options validation dispatch table */ private static readonly METRIC_OPT_MAP; /** Phonetic algorithm options validation dispatch table */ private static readonly PHONETIC_OPT_MAP; /** CmpStr options validation dispatch table */ private static readonly CMPSTR_OPT_MAP; /** * Internal helper to convert a Set to a string for error messages. * * @param {Set< string >} set - The set to convert * @returns {string} - A string representation of the set */ private static set2string; /** * Internal helper to validate primitive types. * * @param {unknown} value - The value to validate. * @param {string} name - The name of the option (for error messages). * @param {'boolean' | 'number' | 'string'} type - The expected type of the value. * @throws {CmpStrValidationError} If the value is not of the expected type or is NaN (for numbers). */ private static validateType; /** * Internal helper to validate enum-like values. * * @param {unknown} value - The value to validate. * @param {string} name - The name of the option (for error messages). * @param {Set< string >} set - The set of allowed values. * @throws {CmpStrValidationError} If the value is not a string or is not in the allowed set. */ private static validateEnum; /** * Internal helper to validate objects against a dispatch table of validation functions. * * @param {unknown} opt - The object to validate. * @param {Object} map - A dispatch table mapping keys to validation functions. * @throws {CmpStrValidationError} If any property in the object fails validation. */ private static validateMap; /** * Internal helper to validate registry-based options (metrics and phonetic algorithms). * * @param {unknown} value - The value to validate. * @param {string} name - The name of the option (for error messages). * @param {string} label - The label to use in error messages (e.g. "Metric" or "Phonetic algorithm"). * @param {( v: string ) => boolean} has - A function that checks if the registry contains a given name. * @param {() => string[]} list - A function that returns a list of registered names for error messages. * @throws {CmpStrValidationError} If the value is not a non-empty string or is not registered. */ private static validateRegistryName; /** * Validate boolean-like values. * * @param {unknown} value - The value to validate * @param {string} name - The name of the option (for error messages) * @throws {CmpStrValidationError} - If the value is not a boolean */ static validateBoolean(value: unknown, name: string): void; /** * Validate number-like values. * * @param {unknown} value - The value to validate * @param {string} name - The name of the option (for error messages) * @throws {CmpStrValidationError} - If the value is not a number or is NaN */ static validateNumber(value: unknown, name: string): void; /** * Validate string-like values. * * @param {unknown} value - The value to validate * @param {string} name - The name of the option (for error messages) * @throws {CmpStrValidationError} - If the value is not a string */ static validateString(value: unknown, name: string): void; /** * Validate normalization flags. * * @param {unknown} value - The flags to validate * @throws {CmpStrValidationError} - If the flags are not a string or contain invalid characters */ static validateFlags(value: unknown): void; /** * Validate CmpStr output mode. * * @param {unknown} value - The output mode to validate * @throws {CmpStrValidationError} - If the output mode is not a string or not allowed */ static validateOutput(value: unknown): void; /** * Validate CmpStr comparison mode. * * @param {unknown} value - The comparison mode to validate * @throws {CmpStrValidationError} - If the comparison mode is not a string or not allowed */ static validateMode(value: unknown): void; /** * Validate CmpStr structured data sort mode. * * @param {unknown} value - The sort mode to validate * @param {string} name - The name of the option (for error messages) * @throws {CmpStrValidationError} - If the sort mode is neither 'asc', 'desc', nor a boolean */ static validateSort(value: unknown, name: string): void; /** * Validate metric name against the MetricRegistry. * * @param {unknown} value - The metric name to validate * @throws {CmpStrValidationError} - If the metric is not a string or not registered */ static validateMetricName(value: unknown): void; /** * Validate phonetic algorithm name against the PhoneticRegistry. * * @param {unknown} value - The phonetic algorithm name to validate * @throws {CmpStrValidationError} - If the phonetic algorithm is not a string or not registered */ static validatePhoneticName(value: unknown): void; /** * Validate metric options. * * @param {MetricOptions} opt - The metric options to validate * @throws {CmpStrValidationError} - If any metric option is invalid */ static validateMetricOptions(opt?: MetricOptions): void; /** * Validate phonetic options. * * @param {PhoneticOptions} opt - The phonetic options to validate * @throws {CmpStrValidationError} - If any phonetic option is invalid */ static validatePhoneticOptions(opt?: PhoneticOptions): void; /** * Validate processor options. * * This method iterates over the keys in the provided processor options and dispatches * validation to the corresponding function in the PROCESSORS table. * * If an invalid processor type is found, a CmpStrValidationError is thrown indicating * the invalid type and the expected processor types. * * @param {unknown} opt - The processor options to validate * @throws {CmpStrValidationError} - If any processor type is invalid */ static validateProcessors(opt?: CmpStrProcessors): void; /** * Validate the provided CmpStr options object. * * This method performs a comprehensive validation of the options object passed to CmpStr. * It checks for the presence and validity of all supported options, including primitive * types, enum-like values, registry-based names, and nested processor options. * * If any validation check fails, a CmpStrValidationError is thrown with a descriptive * message and relevant details about the invalid option. * * @param {CmpStrOptions | StructuredDataOptions} [opt] - The options object to validate * @throws {CmpStrValidationError} - If any validation check fails */ static validateOptions(opt?: CmpStrOptions | StructuredDataOptions): void; }