requireValidSlug
function requireValidSlug(
value: string | null | undefined,
message: string = "Expected a valid slug."
): string;
Starts with a letter, then letters, digits, hyphens and underscores — the shape a URL segment or a key can take. Spaces, leading digits and an empty string are rejected.
isX and nonX answer a question and never interrupt control flow; requireX and requireNonX hand the value back or throw. The scheme is explained in Validation conventions.
Parameters
Parameter | Type | Description |
|---|---|---|
|
| The value to validate. |
|
| The message of the exception. A default is used when omitted. |
Returns
string — the same value, typed as a slug.
Throws
Exception | Condition |
|---|---|
| the value is not a slug. |
Examples
What passes and what does not
requireValidSlug("my-page"); // => "my-page"
requireValidSlug("a_1"); // => "a_1"
requireValidSlug("1-page"); // throws IllegalArgumentException
requireValidSlug("my page"); // throws IllegalArgumentException
See also
Source
23 September 2026
This documentation was generated with AI (Claude) from the library's source code.