API reference › @evolu/common › Type › includes

function includes<Substring extends string>(
  substring: Substring & ValidateLiteral<Substring>,
): BrandFactory<`Includes${Substring}`, string, IncludesError<Substring>>;

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

String Brand requiring an exact, case-sensitive substring.

The substring may occur anywhere, including across line breaks. Validation preserves the complete string. Every string contains the empty string, so an empty substring accepts every string allowed by the parent Type. The substring must be one concrete string literal so different substrings have distinct brands.

Like String.prototype.includes, matching compares UTF-16 code units, so a precomposed é does not match e followed by a combining accent. Use normalized to require one Unicode normalization form.

Example

import {
  assertEqual,
  assertErr,
  assertOk,
  assertType,
  includes,
  maxLength,
  String,
  type Brand,
} from "@evolu/common";

const Greeting = includes("{name}")(maxLength(100)(String));

const greeting = Greeting.fromUnknown("Hello, {name}!");
assertOk(greeting, "Hello, {name}!");
assertType<
  typeof greeting.value,
  string & Brand<"MaxLength100"> & Brand<"Includes{name}">
>();

const invalid = Greeting.fromUnknown("Hello, {Name}!");
assertErr(invalid, {
  type: "Includes{name}",
  value: "Hello, {Name}!",
  substring: "{name}",
});
assertEqual(
  Greeting.formatError(invalid.error),
  'The value "Hello, {Name}!" must contain "{name}".',
);