API reference › @evolu/common › Type › wellFormed

const wellFormed: BrandFactory<"WellFormed", string, WellFormedError>;

Defined in: packages/common/src/Type.ts:7629

String Brand for well-formed Unicode text without lone surrogates.

A JavaScript string is a sequence of UTF-16 code units, so it can contain a high surrogate without the low surrogate that completes it, or the reverse, for example after slicing an emoji in half. Such text has no UTF-8 encoding: TextEncoder and many storage and network APIs replace each lone surrogate with U+FFFD, so the text that arrives differs from the text that was sent. Validation leaves the text unchanged.

Example

import {
  assertErr,
  assertOk,
  maxLength,
  String,
  wellFormed,
} from "@evolu/common";

const Message = wellFormed(maxLength(100)(String));

assertOk(Message.fromUnknown("Hi 😀"), "Hi 😀");
assertErr(Message.fromUnknown("Hi 😀".slice(0, 4)));