/** * A configuration model that dictates the configuration for the card form. */ export interface CardFormConfiguration { /** * A boolean value indicating whether the cardholder's name field * is required in the card form. * * If `true`, the cardholder's name must be collected and provided. * If `false`, the cardholder's name is optional. */ isCardHolderNameRequired?: boolean; /** * A map of custom validation rules for the card holder name field. * The key represents the error message to display when validation fails, * and the value represents the regex pattern string. * * Example: * ```typescript * { * "Card holder name must contain only letters and spaces": "^[a-zA-Z\\s]+$", * "Card holder name must be between 2 and 50 characters": "^.{2,50}$" * } * ``` */ cardHolderValidations?: Record; /** * A boolean value indicating if MoneyHash should validate the card number. * * If `true`, MoneyHash will validate the entered card number. * If `false`, MoneyHash will not validate the entered card number. */ enableCardNumberValidation?: boolean; } /** * Creates a `CardFormConfiguration` from a partial JSON object. * * This allows the user to set only one of the configurations and the other * one will default to `false`. * * @param json - The JSON object to parse. * @returns A new instance of `CardFormConfiguration`. */ export function cardFormConfigurationFromJson( json: Partial> ): CardFormConfiguration { return { isCardHolderNameRequired: json.isCardHolderNameRequired ?? false, cardHolderValidations: json.cardHolderValidations ?? {}, enableCardNumberValidation: json.enableCardNumberValidation ?? true, }; } /** * Converts a `CardFormConfiguration` to a JSON object. * * This ensures that both properties are always included in the output, even if * only one of them was set by the user. If not explicitly set, it will be `false`. * * @param config - The `CardFormConfiguration` instance to convert. * @returns A JSON representation of the configuration. */ export function cardFormConfigurationToJson( config: Partial ): Record { return { isCardHolderNameRequired: config.isCardHolderNameRequired ?? false, cardHolderValidations: config.cardHolderValidations ?? {}, enableCardNumberValidation: config.enableCardNumberValidation ?? true, }; }