Skip to content
SMS SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

validate()

sms.validate(input): SmsValidationResult fields, ValidationIssue codes, and what local validation cannot check.

sms.validate(input)

Checks a message locally and estimates its segments. It is synchronous and free, and it never contacts a provider or claims that an account or sender is approved.

import { sms } from "./sms";

const check = sms.validate({ to: "+14155550123", body: "Your package is ready 📦" });

if (!check.supported) {
  for (const issue of check.issues) console.warn(issue.code, issue.field, issue.message);
}
console.log(check.encoding, check.segments, check.adapterCandidates);

Guide: Validation and encoding.

Parameters

  • input: SmsSendInput, the same input send() takes. See send().

Returns: SmsValidationResult

SegmentPreview plus four fields:

Field Type Meaning
supported boolean true when the primary adapter can send the message and the message has no issues.
issues readonly ValidationIssue[] Every problem found, across the message and every adapter.
adapterCandidates readonly string[] Adapters that could send it, in configured order. Empty when the message itself is invalid.
providerOptionsFor readonly string[] The candidates in adapterCandidates that have an entry in providerOptions, in configured order. Each of these sends its own entry if it ends up sending. Empty when the message itself is invalid or no candidate has an entry.
encoding "gsm7" | "ucs2"
segments number Estimated segments, 0 for an empty body.
units number Septets (GSM-7) or UTF-16 code units (UCS-2).
unitsPerSegment number 160 or 153 (GSM-7), 70 or 67 (UCS-2).
remainingInSegment number Units left before another segment starts.
containsUnicode boolean At least one character forces UCS-2.
nonGsmCharacters readonly string[] Distinct characters that forced UCS-2.

supported matches what send() does before any request. When the primary adapter makes supported false, send() throws even with fallback on.

Message-level issues come first: recipient, body, field formats, and providerOptions checked against every configured adapter. When there are any, adapters are not checked, and adapterCandidates and providerOptionsFor are empty.

ValidationIssue

type ValidationIssue = {
  readonly code: ValidationIssueCode;
  readonly field: ValidationField;
  readonly message: string;
  readonly provider?: string; // set when the issue applies to one adapter
};
code send() throws Meaning
invalid_recipient InvalidRecipientError to is not E.164.
invalid_sender InvalidSenderError The sender is malformed, such as a sender ID over 11 characters, a short code that is not 3 to 8 digits, a Twilio Messaging Service SID that is not MG plus 32 hex, or a Telnyx alphanumeric sender without messagingProfileId.
missing_sender InvalidSenderError No from and no adapter default.
empty_body InvalidMessageError Empty body without media, or any empty body on Vonage.
unsupported_field UnsupportedFieldError The adapter cannot send this field or sender type, Twilio sendAt without a Messaging Service sender, or a providerOptions key the adapter does not accept or that names a parameter the SDK sets.
invalid_field InvalidMessageError A malformed field, a value outside the adapter’s range, or a providerOptions value of the wrong type.

ValidationField is "to" | "from" | "body" | "mediaUrls" | "sendAt" | "validityPeriodSec" | "webhookUrl" | "idempotencyKey" | "providerOptions".

What it does not check

  • Whether the sender is provisioned or registered in your account.
  • Whether the destination accepts your sender type or MMS.
  • Recipient opt-outs held by the provider, account balance, or permissions.

The provider reports these as rejections at send time.

Types

SmsValidationResult, ValidationIssue, ValidationIssueCode, ValidationField, and SegmentPreview are exported as types from @opencoredev/sms-sdk.