Skip to main content

Validation System

The SDK includes a robust multi-layer validation system that ensures data integrity and prevents API errors before they occur.

Phone Number Validation

For comprehensive error handling when validation fails, including detailed error codes and context, see our Enhanced Error Handling Guide.

validatePhoneNumber()

Validates international phone number format using E.164 standard.
Parameters:
  • phoneNumber - Phone number string to validate
Returns:
  • boolean - True if valid, false otherwise
Examples:
Regex Pattern:

Configuration Validation

validateConfig()

Validates WhatsApp client configuration and throws detailed errors for missing fields.
Parameters:
  • config - WhatsAppConfig object to validate
Throws:
  • ConfigurationError - For missing or invalid fields
Example:
Validation Rules:
  • accessToken - Required, must match format [A-Za-z0-9_-]+
  • phoneNumberId - Required, must be non-empty string
  • baseUrl - Optional, must be valid URL if provided
  • timeout - Optional, must be positive number

Message Validation

validateMessage()

Validates outgoing messages based on WhatsApp API requirements.
Parameters:
  • message - Outgoing message object to validate
Throws:
  • MessageValidationError - For invalid message content

Text Message Validation

Interactive Message Validation

List Message Validation

Media File Validation

validateMediaFile()

Validates media files against WhatsApp size and type restrictions.
Parameters:
  • buffer - File buffer to validate
  • type - Media type category
Throws:
  • MediaProcessingError - For oversized or invalid files
Size Limits: Example:

MIME Type Validation

Supported MIME Types:

Template Message Validation

Template Parameter Validation

Contact Message Validation

Contact Information Validation

Validation Error Handling

MessageValidationError

Usage Example:

Best Practices

1. Prevalidation

Always validate input before making API calls:

2. Media Size Checks

Check file sizes before uploading:

3. Text Length Validation

Validate text length for different contexts:

Validation Utilities

Text Sanitization

URL Validation

Phone Number Formatting

This validation system ensures that all data sent through the SDK meets WhatsApp API requirements, preventing errors and improving reliability.