validateLink property

LinkValidationCallback? validateLink
final

A callback to validate whether the link is valid.

The link is passed to the callback, which should return true if valid, or false otherwise.

Example:

validateLink: (link) {
  if (link.startsWith('ws')) {
    return true; // WebSocket links are considered valid
  }
  final regex = RegExp(r'^(http|https)://[a-zA-Z0-9.-]+');
  return regex.hasMatch(link);
}

Return null to fallback to the default handling:

validateLink: (link) {
  if (link.startsWith('custom')) {
    return true;
  }
  return null;
}

Another example to allow inserting any link:

validateLink: (link) {
  // Treats all links as valid. When launching the URL,
  // `https://` is prefixed if the link is incomplete (e.g., `google.com` → `https://google.com`)
  // however this happens only within the editor level and the
  // the URL will be stored as:
  // {insert: ..., attributes: {link: google.com}}
  return true;
}

NOTE: The link will always be considered invalid if empty, and this callback will not be called.

This callback is preferred over linkRegExp when both are set.

Implementation

// ignore: deprecated_member_use_from_same_package
/// This callback is preferred over [linkRegExp] when both are set.
final LinkValidationCallback? validateLink;